def analyse(self, **params): lang = params.get("language", "auto") res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson", json.dumps({"language": lang, "data": [{"text": params["input"]}] } ) ) p = params.get("prefix", None) response = Response(prefix=p) polarity_value = self.maxPolarityValue*int(res.json()["data"][0] ["polarity"]) * 0.25 polarity = "marl:Neutral" neutral_value = self.maxPolarityValue / 2.0 if polarity_value > neutral_value: polarity = "marl:Positive" elif polarity_value < neutral_value: polarity = "marl:Negative" entry = Entry(id="Entry0", text=params["input"], prefix=p) opinion = Opinion(id="Opinion0", prefix=p, hasPolarity=polarity, polarityValue=polarity_value) opinion["prov:wasGeneratedBy"] = self.id entry.opinions.append(opinion) entry.language = lang response.entries.append(entry) return response
def analyse(self, **params): lang = params.get("language", "auto") res = requests.post( "http://www.sentiment140.com/api/bulkClassifyJson", json.dumps({ "language": lang, "data": [{ "text": params["input"] }] })) p = params.get("prefix", None) response = Results(prefix=p) polarity_value = self.maxPolarityValue * int( res.json()["data"][0]["polarity"]) * 0.25 polarity = "marl:Neutral" neutral_value = self.maxPolarityValue / 2.0 if polarity_value > neutral_value: polarity = "marl:Positive" elif polarity_value < neutral_value: polarity = "marl:Negative" entry = Entry(id="Entry0", nif__isString=params["input"]) sentiment = Sentiment(id="Sentiment0", prefix=p, marl__hasPolarity=polarity, marl__polarityValue=polarity_value) sentiment.prov__wasGeneratedBy = self.id entry.sentiments = [] entry.sentiments.append(sentiment) entry.language = lang response.entries.append(entry) return response
def analyse(self, **params): lang = params.get("language", "auto") res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson", json.dumps({"language": lang, "data": [{"text": params["input"]}] } ) ) p = params.get("prefix", None) response = Results(prefix=p) polarity_value = self.maxPolarityValue*int(res.json()["data"][0] ["polarity"]) * 0.25 polarity = "marl:Neutral" neutral_value = self.maxPolarityValue / 2.0 if polarity_value > neutral_value: polarity = "marl:Positive" elif polarity_value < neutral_value: polarity = "marl:Negative" entry = Entry(id="Entry0", nif__isString=params["input"]) sentiment = Sentiment(id="Sentiment0", prefix=p, marl__hasPolarity=polarity, marl__polarityValue=polarity_value) sentiment.prov__wasGeneratedBy = self.id entry.sentiments = [] entry.sentiments.append(sentiment) entry.language = lang response.entries.append(entry) return response
def analyse(self, **params): lang = params.get("language", "auto") res = requests.post( "http://www.sentiment140.com/api/bulkClassifyJson", json.dumps({ "language": lang, "data": [{ "text": params["input"] }] })) p = params.get("prefix", None) response = Response(prefix=p) polarity_value = self.maxPolarityValue * int( res.json()["data"][0]["polarity"]) * 0.25 polarity = "marl:Neutral" neutral_value = self.maxPolarityValue / 2.0 if polarity_value > neutral_value: polarity = "marl:Positive" elif polarity_value < neutral_value: polarity = "marl:Negative" entry = Entry(id="Entry0", text=params["input"], prefix=p) opinion = Opinion(id="Opinion0", prefix=p, hasPolarity=polarity, polarityValue=polarity_value) opinion["prov:wasGeneratedBy"] = self.id entry.opinions.append(opinion) entry.language = lang response.entries.append(entry) return response
def analyse(self, **params): logger.debug("Analysing with params {}".format(params)) text_input = params.get("input", None) text=self._my_preprocessor(text_input) dictionary={} lang = params.get("language", "auto") if lang == 'es': with open(self.local_path+self.anew_path_es,'rb') as tabfile: reader = csv.reader(tabfile, delimiter='\t') for row in reader: dictionary[row[2]]={} dictionary[row[2]]['V']=row[4] dictionary[row[2]]['A']=row[6] dictionary[row[2]]['D']=row[8] else: with open(self.local_path+self.anew_path_en,'rb') as tabfile: reader = csv.reader(tabfile, delimiter='\t') for row in reader: dictionary[row[0]]={} dictionary[row[0]]['V']=row[2] dictionary[row[0]]['A']=row[4] dictionary[row[0]]['D']=row[6] feature_set=self._extract_features(text,dictionary,lang) p = params.get("prefix", None) response = Response(prefix=p) entry = Entry(id="Entry", text=text_input, prefix=p) emotions = EmotionSet(id="Emotions0") emotion1 = Emotion(id="Emotion0") emotion1["onyx:hasEmotionCategory"] = self.emotions_ontology[feature_set['emotion']] emotion1["http://www.gsi.dit.upm.es/ontologies/onyx/vocabularies/anew/ns#valence"] = feature_set['V'] emotion1["http://www.gsi.dit.upm.es/ontologies/onyx/vocabularies/anew/ns#arousal"] = feature_set['A'] emotion1["http://www.gsi.dit.upm.es/ontologies/onyx/vocabularies/anew/ns#dominance"] = feature_set['D'] emotions.emotions.append(emotion1) entry.emotionSets.append(emotions) entry.language = lang response.entries.append(entry) return response
def analyse(self, **params): lang = params.get("language", "auto") response = Results() polarity_value = max(-1, min(1, random.gauss(0.2, 0.2))) polarity = "marl:Neutral" if polarity_value > 0: polarity = "marl:Positive" elif polarity_value < 0: polarity = "marl:Negative" entry = Entry({"id":":Entry0", "nif:isString": params["input"]}) sentiment = Sentiment({"id": ":Sentiment0", "marl:hasPolarity": polarity, "marl:polarityValue": polarity_value}) sentiment["prov:wasGeneratedBy"] = self.id entry.sentiments = [] entry.sentiments.append(sentiment) entry.language = lang response.entries.append(entry) return response
def analyse(self, **params): lang = params.get("language", "auto") response = Results() polarity_value = max(-1, min(1, random.gauss(0.2, 0.2))) polarity = "marl:Neutral" if polarity_value > 0: polarity = "marl:Positive" elif polarity_value < 0: polarity = "marl:Negative" entry = Entry({"id": ":Entry0", "nif:isString": params["input"]}) sentiment = Sentiment({ "id": ":Sentiment0", "marl:hasPolarity": polarity, "marl:polarityValue": polarity_value }) sentiment["prov:wasGeneratedBy"] = self.id entry.sentiments = [] entry.sentiments.append(sentiment) entry.language = lang response.entries.append(entry) return response
def analyse(self, **params): lang = params.get("language", "auto") p = params.get("prefix", None) response = Response(prefix=p) polarity_value = max(-1, min(1, random.gauss(0.2, 0.2))) polarity = "marl:Neutral" if polarity_value > 0: polarity = "marl:Positive" elif polarity_value < 0: polarity = "marl:Negative" entry = Entry(id="Entry0", text=params["input"], prefix=p) opinion = Opinion(id="Opinion0", prefix=p, hasPolarity=polarity, polarityValue=polarity_value) opinion["prov:wasGeneratedBy"] = self.id entry.opinions.append(opinion) entry.language = lang response.entries.append(entry) return response
def analyse(self, **params): logger.debug("Analysing with params {}".format(params)) text = params.get("input", None) tokens = self._tokenize(text) tokens = self._pos(tokens) for i in tokens: tokens[i]['lemmas'] = {} for w in tokens[i]['tokens']: lemmas = wn.lemmas(w[0], lang='spa') if len(lemmas) == 0: continue tokens[i]['lemmas'][w[0]] = lemmas logger.debug("Tokens: {}".format(tokens)) trans = TextBlob(unicode(text)).translate(from_lang='es',to='en') useful_synsets = {} for s_i, t_s in enumerate(trans.sentences): useful_synsets[s_i] = {} for w_i, t_w in enumerate(trans.sentences[s_i].words): synsets = wn.synsets(trans.sentences[s_i].words[w_i]) if len(synsets) == 0: continue eq_synset = self._compare_synsets(synsets, tokens, s_i) useful_synsets[s_i][t_w] = eq_synset logger.debug("Synsets used for analysis: {}".format(useful_synsets)) scores = {} for i in tokens: scores[i] = {} if useful_synsets is None: for word in useful_synsets[i]: if useful_synsets[i][word] is None: continue temp_scores = self._swn.get_score(useful_synsets[i][word].name().split('.')[0].replace(' ',' ')) for score in temp_scores: if score['synset'] == useful_synsets[i][word]: t_score = score['pos'] - score['neg'] f_score = 'neu' if t_score > 0: f_score = 'pos' elif t_score < 0: f_score = 'neg' score['score'] = f_score scores[i][word] = score break logger.debug("All scores (some not used): {}".format(scores)) lang = params.get("language", "auto") p = params.get("prefix", None) response = Results() for i in scores: n_pos = 0.0 n_neg = 0.0 for w in scores[i]: if scores[i][w]['score'] == 'pos': n_pos += 1.0 elif scores[i][w]['score'] == 'neg': n_neg += 1.0 inter = interp1d([-1.0, 1.0], [0.0, 1.0]) try: g_score = (n_pos - n_neg) / (n_pos + n_neg) g_score = float(inter(g_score)) except: if n_pos == 0 and n_neg == 0: g_score = 0.5 polarity = 'marl:Neutral' if g_score > 0.5: polarity = 'marl:Positive' elif g_score < 0.5: polarity = 'marl:Negative' entry = Entry(id="Entry"+str(i), nif_isString=tokens[i]['sentence']) opinion = Sentiment(id="Opinion0"+'_'+str(i), marl__hasPolarity=polarity, marL__polarityValue=float("{0:.2f}".format(g_score))) opinion["prov:wasGeneratedBy"] = self.id entry.sentiments = [] entry.sentiments.append(opinion) entry.language = lang response.entries.append(entry) return response