/** * Core sentiment detection method. doNota = none of the above * * @param captions (null/none = all) */ public Map<String, Collection<Document>> getEmotions( Indexer indexer, Collection<Document> docs, boolean doNota, boolean originalContentOnly, String... captions) { Collection<Lexicon1Lang> lexicons = getRelevantLexicon1Langs(docs); Map<String, Collection<Document>> result = new LinkedHashMap<>(); Set<Document> docs_set = Util.castOrCloneAsSet(docs); // aggregate results for each lang into result for (Lexicon1Lang lex : lexicons) { Map<String, Collection<Document>> resultsForThisLang = (doNota ? lex.getEmotionsWithNOTA(indexer, docs_set, originalContentOnly) : lex.getEmotions(indexer, docs_set, originalContentOnly, captions)); if (resultsForThisLang == null) continue; for (String caption : resultsForThisLang.keySet()) { Collection<Document> resultDocsThisLang = resultsForThisLang.get(caption); Collection<Document> resultDocs = result.get(caption); // if caption doesn't exist already, create a new entry, or else add to the existing set of // docs that match this caption if (resultDocs == null) result.put(caption, resultDocsThisLang); else resultDocs.addAll(resultDocsThisLang); } } // TODO: the result can be cached at server to avoid redundant computation (by concurrent users, // which are few for now) return result; }
/** * updates the map for a given language * * @throws IOException * @throws FileNotFoundException */ public boolean update(String language, Map<String, String> map) throws IOException { language = language.toLowerCase(); Lexicon1Lang langLex = languageToLexicon.get(language); if (langLex == null) { langLex = new Lexicon1Lang(); languageToLexicon.put(language, langLex); } langLex.setRawQueryMap(map); return true; }
/** * returns whether it succeeded * * @throws Exception */ public boolean save(String dir, String language) throws Exception { language = language.toLowerCase(); Lexicon1Lang langLex = languageToLexicon.get(language); if (langLex == null) return false; langLex.save( dir + File.separator + name + "." + language + LEXICON_SUFFIX); // LEXICON_SUFFIX already has a . return true; }
// accumulates counts returned by lexicons in each language // TODO: It is possible to write a generic accumulator that accumulates sum over all the languages public Map<String, Integer> getLexiconCounts(Indexer indexer, boolean originalContentOnly) { List<Document> docs = indexer.docs; Collection<Lexicon1Lang> lexicons = getRelevantLexicon1Langs(docs); Map<String, Integer> result = new LinkedHashMap<String, Integer>(); Set<Document> docs_set = Util.castOrCloneAsSet(docs); // aggregate results for each lang into result for (Lexicon1Lang lex : lexicons) { Map<String, Integer> resultsForThisLang = lex.getLexiconCounts(indexer, originalContentOnly); if (resultsForThisLang == null) continue; for (String caption : resultsForThisLang.keySet()) { Integer resultCountsThisLang = resultsForThisLang.get(caption); Integer resultCounts = result.get(caption); // if caption doesn't exist already, create a new entry, or else add to the existing set of // docs that match this caption if (resultCounts == null) result.put(caption, resultCountsThisLang); else result.put(caption, resultCounts + resultCountsThisLang); } } return result; }