webis-twitter情感识别工具的python包装器
webis的Python项目详细描述
webis twitter情感评估集成的python包装器
这是一个python包装器,它围绕着由Hagen et al. (2015)提出的twitter情感评估框架的java实现。它使用PyJnius调用java模块来评估情绪。
依赖性
脚本是用python 3编写的,依赖于python模块PyJnius、pandas和emojientities。
除此之外,还需要一个Java运行时环境(JRE),外加一个匹配的Java开发工具包(JDK)。我们使用了java 8,但是其他版本也可以。OpenJDK工作正常。
要在基于debian的系统上安装所有依赖项,请运行:
apt-get update -y &&
apt-get install -y python3-dev python3-pip python3-virtualenv cython3 openjdk-8-jdk-headless openjdk-8-jre-headless ca-certificates-java
(有一个archlinux aur包引入了所有依赖项,请参阅下一步)
安装
- 使用
pip
或类似:
pip3 install webis
或:手动:
- 克隆此存储库
git clone https://gitlab.com/christoph.fink/python-webis.git
- 切换到克隆目录
- 使用python
setuptools
安装包:
cd python-webis python3 ./setup.py install
或:(仅限Arch Linux)来自AUR:
# e.g. using yaourt
yaourt python-webis
使用量
导入webis
模块。在第一次运行时,python webis将下载并编译java后端,这可能需要几分钟的时间。
然后实例化一个webis.SentimentIdentifier
对象并使用它的identifySentiment()
函数,传入一个元组列表([(tweetId, tweetText),(tweetId, tweetText), … ]
)、一个dict({tweetId: tweetText, … }
)或一个pandas.DataFrame
(第一列作为标识符,第二列作为tweettext)。
函数返回一个元组列表([(tweetId, sentiment), … ]
)、一个dict({tweetId: sentiment, … }
)或一个数据帧(第一列id,第二列情感),它成功地标识了一个情感。返回值的类型与调用函数的参数匹配。tweetId
值将强制转换为第一行的类型tweetId
。
importwebissentimentIdentifier=webis.SentimentIdentifier()# list of tuplestweets=[(1,"What a beautiful morning! There’s nothing better than cycling to work on a sunny day ?."),(2,"Argh, I hate it when you find seven (7!) cars blocking the bike lane on a five-mile commute")]tweets=sentimentIdentifier.identifySentiment(tweets)# [(1, "positive"), (2, "negative")]# pandas Dataframeimportpandastweets=pandas.DataFrame([(1,"What a beautiful morning! There’s nothing better than cycling to work on a sunny day ?."),(2,"Argh, I hate it when you find seven (7!) cars blocking the bike lane on a five-mile commute")])tweets=sentimentIdentifier.identifySentiment(tweets)# sentiment tweetId# 0 positive 1# 1 negative 2# dicttweets={1:"What a beautiful morning! There’s nothing better than cycling to work on a sunny day ?.",2:"Argh, I hate it when you find seven (7!) cars blocking the bike lane on a five-mile commute"}tweets=sentimentIdentifier.identifySentiment(tweets)# { 1: "positive", 2: "negative" }
python-webis
可以充当context manager:
withwebis.SentimentIdentifier()ass:tweets=s.identifySentiment(tweets)
webis.identifySentiment()
是初始化一个SentimentIdentifier
对象并调用其identifySentiment()
方法的简写方法:
tweets=webis.identifySentiment(tweets)