使用python 写一份nlp
时间: 2023-07-20 16:23:02 浏览: 46
以下是一个使用Python进行NLP的示例代码:
```python
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.sentiment import SentimentIntensityAnalyzer
# 加载停用词表
stop_words = set(stopwords.words('english'))
# 加载情感分析器
sia = SentimentIntensityAnalyzer()
# 加载文本数据
text = "The movie was amazing! The special effects were incredible and the acting was superb."
# 分词
tokens = word_tokenize(text)
# 去除停用词
filtered_tokens = [token for token in tokens if token not in stop_words]
# 计算情感分数
scores = sia.polarity_scores(' '.join(filtered_tokens))
# 输出情感分数
print(scores)
```
该代码使用NLTK库进行情感分析,首先加载了停用词表和情感分析器,然后对输入的文本进行了分词和去除停用词的处理,最后使用情感分析器计算情感分数。在本例中,输出的情感分数为:
```
{'neg': 0.0, 'neu': 0.546, 'pos': 0.454, 'compound': 0.6486}
```
其中,'neg'表示负面情感分数,'neu'表示中性情感分数,'pos'表示正面情感分数,'compound'表示综合情感分数。根据输出结果可以看出,该代码对输入的文本进行了情感分析,并得出了该文本的情感分数。