本文主要为【爬取百度搜索内容页广告均数】提供关键词文件,主要做输入文件的分词功能,并写入key_word.txt文件,以供下一模块使用。
https://blog.youkuaiyun.com/qq_36791314/article/details/86724025
函数功能主要为调用简单的jiaba分词(stripdata函数)并进行停用词去除(stripword函数)
main函数为creat(),可修改为if __name__ ==’__main__’: 进行调用。
文件解释:
- Rawdata 初始数据,即一个段落或文章
- stop 停用词文件,用\n间隔
- keyword 关键词表
import jieba
#分词
def stripdata(Test):
# jieba 默认启用了HMM(隐马尔科夫模型)进行中文分词
seg_list = jieba.cut(Test,cut_all=True) # 分词
#获取字典,去除停用词
line = "/".join(seg_list)
word = stripword(line)
#print(line)
#列出关键字
print("\n关键字:\n"+word)
#停用词分析
def stripword(seg):
#打开写入关键词的文件
keyword = open('key_word.txt', 'w+', encoding='utf-8')
print("去停用词:\n")
wordlist = []
#获取停用词表
stop = open('stopword.txt', 'r+', encoding='utf-8')
stopword = stop.read().split("\n")
#遍历分词表
for key in seg.split('/'):
#print(key)
#去除停用词,去除单字,去除重复词
if not(key.strip() in stopword) and (len(key.strip()) > 1) and not(key.strip() in wordlist) :
wordlist.append(key)
print(key)
keyword.write(key+"\n")
#停用词去除END
stop.close()
keyword.close()
return '/'.join(wordlist)
def creat():
Rawdata = open('raw.txt','r+',encoding='utf-8')
text = Rawdata.read()
#调用分词
stripdata(text)
#END
Rawdata.close()