0
好的,謝謝,現在代碼可以在字符串數組中找到該單詞,但現在我需要從用戶輸入中獲取最正面的單詞和來自用戶輸入的最負面的詞。所以,如果我插入可怕的區域,可怕的是1.25和區域是2.66,那麼可怕的是最消極的詞,區是最積極的詞,但是如何設置代碼,我不知道如何跟蹤這些值,然後確保它可以打印出正確的單詞作爲最正面的單詞並將該單詞作爲後負片打印出來。所以打印用戶輸入的平均值。我嘗試過並行陣列,但我想避免這些。其他問題是能夠從用戶接受多個輸入,直到他們使用按鍵ctrl z或ctrl d。 (我被告知他們是Eclipse的一部分,但我不知道如何使用它們。) 謝謝你有關如何繼續的任何建議。嘗試從用戶輸入中取出最大值和最小值並在Java中顯示它們
輸出:
Please type one line of review and when you are done press either Ctr D or Ctr Z
dreadful zone
dreadful zone
The average is negative at 1.9583333333333333
Incomplete assignment
public class MovieReviewSentimentAnalysis {
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
// TODO: complete me
//make own arrays to pass by value
//movieReviewComments = the text
String[] movieReviewComments = new String[8529];
//movieReviewScores = numeric values, avoid lit. values
int[] movieReviewScores = new int[8529];
String userComment = "";
MovieReviewReader.readMovieReviews("movie_reviews.txt", movieReviewComments, movieReviewScores); //string, string array, and int array
System.out.println("Please type one line of review and when you are done press either Ctr D or Ctr Z");
userComment = userInput.nextLine();
System.out.println(userComment);
// String[] words = userComment.split("\\s+");
String[] words2 = userComment.split("[\\W]"); //splits at "\W", or non-word characters
double singularUserWordTotal = 0;
double wordTotal = 0;
double totalSumOfUserCommentWords = 0;
double highestWordScore = 20;
double lowestWordScoreTotal = 0;
int locationOfWordInUserInput = 0;
String userInputWord = "";
// int itemCount = words.length;
for (int i = 0; i < words2.length; i++)
{
userInputWord = words2[i];
singularUserWordTotal = wordCount(userInputWord, movieReviewComments, movieReviewScores);
wordTotal += singularUserWordTotal;
totalSumOfUserCommentWords = wordTotal/words2.length;
// locationOfWordInUserInput = i;
// if(singularUserWordTotal > highestWordScore)
// {
// singularUserWordTotal = highestWordScore;
// }
// if(singularUserWordTotal < highestWordScore)
// {
// singularUserWordTotal = highestWordScore;
// lowestWordScoreTotal = singularUserWordTotal;
// }
}
displayScores(totalSumOfUserCommentWords);
// System.out.println(reviewFile);
System.out.println("Incomplete assignment");
userInput.close();
}
public static double wordCount(String userInputWord, String[] movieReviewComments, int[] movieReviewScores)
{
double storeScore = 0;
double totalSumOfReviewScores = 0;
double numOfTimesWordAppears = 0;
for (int i=0; i < (movieReviewComments.length); i++)
{
if (movieReviewComments[i].contains(userInputWord))
//PUNCTUATION IS A PROBLEM (if it's at the end of the user input then it's fine though)
{
storeScore = movieReviewScores[i];
totalSumOfReviewScores += storeScore;
numOfTimesWordAppears++;
//System.out.println("Found");
//What if the word doesn't appear in the text file?????
}else if (!movieReviewComments[i].contains(userInputWord))
{
numOfTimesWordAppears += 0;
}
// else
// System.out.println("You dun goofed"); //delete after fixing problem
}
double wordScoreAverage = totalSumOfReviewScores/numOfTimesWordAppears;
return wordScoreAverage;
}
public static double displayScores(double userCommentTotal)
{
if(userCommentTotal > 2.01)
{
System.out.println("The average is positive at " + userCommentTotal);
}else if(userCommentTotal < 2.01 && userCommentTotal > 1.99)
{
System.out.println("The average is neutral at " + userCommentTotal);
}else
System.out.println("The average is negative at " + userCommentTotal);
return userCommentTotal;
}
使用二維數組。允許Array的第一列包含Word,而Array的第二列包含單詞數值。 – DevilsHnd