做网站在哪里,泰安市最大的网络公司,做个商城网站要多少钱,网站建设意见征求1#xff0e;移除用词 在很多情况下#xff0c;有一些文章内的英文字符、标点符号分词的结果不符合自己的预期#xff0c;会出现一些不想要的分词#xff0c;此时就能通过以下的函数自己设定用词#xff0c;并且删除。
jieba.analyse.set_stop_words(stop_words.tx…1移除用词 在很多情况下有一些文章内的英文字符、标点符号分词的结果不符合自己的预期会出现一些不想要的分词此时就能通过以下的函数自己设定用词并且删除。
jieba.analyse.set_stop_words(stop_words.txt)
2自定比重分数 因为jieba对每一个字会给出IDF分数比重但是在很多时候会希望把文章中特别的关键字突显出来或者降低可以设定IDF分数高一些或低一些就能将想要的字突显出来或者降低。 jieba.analyse.set_idf_path(idf.txt) #读入IDF关键字比重分数
一个demo
import sys
from os import path
import jieba
import jieba.analyse
dpath.dirname(__file__)
jieba.load_userdict(path.join(d,rC:\Users\nsy\Desktop\userdict.txt.txt))
text今天学习好烦躁还没有效率
content text
extracted_tagsjieba.analyse.extract_tags(content,topK10,withWeightFalse)
print( ,.join(extracted_tags))
jieba.analyse.set_stop_words(path.join(d, rC:\Users\nsy\Desktop\stop_words.txt.txt))
weighted_tagsjieba.analyse.extract_tags(content,topK10,withWeightTrue,allowPOS(ns,n,vn,v))
for item in weighted_tags:keyword,weightitemprint(f关键词:{keyword},权重:{weight}) 3.排列出最常出现的分词次数的统计
import sys
from os import path
import jieba
import jieba.analysed path.dirname(__file__)# 根据Python版本打开文件
if sys.version_info (3, 0):text open(path.join(d, rC:\\Users\\nsy\\Desktop\\test.txt), r, encodingutf-8).read()
else:text open(path.join(d, rC:\\Users\\nsy\\Desktop\\test.txt), r).read()text text.replace(\n, )# 设置停用词文件路径注意文件名是否正确
jieba.analyse.set_stop_words(rC:\Users\nsy\Desktop\stop_words.txt.txt)
# 输出分词结果
print( .join(jieba.cut(text)))# 打印分隔线
print(- * 10)# 使用自定义词典
jieba.load_userdict(path.join(d, rC:\Users\nsy\Desktop\userdict.txt.txt))# 初始化字典存储词频
dic {}for ele in jieba.cut(text):if ele not in dic:dic[ele] 1else:dic[ele] 1# 按词频排序并输出
for w in sorted(dic, keydic.get, reverseTrue):print(%s %d % (w, dic[w])) 4.通过jieba来分析和计算网站文章所探讨的主要内容
import sys
import jieba
import jieba.analyse
import urllib.request as httplib# 网络请求异常处理
try:# 网络文章的网址url https://csdnnews.blog.csdn.net/article/details/140678511?spm1000.2115.3001.5928# 送出连接的需求req httplib.Request(url)# 打开网页response httplib.urlopen(req)# 连接网页正常200if response.status 200:# 如果是 Python 3.0 以上if sys.version_info (3, 0):# 取得网页的数据并解码contents response.read().decode(response.headers.get_content_charset())else:# 考虑到 Python 2 不再使用这里可以省略对应的处理逻辑raise Exception(Python 2 is not supported)
except Exception as e:print(Error during HTTP request:, e)contents # 去除不要的文字
jieba.analyse.set_stop_words(C:\\Users\\nsy\\Desktop\\stop_words.txt.txt)# 仅捕获地名、名词、动名词、动词
keywords jieba.analyse.extract_tags(contents, topK5, withWeightTrue, allowPOS(ns, n, vn))# 输出关键词和相应的权重
for item in keywords:print(%s%f % (item[0], item[1]))print(* * 40)# 数据结构字典 key:value
dic {}# 做分词动作
words jieba.cut(contents)# 仅处理名词、动名词
for word in words:if word not in dic:dic[word] 1 # 记录为1else:dic[word] 1 # 累加1# 由大到小排列并打印
for w in sorted(dic.items(), keylambda x: x[1], reverseTrue):print(%s: %d % w)# 异常处理应该针对具体的操作而不是放在代码的最后