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.tools.twitter.modes.preprocessing; 031 032import java.io.IOException; 033import java.util.Map; 034 035import org.openimaj.io.FileUtils; 036import org.openimaj.text.nlp.language.LanguageDetector; 037import org.openimaj.twitter.GeneralJSON; 038import org.openimaj.twitter.GeneralJSONRDF; 039import org.openimaj.twitter.RDFAnalysisProvider; 040import org.openimaj.twitter.USMFStatus; 041 042import com.hp.hpl.jena.query.ParameterizedSparqlString; 043import com.hp.hpl.jena.rdf.model.Model; 044import com.hp.hpl.jena.rdf.model.Resource; 045import com.hp.hpl.jena.update.UpdateAction; 046 047/** 048 * A gateway class which loads and uses the #LanguageDetector 049 * 050 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 051 * 052 */ 053public class LanguageDetectionMode extends TwitterPreprocessingMode<Map<String, Object>> { 054 055 private LanguageDetector detector; 056 final static String LANGUAGES = "langid"; 057 058 /** 059 * Loads the language detector 060 * 061 * @throws IOException 062 */ 063 public LanguageDetectionMode() throws IOException { 064 detector = new LanguageDetector(); 065 } 066 067 @Override 068 public Map<String, Object> process(USMFStatus twitterStatus) { 069 Map<String, Object> language = null; 070 try { 071 language = detector.classify(twitterStatus.text).asMap(); 072 073 } catch (final Exception e) { 074 } 075 twitterStatus.addAnalysis(LANGUAGES, language); 076 return language; 077 078 } 079 080 @Override 081 public RDFAnalysisProvider rdfAnalysisProvider() { 082 return new RDFAnalysisProvider() { 083 private static final String DETECTED_LANGUAGE_INSERT_SPARQL = "/org/openimaj/tools/twiiter/rdf/detected_language_insert.sparql"; 084 private String query; 085 086 @Override 087 public void addAnalysis(Model m, Resource socialEvent, GeneralJSON analysisSource) { 088 final Map<String, Object> analysis = analysisSource.getAnalysis(LANGUAGES); 089 if (analysis == null) 090 return; 091 092 final ParameterizedSparqlString pss = new ParameterizedSparqlString(query); // wasteful? 093 // makes 094 // it 095 // threadsafe 096 // but 097 // is 098 // it 099 // bad? 100 pss.setParam("socialEvent", socialEvent); 101 final Resource langNode = m.createResource(); 102 pss.setParam("langid", langNode); 103 pss.setLiteral("language", analysis.get("language").toString()); 104 pss.setLiteral("confidence", (Double) analysis.get("confidence")); 105 UpdateAction.execute(pss.asUpdate(), m); 106 } 107 108 @Override 109 public void init() { 110 try { 111 query = FileUtils.readall(GeneralJSONRDF.class.getResourceAsStream(DETECTED_LANGUAGE_INSERT_SPARQL)); 112 } catch (final IOException e) { 113 throw new RuntimeException(e); 114 } 115 116 } 117 }; 118 } 119 120 @Override 121 public String getAnalysisKey() { 122 return LANGUAGES; 123 } 124}