mirror of
https://github.com/gsi-upm/senpy
synced 2025-09-18 04:22:21 +00:00
First tests
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
import sys
|
||||
|
||||
from senpy.plugins import SentimentPlugin
|
||||
from senpy.models import Response, Opinion, Entry
|
||||
|
||||
class Sentiment140Plugin(SentimentPlugin):
|
||||
EXTRA_PARAMS = {
|
||||
"language": {"aliases": ["language", "l"],
|
||||
"required": False,
|
||||
"options": ["es", "en", "auto"],
|
||||
}
|
||||
}
|
||||
def __init__(self, **kwargs):
|
||||
super(Sentiment140Plugin, self).__init__(name="sentiment140",
|
||||
version="2.0",
|
||||
extraparams=self.EXTRA_PARAMS,
|
||||
**kwargs)
|
||||
|
||||
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"]}]}
|
||||
))
|
||||
|
||||
|
||||
response = Response()
|
||||
polarityValue = int(res.json()["data"][0]["polarity"]) * 25
|
||||
polarity = "marl:Neutral"
|
||||
if polarityValue > 50:
|
||||
polarity = "marl:Positive"
|
||||
elif polarityValue < 50:
|
||||
polarity = "marl:Negative"
|
||||
|
||||
|
||||
entry = Entry(text=params["input"])
|
||||
opinion = Opinion(polarity=polarity, polarityValue=polarityValue)
|
||||
entry.opinions.append(opinion)
|
||||
entry.language = lang
|
||||
response.entries.append(entry)
|
||||
return response
|
||||
|
||||
plugin = Sentiment140Plugin()
|
@@ -1,41 +0,0 @@
|
||||
'''
|
||||
SENTIMENT140
|
||||
=============
|
||||
|
||||
* http://www.sentiment140.com/api/bulkClassifyJson
|
||||
* Method: POST
|
||||
* Parameters: JSON Object (that is copied to the result)
|
||||
* text
|
||||
* query
|
||||
* language
|
||||
* topic
|
||||
|
||||
* Example response:
|
||||
```json
|
||||
{"data": [{"text": "I love Titanic.", "id":1234, "polarity": 4},
|
||||
{"text": "I hate Titanic.", "id":4567, "polarity": 0}]}
|
||||
```
|
||||
'''
|
||||
import requests
|
||||
import json
|
||||
|
||||
ENDPOINT_URI = "http://www.sentiment140.com/api/bulkClassifyJson"
|
||||
|
||||
def analyse(texts):
|
||||
parameters = {"data": []}
|
||||
if isinstance(texts, list):
|
||||
for text in texts:
|
||||
parameters["data"].append({"text": text})
|
||||
else:
|
||||
parameters["data"].append({"text": texts})
|
||||
|
||||
res = requests.post(ENDPOINT_URI, json.dumps(parameters))
|
||||
res.json()
|
||||
return res.json()
|
||||
|
||||
def test():
|
||||
print analyse("I love Titanic")
|
||||
print analyse(["I love Titanic", "I hate Titanic"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
test()
|
Reference in New Issue
Block a user