This site requires JavaScript, please enable it in your browser!
Greenfoot back
rockon411
rockon411 wrote ...

2017/4/11

Record Word and Review

rockon411 rockon411

2017/4/11

#
I am working on a sentiment analyzer program. In particular, I am supposed to create a method that takes a number as a parameter. It then is supposed to record that the word was associated with a particular number once. I'm supposed to call the method every time the word is seen in my file. It is the recordOccurence(int score) method. This is what I have in my code currently.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sofia.micro.*;
import java.util.Scanner;
import student.IOHelper;
 
 
public class WordSentiment extends Actor
{
    private static String [] words = new String[3];
    private int count;
    private Scanner inputMovie;
    private int reviewScore;  
    private String reviewText;
    private int occur;
    private int score;
    private int total;
    public WordSentiment()
    {
        words = new String[3];
        count = 0;
        inputMovie = IOHelper.createScanner("movieReviews.text");
        reviewScore = 0;
        reviewText = new String();
        occur = 0;
        score = 0;
    }
    public int getCount()
    {
        int size = words.length;
        for (int i = 0; i < size; i++)
        {
            int position = i;
            int count = 0;
            for (int j = 0; j < size; j++)
            {
                String element = words[i];
                if (words[i].equals(element))
                {
                    count++;
                }
            }
            return count;
        }
        return count;
    }
    public int getSumOfReviewScores()
    {
        total = 0;
        while (inputMovie.hasNextLine())
        {
            String word = inputMovie.nextLine();
            while (inputMovie.hasNext())
            {
                reviewScore = inputMovie.nextInt();
                reviewText = inputMovie.nextLine();
                if (reviewText.contains(word))
                {
                    total = total + reviewScore;
                }
            }
            return total;
        }
        return total;
    }
    public double getSentimentScore()
    {
        return getSumOfReviewScores()/getCount();
    }
    public void recordOccurrence(int score)
    {
        occur++;
    }
}
I was told that the methods are supposed to be very simple, so I am also worried that I have made everything too complicated. Any help would be appreciate.
danpost danpost

2017/4/11

#
It is difficult to tell exactly what you have your code doing. There is no act method and no codes related to the creation of a WordSentiment object and not code that works with any such object outside this class.
rockon411 rockon411

2017/4/12

#
Ok so what I am given is a text file full of statements. At the beginning of each line is a number related to the statements. The getCount() method is supposed to go through the file and return the number of times a word is found. The getSumOfReviewScores() is supposed to return the total of all numbers related to the word that we are looking for and getSentimentScore() is supposed to return the average of the values. I have not implemented them yet kind of because I don't really know how and am pushing that off...
danpost danpost

2017/4/12

#
The 'getCount' method might as well just return the value of 'words.length'. You set element to a word (line 35) and then check that the word equals the element (line 36), which of course will always be true. Therefore 'count' will always end up to be 'size', which was assigned the value of 'words.length'.
rockon411 rockon411

2017/4/14

#
Thank you! I don't really know how to create a getCount() method (obviously), any hints would be appreciated!
danpost danpost

2017/4/14

#
rockon411 wrote...
I don't really know how to create a getCount() method (obviously), any hints would be appreciated!
Currently, I do not see the 'getCount' method trying to work with any file. I would think your inner 'for' loop would be dealing with the file.
rockon411 rockon411

2017/4/16

#
1
2
3
4
5
6
7
8
9
public int getCount() {
int count = 0;
while (inputMovie.hasNextLine()) {
    String nextToken = inputMovie.next();
    if (nextToken.equalsIgnoreCase(word))
    count++;
}
return count;
}
Something like this?
danpost danpost

2017/4/16

#
rockon411 wrote...
< Code Omitted > Something like this?
That looks much better (in general, as far as what the code should look like).
rockon411 rockon411

2017/4/17

#
Thanks! For the occurrence method how would I go about recording the occurrence of the word with a specific related number?
danpost danpost

2017/4/17

#
rockon411 wrote...
For the occurrence method how would I go about recording the occurrence of the word with a specific related number?
You only need to multiply the value returned by 'getCount()' by the number associated with the String 'word'.
rockon411 rockon411

2017/4/20

#
Oh thank you!
You need to login to post a reply.