import jieba def word_extract(): # 读取文件 corpus = [] path = 'D:/自然语言处理/第2章/data/金庸-白马啸西风.txt' content = '' for line in open(path, 'r', encoding='gbk', errors='ignore'): line = line.strip() content += line corpus.append(content) # 加载停用词 stop_words = [] path = 'D:/自然语言处理/第4章/data/stopword.txt' for line in open(path, encoding='utf8'): line = line.strip() stop_words.append(line) # jieba分词 split_words = [] word_list = jieba.cut(corpus[0]) for word in word_list: if word not in stop_words: split_words.append(word)这段代码哪里有问题
时间: 2024-02-07 15:02:12 浏览: 94
这段代码的缩进有问题,导致程序运行时会报错。具体来说,for循环和if语句的缩进应该向右移动一个制表符或四个空格,如下所示:
```
import jieba
def word_extract():
# 读取文件
corpus = []
path = 'D:/自然语言处理/第2章/data/金庸-白马啸西风.txt'
content = ''
for line in open(path, 'r', encoding='gbk', errors='ignore'):
line = line.strip()
content += line
corpus.append(content)
# 加载停用词
stop_words = []
path = 'D:/自然语言处理/第4章/data/stopword.txt'
for line in open(path, encoding='utf8'):
line = line.strip()
stop_words.append(line)
# jieba分词
split_words = []
word_list = jieba.cut(corpus[0])
for word in word_list:
if word not in stop_words:
split_words.append(word)
return split_words
```
阅读全文