python词频统计 情感分析
时间: 2023-10-27 14:08:26 浏览: 52
好的,针对您的问题,我可以给出以下建议:
1. Python词频统计可以使用Python内置的collections模块中的Counter类来实现。具体实现方法可以参考以下代码:
```python
from collections import Counter
text = "This is a sample text with several words. This is another sample text with some different words."
# 将文本转换为小写并分割成单词列表
words = text.lower().split()
# 使用Counter类统计单词出现次数
word_counts = Counter(words)
# 输出前10个出现次数最多的单词
for word, count in word_counts.most_common(10):
print(word, count)
```
2. 情感分析可以使用Python中的一些第三方库来实现,比如NLTK、TextBlob、Stanford CoreNLP等。这些库可以帮助你对文本进行情感分析,并输出文本的情感极性(积极、消极或中性)。以下是使用TextBlob库进行情感分析的示例代码:
```python
from textblob import TextBlob
text = "I love this product! It's amazing!"
# 使用TextBlob库进行情感分析
blob = TextBlob(text)
sentiment = blob.sentiment.polarity
# 输出情感极性
if sentiment > 0:
print("Positive")
elif sentiment < 0:
print("Negative")
else:
print("Neutral")
```