com.lbarriosh.sentimentanalyzer.main.Main.java Source code

Java tutorial

Introduction

Here is the source code for com.lbarriosh.sentimentanalyzer.main.Main.java

Source

/*******************************************************************************
 * Copyright (c) 2014 Luis Barrios Hernndez, Adrin Fernndez Hernndez
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Luis Barrios Hernndez, Adrin Fernndez Hernndez - initial API and implementation
 *******************************************************************************/
package com.lbarriosh.sentimentanalyzer.main;

import java.util.Arrays;
import java.util.List;

import twitter4j.Status;

import com.lbarriosh.sentimentanalyzer.SentimentAnalyzer;
import com.lbarriosh.sentimentanalyzer.utils.DocumentClassifier;
import com.lbarriosh.sentimentanalyzer.utils.TwitterLocale;

public class Main {

    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: sentimentanalyzer.jar <locale (EN|ES)> <query word list>");
            System.exit(-1);
        }
        printLogo();
        SentimentAnalyzer valorator = new SentimentAnalyzer();
        TwitterLocale locale;
        if (args[0].equalsIgnoreCase("EN")) {
            locale = TwitterLocale.EN;
        } else {
            locale = TwitterLocale.ES;
        }
        DocumentClassifier<Status> doccl = valorator.generateStatistics(args, 30, locale, 0.01);
        System.out.println("Here are the results for the query :" + Arrays.toString(args));
        System.out.println("Positive reviews: " + doccl.countPositiveDocuments());
        System.out.println("Negative reviews: " + doccl.countNegativeDocuments());
        System.out.println("Neutral reviews: " + doccl.countNeutralDocuments());
        System.out.println("Most tweets happen to be " + classifyResult(doccl.countPositiveDocuments(),
                doccl.countNegativeDocuments(), doccl.countNeutralDocuments()));
        System.out.println("Here's what people are saying:");
        System.out.println("### Positive tweets:\n");
        printTweets(doccl.getPositiveDocuments());
        System.out.println("### Negative tweets:\n");
        printTweets(doccl.getNegativeDocuments());
        System.out.println("### Neutral tweets:\n");
        printTweets(doccl.getNeutralDocuments());
    }

    private static void printLogo() {
        printHorizontalRule();
        System.out.println("   _____            _   _                      _   ");
        System.out.println("  / ____|          | | (_)                    | |  ");
        System.out.println(" | (___   ___ _ __ | |_ _ _ __ ___   ___ _ __ | |_ ");
        System.out.println("  \\___ \\ / _ \\ '_ \\| __| | '_ ` _ \\ / _ \\ '_ \\| __|");
        System.out.println("  ____) |  __/ | | | |_| | | | | | |  __/ | | | |_ ");
        System.out.println(" |_____/ \\___|_| |_|\\__|_|_| |_| |_|\\___|_| |_|\\__|");
        System.out.println("      /\\               | |                         ");
        System.out.println("     /  \\   _ __   __ _| |_   _ _______ _ __       ");
        System.out.println("    / /\\ \\ | '_ \\ / _` | | | | |_  / _ \\ '__|      ");
        System.out.println("   / ____ \\| | | | (_| | | |_| |/ /  __/ |         ");
        System.out.println("  /_/    \\_\\_| |_|\\__,_|_|\\__, /___\\___|_|         ");
        System.out.println("                           __/ |                   ");
        System.out.println("                          |___/                    ");
        System.out.println("Version 3.0");
        System.out.println("(C) 2013 Luis Barrios Hernndez, Adrin Fernndez Hernndez");
        System.out.println("(C) 2014 Luis Barrios Hernndez");
        printHorizontalRule();
    }

    private static void printTweets(List<Status> tweets) {
        for (Status tweet : tweets) {
            System.out.println("@" + tweet.getUser().getName() + ": " + tweet.getText());
        }
    }

    private static void printHorizontalRule() {
        for (int i = 0; i < 80; i++) {
            System.out.print("*");
        }
        System.out.println();
    }

    private static String classifyResult(int positive, int negative, int neutral) {
        if (positive >= negative) {
            if (positive >= neutral)
                return "POSITIVE";
            else
                return "NEUTRAL";
        }
        if (positive < negative) {
            if (positive >= neutral)
                return "NEGATIVE";
            else {
                if (negative < neutral)
                    return "NEUTRAL";
            }
        }
        return "NEGATIVE";
    }
}