A. Hodges A. Hodges - 1 month ago 16
Java Question

How to use a Movie Review file to print out sentiment averages for words in another file?

I am working on a project that has many different parts and methods. For this one specific part I need to ask the user for the name of a file that contains a set of words, one per line. (see attached) Then I need to compute the average score/sentiment of the word by comparing it to a movieReview file that scores the sentiment of words. (see attached)

[Edit] : I have not gotten my code to take the first line of the wordList file, search through the movieReview file for the word, and find the average score of the word. And after the search is complete, move onto the next word. However, it is printing NaN for the rest of the words after the first, "mechanical"

Example: The first word in the wordList file is "mechanical". Mechanical is found in the movieReview file 6 times and the total score is 4. The average sentiment for the word "mechanical" is .666666666.

How can I make my code so the loop continues and finds the average for every single word and prints it out? Sorry if this sound confusing, let me know if I need to clarify. Also, I am a very beginner coder so please try to not use difficult concepts. (Also, it was said using an array or buffer wasn't needed)

Movie Review File :
http://nifty.stanford.edu/2016/manley-urness-movie-review-sentiment/movieReviews.txt

Content of Word List file(txt):
mechanical
car
soulless
style
family
wonderful
historical
nor
strong
slapstick
complicated
provoking
interest
cast
witty
muted
sentiment
narrative
refreshing
preachy
horrible
resolutely
terrible
dialogue
incoherent
spend
words
moving
devoid
indulgent
dull
value
barely
always
dog
tale
hardly
unfocused
formulaic
eccentric
quirky
unpredictable
tears
writing

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class methodThree {
public static void main (String [] args) throws FileNotFoundException {

Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the file with words you want to score: ");
String inputFileName = in.next();
File inputFile = new File(inputFileName);

while(!inputFile.exists())
{
System.out.println("Please enter a valid file name:");
inputFile = new File(in.next());
}

Scanner wordFile = new Scanner (inputFile);

File inputMovie = new File("movieReviews.txt");
Scanner movieReview = new Scanner (inputMovie);


String reviewText;
int reviewScore;

while (wordFile.hasNextLine())
{
int count = 0;
double total = 0;
String word = wordFile.nextLine();

while (movieReview.hasNext()) {
reviewScore = movieReview.nextInt();
reviewText = movieReview.nextLine();

if (reviewText.contains(word)) {
count++;
total = total + reviewScore;
}
}

double average = (total / count);
System.out.println (word + " " + average);
}
}
}

Answer

So what you basically want to do is repeat this code for every line in the wordfile?

int count = 0;
double total = 0;
String word = wordFile.nextLine();

while(movieReview.hasNext()){
    reviewScore = movieReview.nextInt();
    reviewText = movieReview.nextLine();

    if (reviewText.contains(word)) {
        count++;
        total = total + reviewScore;
    }
}
double average = (total / count);
System.out.println(average);

if thats the case you could surround it with another while loop. The loop must run for every line in wordFile, so it's more or less the same loop as your movieReview.hasNext() loop.

while(wordFile.hasNext()){
    int count = 0;
    ...
}

The loop runs as long as wordFile has another word to score.