gpt4 book ai didi

java - 为什么我没有打印却打印出这个

转载 作者:行者123 更新时间:2023-11-30 05:22:12 26 4
gpt4 key购买 nike

import java.util.Scanner;
import java.io.File;
import java.util.HashMap;
import java.util.ArrayList;


/**
* Class that contains helper methods for the Review Lab
**/
public class Review {

public static void main(String[] args) {
//System.out.print(sentimentVal("ok"));
//System.out.println(totalSentiment("simpleReview.txt"));
System.out.println(starRating("simpleReview.txt"));
//System.out.println(fakeReview("simpleReview.txt"));
}





/* public static double totalSentiment(String fileName) {
String review = textToString("simpleReview.txt");

int s = review.indexOf(" ");
double sum = 0;

//take first word from whole string
//review.substring(0 --> " ")

String firstWord = removePunctuation(review.substring(0, review.indexOf(" ")));
sum += sentimentVal(firstWord);

while(s != -1) {
int nextSpace = review.indexOf(" ", s + 1);
String word;

if (nextSpace != -1){
word = removePunctuation(review.substring(s + 1, nextSpace));
sum += sentimentVal(word);


} else {
word = removePunctuation(review.substring(s + 1));
sum += sentimentVal(word);

}

//System.out.println(sentimentVal(word));



s = nextSpace;
}

return sum;



} */




private static HashMap<String, Double> sentiment = new HashMap<String, Double>();
private static ArrayList<String> posAdjectives = new ArrayList<String>();
private static ArrayList<String> negAdjectives = new ArrayList<String>();


private static final String SPACE = " ";

static{
try {
Scanner input = new Scanner(new File("cleanSentiment.csv"));
while(input.hasNextLine()){
String[] temp = input.nextLine().split(",");
sentiment.put(temp[0],Double.parseDouble(temp[1]));
//System.out.println("added "+ temp[0]+", "+temp[1]);
}
input.close();
}
catch(Exception e){
System.out.println("Error reading or parsing cleanSentiment.csv");
}


//read in the positive adjectives in postiveAdjectives.txt
try {
Scanner input = new Scanner(new File("positiveAdjectives.txt"));
while(input.hasNextLine()){
String temp = input.nextLine().trim();
System.out.println(temp);
posAdjectives.add(temp);
}
input.close();
}
catch(Exception e){
System.out.println("Error reading or parsing postitiveAdjectives.txt\n" + e);
}

//read in the negative adjectives in negativeAdjectives.txt
try {
Scanner input = new Scanner(new File("negativeAdjectives.txt"));
while(input.hasNextLine()){
negAdjectives.add(input.nextLine().trim());
}
input.close();
}
catch(Exception e){
System.out.println("Error reading or parsing negativeAdjectives.txt");
}
}

/**
* returns a string containing all of the text in fileName (including punctuation),
* with words separated by a single space
*/
public static String textToString( String fileName )
{
String temp = "";
try {
Scanner input = new Scanner(new File(fileName));

//add 'words' in the file to the string, separated by a single space
while(input.hasNext()){
temp = temp + input.next() + " ";
}
input.close();

}
catch(Exception e){
System.out.println("Unable to locate " + fileName);
}
//make sure to remove any additional space that may have been added at the end of the
string.
return temp.trim();
}

/**
* @returns the sentiment value of word as a number between -1 (very negative) to 1 (very
positive sentiment)
*/
public static double sentimentVal( String word )
{
try
{
return sentiment.get(word.toLowerCase());
}
catch(Exception e)
{
return 0;
}
}

/**
* Returns the ending punctuation of a string, or the empty string if there is none
*/
public static String getPunctuation( String word )
{
String punc = "";
for(int i=word.length()-1; i >= 0; i--){
if(!Character.isLetterOrDigit(word.charAt(i))){
punc = punc + word.charAt(i);
} else {
return punc;
}
}
return punc;
}

/**
* Returns the word after removing any beginning or ending punctuation
*/
public static String removePunctuation( String word )
{
while(word.length() > 0 && !Character.isAlphabetic(word.charAt(0)))
{
word = word.substring(1);
}
while(word.length() > 0 && !Character.isAlphabetic(word.charAt(word.length()-1)))
{
word = word.substring(0, word.length()-1);
}

return word;
}

/**
* Randomly picks a positive adjective from the positiveAdjectives.txt file and returns it.
*/
public static String randomPositiveAdj()
{
int index = (int)(Math.random() * posAdjectives.size());
return posAdjectives.get(index);
}

/**
* Randomly picks a negative adjective from the negativeAdjectives.txt file and returns it.
*/
public static String randomNegativeAdj()
{
int index = (int)(Math.random() * negAdjectives.size());
return negAdjectives.get(index);

}

/**
* Randomly picks a positive or negative adjective and returns it.
*/
public static String randomAdjective()
{
boolean positive = Math.random() < .5;
if(positive){
return randomPositiveAdj();
} else {
return randomNegativeAdj();
}
}




/** Activity 2 starRating method
Write the starRating method here which returns the number of stars for the review based on
enter code here its totalSentiment.
* @param fileName
*/



public static int starRating(String filename){

// determine number of stars between 0 and 4 based on totalSentiment value

double totalSentiment = totalSentiment("simpleReview.txt");

// write if statements here
if (totalSentiment < 15 && totalSentiment >= 10) {
return 4;
} else if(totalSentiment < 10 && totalSentiment >= 5) {
return 3;
} else if(totalSentiment < 5 && totalSentiment >= 0) {
return 2;
} else if(totalSentiment < 0) {
return 1;
} else {
return 0;
}


}

public static double totalSentiment(String simpleReview) {
String review = textToString("simpleReview.txt");
int s = review.indexOf(" ");
double sum = 0;

//take first word from whole string
//review.substring(0 --> " ")

String firstWord = removePunctuation(review.substring(0, review.indexOf(" ")));
sum += sentimentVal(firstWord);

while(s != -1) {
int nextSpace = review.indexOf(" ", s + 1);
String word;

if (nextSpace != -1){
word = removePunctuation(review.substring(s + 1, nextSpace));
sum += sentimentVal(word);


} else {
word = removePunctuation(review.substring(s + 1));
sum += sentimentVal(word);

}

s = nextSpace;
}

return sum;


}

}

当我运行代码时,它将打印我的积极形容词列表。我什至没有运行该代码,所以我很困惑为什么会发生这种情况。

谁能帮忙:)

我目前正在从事一个 CS 项目,该项目最终会对 .txt 文件进行审查,然后从随机正面或随机负面形容词中随机选择一个形容词。这在代码中显示,但没有显示用于审核的 .txt 文件

最佳答案

您的 static 初始值设定项 block 中有一个 System.out.println(temp); 语句。

该语句似乎打印了 positiveAdjectives.txt 文件中的行。

static block 在您的 Review 类初始化时执行,这发生在您的 main 方法执行之前。

关于java - 为什么我没有打印却打印出这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59351718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com