Java tutorial
/******************************************************************************* * 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; import java.util.List; import java.util.StringTokenizer; import twitter4j.Status; import com.lbarriosh.sentimentanalyzer.downloads.TweetsDownloader; import com.lbarriosh.sentimentanalyzer.lexicons.LemmaValoration; import com.lbarriosh.sentimentanalyzer.lexicons.Lexicon; import com.lbarriosh.sentimentanalyzer.lexicons.LexiconFactory; import com.lbarriosh.sentimentanalyzer.stopwords.StopWordsDetectorFactory; import com.lbarriosh.sentimentanalyzer.stopwords.TwitterStopWordsDetector; import com.lbarriosh.sentimentanalyzer.utils.DocumentClassifier; import com.lbarriosh.sentimentanalyzer.utils.TwitterLocale; public class SentimentAnalyzer { public static final String API_KEY = "***"; public static final String API_SECRET = "***"; public static final String ACCESS_TOKEN = "***"; public static final String ACCESS_TOKEN_SECRET = "***"; public DocumentClassifier<Status> generateStatistics(String[] query, int maxResults, TwitterLocale locale, double neutralEpsilon) { try { TweetsDownloader downloader = new TweetsDownloader(API_KEY, API_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET); List<Status> retrievedTweets = downloader.downloadTweets(query, locale, maxResults); TwitterStopWordsDetector sw_detector = StopWordsDetectorFactory.buildStopWordsDetector(locale); sw_detector.loadStopWordsFile(); Lexicon lexicon = LexiconFactory.buildLexicon(locale); lexicon.loadData(); DocumentClassifier<Status> docClassifier = new DocumentClassifier<Status>(neutralEpsilon); docClassifier.resetAllCounters(); for (Status tweet : retrievedTweets) { StringTokenizer tok = new StringTokenizer(tweet.getText(), " "); docClassifier.resetWeightCounters(); while (tok.hasMoreTokens()) { String preProcessedWord = sw_detector.processToken(tok.nextToken()); if (preProcessedWord != null) { LemmaValoration valoration = lexicon.getWordValoration(preProcessedWord); docClassifier.processWordValoration(valoration); } // if } // while docClassifier.classifyDocument(tweet); } // for return docClassifier; } catch (Exception e) { e.printStackTrace(); return null; } } }