Question
I am trying to estimate a movies rating based on this: Here are the functions that I have so far that are used. /** *
I am trying to estimate a movies rating based on this:
Here are the functions that I have so far that are used.
/** * Looks for word in the words array in the first numberOfSpacesUsedInWords elements. * Do not look at the entire length of the array - many spots are not used * until the end of the word counting process. * * @param words: An array of String values. The first numberOfSpacesUsedInWords are filled. * @param word: The search word. * @param numberOfSpacesUsedInWords: the number of elements currently used in the words array. * @return the index of the search word in words, or -1 if not found. */
public static int indexOfWordInArray(String[] words, String word, int numberOfSpacesUsedInWords) {
for (int i = 0; i
if (words[i].equals(word)) {
return i;
}
}
return -1; // Implement this method
}
/** * For a review sentence, estimate the movie rating based on the words in the * review. For each word, find its index in the words list, then compute its * average score (scores[index]/wordCount[index]) and add it to a cumulative * review score. Count up the number of words in the review and use the count * and the cumulative review score to get an averaged movie score. * * Assume that at least one word in the review is in the array of words. * * @param review: The text of the review. * @param words: The array of words found in all reviews. * @param scores: The cumulative score for each word in words. * @param wordCount: The number of times each word in words appears in all the * reviews. * @param numberOfSpacesUsedInWords: The number of elements in the arrays to be used. * @return the average score for the words in review. */
public static double scoreReview(String review, String[] words, double[] scores, int[] wordCount,
int numberOfSpacesUsedInWords) {
double cumScore = 0;
for (int i = 0; i
if (review.contains(words[i])) {
cumScore +=(scores[i] / wordCount[i]);
}
}
return cumScore / words.length;
}
/** * Search through numberOfSpacesUsedInWords elements of the scores array. Following an * optimization loop pattern, find the index of the highest average scoring * word. With this index the actual word can be found later. Ignore words whose * counts are not greater than the countAbove value. * * @param scores: An array of cumulative scores for a word. * @param counts: An array of times the word appeared in the reviews. * @param countAbove: Words with counts below or equal to countAbove are * ignored. * @param numberOfSpacesUsedInWords: Specifies the number of valid elements in the * arrays. * @return the index of the best average score or -1 if none satisfy the * countAbove threshold. */
public static int indexOfBestWord(double[] scores, int[] counts, int countAbove, int numberOfSpacesUsedInWords) {
int maxIndex = -1;
double max = 0;
for (int i = 0; i
if (counts[i] > countAbove && scores[i] > max) {
max = scores[i];
maxIndex = i;
}
}
return maxIndex;
}
/** * Process the words in the scanner s. If a token in s is already in words, then * add the lineScore to the word_score location for that word and add 1 to the * word_count for that location. If the token is not is words, then add the * token to the next available spot in words and add the lineScore to word_score * at that location and put a count of 1 in word_count at that location. Adjust * numberOfSpacesUsedInWords by adding 1 when a new spot is used up. Do not change * this if the word is already in the array. * * @param s: A Scanner with the text part of a movie review * @param lineScore: the integer movie rating taken from the review * @param words: an array to hold the words from the reviews * @param wordScore: an array holding the cumulative (summed) score for that word. * @param wordCount: an array holding the number of times a word has been seen * in reviews * @param numberOfSpacesUsedInWords: the number of elements used in the arrays * @return the new numberOfSpacesUsedInWords. If no new words are found in s, then it * is the same value as the input numberOfWordSoFar. */
public static int processWords(Scanner s, int lineScore, String[] words, double[] wordScore, int[] wordCount,
int numberOfSpacesUsedInWords) {
while (s.hasNext()) {
String word = s.next();
int index = -1;
for (int i = 0; i
if (words[i].equals(word)) {
index = i;
break;
}
}
if (index == -1 && numberOfSpacesUsedInWords
words[numberOfSpacesUsedInWords] = word;
wordCount[numberOfSpacesUsedInWords] = 1;
wordScore[numberOfSpacesUsedInWords] = lineScore;
numberOfSpacesUsedInWords++;
} else if (index != -1) {
wordCount[index] += 1;
wordScore[index] += lineScore;
}
}
return numberOfSpacesUsedInWords;
}
How To Estimate a Movie Rating? Use the review file to get an average score for all the words it knows Test with a known review we will use an existing review so we know all the words are already scored Go through each word in the review count up the score from each word Divide by the number of words to get an average compare with the actual review rating How To Estimate a Movie Rating? Use the review file to get an average score for all the words it knows Test with a known review we will use an existing review so we know all the words are already scored Go through each word in the review count up the score from each word Divide by the number of words to get an average compare with the actual review ratingStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started