001/**
002 * Copyright (c) 2012, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.twitter.experiments.langid;
031
032import java.io.File;
033import java.io.IOException;
034import java.util.ArrayList;
035import java.util.HashMap;
036import java.util.Map.Entry;
037
038import org.openimaj.io.FileUtils;
039import org.openimaj.text.nlp.language.LanguageDetector;
040import org.openimaj.text.nlp.language.LanguageDetector.WeightedLocale;
041import org.openimaj.twitter.collection.MemoryTwitterStatusList;
042import org.openimaj.twitter.collection.TwitterStatusList;
043import org.openimaj.util.pair.IndependentPair;
044
045/**
046 * Perform an experiment, a set of tweets with a known language compared to the {@link LanguageDetector}'s response
047 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
048 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk), 
049 *
050 */
051public class TrueLanguageExperiment {
052        String[] inputFiles = new String[]{
053                "dev-out-r",
054                "trn-out-r",
055                "tst-out-r"
056        };
057        private ArrayList<TrueLanguageTwitterStatus> inputStatusLists;
058        private HashMap<String, IndependentPair<Float, Long>> scores;
059        
060        /**
061         * Load the tweets needed for this experiment
062         * @throws IOException
063         */
064        public void prepareExperiment() throws IOException{
065                this.inputStatusLists = new ArrayList<TrueLanguageTwitterStatus>();
066                
067                for (String input : this.inputFiles) {
068                        File inputFile = FileUtils.copyStreamToFile(TrueLanguageExperiment.class.getResourceAsStream(input), File.createTempFile(input, ".txt"));
069                        TwitterStatusList<TrueLanguageTwitterStatus> list = MemoryTwitterStatusList.read(inputFile,TrueLanguageTwitterStatus.class);
070                        this.inputStatusLists.addAll(list);
071                        inputFile.delete();
072                }
073        }
074        
075        /**
076         * do the experiment
077         * @throws IOException
078         */
079        public void doExperiment() throws IOException{
080                this.scores = new HashMap<String,IndependentPair<Float,Long>>();
081                IndependentPair<Float, Long> totalScore;
082                this.scores.put("total",  totalScore = IndependentPair.pair(0f, 0l));
083                LanguageDetector languageDetector = new LanguageDetector();
084                for (TrueLanguageTwitterStatus status : inputStatusLists) {
085                        WeightedLocale detected = languageDetector.classify(status.text);
086                        IndependentPair<Float, Long> currentScore = this.scores.get(detected.language);
087                        if(currentScore == null){
088                                currentScore = IndependentPair.pair(0.0f, 0l);
089                                this.scores.put(detected.language, currentScore);
090                        }
091                        if(detected.language.equals(status.lang_true)){
092                                currentScore.setFirstObject(currentScore.firstObject() + 1);
093                                totalScore.setFirstObject(totalScore.firstObject() + 1);
094                        }
095                        currentScore.setSecondObject(currentScore.secondObject() + 1);
096                        totalScore.setSecondObject(totalScore.secondObject() + 1);
097                }
098                for (Entry<String, IndependentPair<Float, Long>> entry : this.scores.entrySet()) {
099                        System.out.println(entry.getKey() + ":" + entry.getValue().firstObject() / entry.getValue().secondObject());
100                }
101                System.out.println("Total score:" + totalScore.firstObject() / totalScore.secondObject());
102        }
103        
104        /**
105         * @param args
106         * @throws IOException
107         */
108        public static void main(String[] args) throws IOException {
109                TrueLanguageExperiment exp = new TrueLanguageExperiment();
110                exp.prepareExperiment();
111                exp.doExperiment();
112        }
113}