2016-04-17 10 views
0

OK danke, jetzt kann der Code das Wort im String-Array finden, aber jetzt muss ich das positivste Wort von der Benutzereingabe bekommen und das negativste Wort von der Benutzereingabe. Also, wenn ich eine schreckliche Zone einschalte, ist Schrecklich 1,25 und Zone ist 2,66, so schrecklich ist das negativste Wort und die Zone ist das positivste Wort, aber wie der Code aufgebaut ist, weiß ich nicht, wie ich diese Werte dann verfolgen soll Stellen Sie sicher, dass es das richtige Wort als das positivste und das Wort als das Postnegativ ausdrucken kann. So wird der Durchschnitt der Benutzereingabe gedruckt. Ich habe versucht, parallele Arrays zu machen, aber ich möchte diese vermeiden. Die anderen Probleme sind in der Lage, mehrere Eingaben vom Benutzer zu nehmen, bis sie die Tasten ctrl z oder ctrl d verwenden. (Mir wurde gesagt, dass sie ein Teil von Eclipse sind, aber ich habe keine Ahnung, wie man sie benutzt.) Vielen Dank für Vorschläge.Versuchen, die Max und Min Werte aus einer Benutzereingabe zu nehmen und sie in Java Eclipse anzuzeigen

Ausgang:

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; 
    } 
+0

Verwendung einer zweidimensionalen Matrix. Lassen Sie die erste Spalte des Array das Wort halten und die zweite Spalte des Arrays das Wort Zahlenwert. – DevilsHnd

Antwort

0

Sie können versuchen, HashMap für diesen Einsatz:

Map<String,Integer> h = new HashMap<String,Integer>(); 
h.put(word,word_score); 
//get word score 
int score = h.get(word) 
Verwandte Themen