Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This method gives away the sentiment of the input lyrics.
public static int classifyNewText(DoccatModel sentimentModel, String input)
throws IOException {
DocumentCategorizerME myCategorizer = new DocumentCategorizerME(
sentimentModel);
double[] outcomes = myCategorizer.categorize(input);
return Integer.parseInt(myCategorizer.getBestCategory(outcomes));
}
String[] inputFilePathArray = new String[4];
inputFilePathArray[0] = "C:\\input1.txt";
inputFilePathArray[1] = "C:\\input2.txt";
inputFilePathArray[2] = "C:\\input3.txt";
inputFilePathArray[3] = "C:\\input4.txt";
InputStream dataIn = new FileInputStream("C:\\tweets.txt");
ObjectStream<String> lineStream = new PlainTextByLineStream(dataIn,
"UTF-8");
ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(
lineStream);
DoccatModel sentimentModel = DocumentCategorizerME.train("en",
sampleStream);
int positiveCounts = 0, negativeCounts = 0;
for (String inputFilePath : inputFilePathArray) {
BufferedReader br = new BufferedReader(
new FileReader(inputFilePath));
String line = null, mapKey = null;
while ((line = br.readLine()) != null) {
peopleNamesList.addAll(getPeople(line));
if (classifyNewText(sentimentModel, line) == 1)
positiveCounts++;
if (classifyNewText(sentimentModel, line) == 0)
negativeCounts++;
br.close();
}
System.out.println("***POSITIVE SENTIMENT COUNT***");
System.out.print(positiveCounts);
System.out.println("***NEGATIVE SENTIMENT COUNT***");
System.out.print(negativeCounts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.