jupyter 读取自己的csv来去除中文停用词
时间: 2023-08-15 15:06:13 浏览: 46
你可以使用 Python 的 pandas 库来读取 CSV 文件,并使用中文停用词列表来去除停用词。以下是一些示例代码:
```python
import pandas as pd
import jieba
from zhon import hanzi # 用于匹配中文字符的库
from collections import Counter
# 读取 CSV 文件
df = pd.read_csv("your_file.csv")
# 定义中文停用词列表
stop_words = ['的', '了', '在', '是', '我', '有', '和', '就', '不', '人', '都', '一', '一个', '上', '也', '很', '到', '说', '要', '去', '你', '会', '着', '没有', '看', '好', '自己', '这']
# 定义分词函数
def cut_words(sentence):
# 匹配中文字符
pattern = hanzi.characters
# 分词并去除停用词
words = [word for word in jieba.cut(sentence) if word not in stop_words and re.match(pattern, word)]
return words
# 对文本进行分词
df['words'] = df['text'].apply(cut_words)
# 统计词频
word_counts = Counter([word for words in df['words'] for word in words])
# 打印出现频率最高的 20 个词语
for word, count in word_counts.most_common(20):
print(f"{word}: {count}")
```
在这个例子中,我们使用了 pandas 库来读取 CSV 文件,并使用 jieba 库进行中文分词。我们还定义了一个中文停用词列表,以去除常见的无意义词语。最后,我们使用 Counter 对所有词语进行计数,并打印出现频率最高的 20 个词语。请注意,这只是一个示例,你需要根据自己的需求进行修改和调整。