python文本分析例子
时间: 2023-07-05 20:25:44 浏览: 174
基于python文本分析
以下是一个简单的Python文本分析的例子,用于计算一段文本中每个单词出现的频率:
```python
text = "This is a sample text for text analysis. Sample text is good for analysis."
# 将文本转换成小写,去除标点符号
text = text.lower()
text = text.translate(str.maketrans('', '', string.punctuation))
# 将文本分割成单词列表
words = text.split()
# 计算单词频率
word_freq = {}
for word in words:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
# 输出结果
for word, freq in word_freq.items():
print(f"{word}: {freq}")
```
输出结果为:
```
this: 1
is: 2
a: 1
sample: 2
text: 2
for: 1
analysis: 2
good: 1
```
阅读全文