import jieba excludes={"将军","却说","这样","他们","东汉","","然而","自己","这个","没有"} txt=open("C:\python\三国演义.txt","r",encoding='utf-8').read() words=jieba.lcut(txt) counts={} for word in words: if len(word)==1: continue else: counts[word]=counts.get(word,0)+1 items=list(counts.items()) items.sort(key=lambda x:x[1],reverse=True) for i in range(15): word,count=items[i] print("{0:<10}{1:>5}".format(word,count))请问这段代码有错吗
时间: 2023-06-17 09:02:10 浏览: 219
这段代码缺少语法错误,但可能会有以下问题:
1. `excludes`参数应该是`jieba.lcut()`的参数之一,应该写成`jieba.lcut(txt, exclude=excludes)`。
2. 打开文件路径中的反斜杠`\`应该写成双反斜杠`\\`或者使用原始字符串`r`,即`txt=open(r"C:\python\三国演义.txt","r",encoding='utf-8').read()`。
3. `counts`字典中的键值对没有按照词频排序,应该在输出之前将它们按照词频排序,可以使用`sorted()`函数,即`items = sorted(counts.items(), key=lambda x:x[1], reverse=True)`。
4. 如果要剔除一些常用的词语,需要将它们放在`excludes`列表中,而不是将它们写成一个字符串。
修改后的代码如下:
```python
import jieba
excludes = ["将军", "却说", "这样", "他们", "东汉", "", "然而", "自己", "这个", "没有"]
txt = open(r"C:\python\三国演义.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt, exclude=excludes)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word, 0) + 1
items = sorted(counts.items(), key=lambda x:x[1], reverse=True)
for i in range(15):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
```
阅读全文