Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSentimentDictionary.java
More file actions
Latest commit
108 lines (94 loc) · 3.4 KB
/
Copy pathSentimentDictionary.java
File metadata and controls
108 lines (94 loc) · 3.4 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Scanner;
/**
* This class is a singleton that manages access to the sentiments dictionary.
*
* The sentiments file is part of the Twitter Trends project by Aditi Muralidharan,
* John DeNero, and Hamilton Nguyen and featured on Stanford’s Nifty Lab's site.
*
* <http://nifty.stanford.edu/2013/denero-muralidharan-trends/>
*
* @author gcschmit
* @version 08jul2016
*/
publicclassSentimentDictionary
{
privatestaticSentimentDictionarysingleton;
privatestaticStringSENTIMENTS_FILENAME = "sentiments.csv";
privateMap<String, Double> dictionary;
/**
* Default Constructor for objects of class SentimentDictionary.
*
* This is private to ensure that the getSingleton method is used by clients.
*/
privateSentimentDictionary()
{
}
/**
* Constructor for objects of class SentimentDictionary.
*
* This is private to ensure that the getSingleton method is used by clients.
*
* @param fileName name of the sentiments file from which to build this dictionary
*
* @precondition the specified file must be a CSV file with two columns.
* The first contains the word; the second, a sentiment score.
* The sentiment will range from -1 to 1, where negative values correspond
* to negative sentiment; positive values, positive.
*/
privateSentimentDictionary( StringfileName )
{
dictionary = newHashMap<String, Double>();
try
{
FilesentimentFile = newFile( fileName );
Scannerin = newScanner( sentimentFile );
in.useDelimiter( "[,\r\n]" );
while( in.hasNext())
{
Stringword = in.next();
Doublesentiment = in.nextDouble();
dictionary.put( word, sentiment );
in.nextLine();
}
in.close();
}
catch( FileNotFoundExceptione )
{
System.out.println( "Sentiment file: " + fileName + " not found." );
}
}
/**
* Returns the singleton instance of the SentimentDictionary.
* Clients should invoke this method to obtain a refernece to a SentimentDictionary object.
*
* @return the singleton instance of the SentimentDictionary
*
* @precondition callers are responsible for invoking this method in a thread-safe manner
*/
publicstaticSentimentDictionarygetSingleton()
{
// not thread safe
if( singleton == null )
{
singleton = newSentimentDictionary( SENTIMENTS_FILENAME );
}
returnsingleton;
}
/**
* Returns the sentiment associated with the specified word.
* The sentiment will range from -1 to 1, where negative values correspond
* to negative sentiment; positive values, positive. If there is no sentiment for
* the specified word, a null value will be returned.
*
* @param word the word whose sentiment will be returned, the word must be all lowercase
* @return the sentiment associated with the specified word or null if none
*/
publicDoublegetSentiment( Stringword )
{
returndictionary.get( word );
}
}