python英文词频统计
时间: 2023-08-31 07:12:33 浏览: 121
Python之词频统计
Sure, I can help you with that! To perform word frequency analysis in Python, you can use the Counter object from the collections module. Here's a sample code:
```python
from collections import Counter
# read in your text file (substitute 'filename.txt' with your own file)
with open('filename.txt', 'r') as f:
text = f.read()
# remove any unwanted characters or symbols
text = text.lower().replace('\n', ' ').replace('\r', ' ')
words = text.split(' ')
# count the frequency of each word
freq = Counter(words)
# display the most common words and their frequencies
for word, count in freq.most_common(10):
print(f'{word}: {count}')
```
This code reads in a text file, converts all words to lowercase, and removes new line and carriage return characters. It then splits the text into individual words, and uses the Counter object to count the frequency of each word. Finally, it displays the top 10 most common words and their frequencies. I hope this helps!
阅读全文