python机器学习8--自然语言处理(2)

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
d=path.dirname(__file__)
jieba.load_userdict(path.join(d,r"C:\Users\nsy\Desktop\userdict.txt.txt"))
text="今天学习好烦躁,还没有效率"
content =text
extracted_tags=jieba.analyse.extract_tags(content,topK=10,withWeight=False)
print(" ,".join(extracted_tags))
jieba.analyse.set_stop_words(path.join(d, r"C:\Users\nsy\Desktop\stop_words.txt.txt"))
weighted_tags=jieba.analyse.extract_tags(content,topK=10,withWeight=True,allowPOS=('ns','n','vn','v'))
for item in weighted_tags:
    keyword,weight=item
    print(f"关键词:{keyword},权重:{weight}")

3. 排列出最常出现的分词( 次数的统计)

python 复制代码
import sys
from os import path
import jieba
import jieba.analyse

d = path.dirname(__file__)

# 根据Python版本打开文件
if sys.version_info > (3, 0):
    text = open(path.join(d, r"C:\\Users\\nsy\\Desktop\\test.txt"), 'r', encoding='utf-8').read()
else:
    text = open(path.join(d, r"C:\\Users\\nsy\\Desktop\\test.txt"), 'r').read()

text = text.replace('\n', '')

# 设置停用词文件路径,注意文件名是否正确
jieba.analyse.set_stop_words(r"C:\Users\nsy\Desktop\stop_words.txt.txt")
# 输出分词结果
print(" ".join(jieba.cut(text)))

# 打印分隔线
print("-" * 10)

# 使用自定义词典
jieba.load_userdict(path.join(d, r"C:\Users\nsy\Desktop\userdict.txt.txt"))

# 初始化字典存储词频
dic = {}

for ele in jieba.cut(text):
    if ele not in dic:
        dic[ele] = 1
    else:
        dic[ele] += 1

# 按词频排序并输出
for w in sorted(dic, key=dic.get, reverse=True):
    print("%s %d" % (w, dic[w]))

4.通过jieba来分析和计算网站文章所探讨的主要内容

python 复制代码
import sys
import jieba
import jieba.analyse
import urllib.request as httplib

# 网络请求异常处理
try:
    # 网络文章的网址
    url = "https://csdnnews.blog.csdn.net/article/details/140678511?spm=1000.2115.3001.5928"
    # 送出连接的需求
    req = httplib.Request(url)
    # 打开网页
    response = httplib.urlopen(req)
    # 连接网页正常(200)
    if 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, topK=5, withWeight=True, 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  # 记录为1
    else:
        dic[word] += 1  # 累加1

# 由大到小排列并打印
for w in sorted(dic.items(), key=lambda x: x[1], reverse=True):
    print("%s: %d" % w)

# 异常处理应该针对具体的操作,而不是放在代码的最后
相关推荐
算家计算4 分钟前
AI真的懂你!阿里发布Qwen3-Omni-Flash 全模态大模型:超强交互,人设任选
人工智能·算法·机器学习
森诺Alyson16 分钟前
前沿技术借鉴研讨-2025.12.9(胎儿面部异常检测/超声标准平面检测/宫内生长受限)
论文阅读·人工智能·经验分享·深度学习·论文笔记
亚马逊云开发者18 分钟前
使用Amazon Bedrock和Pipecat构建低延迟智能语音Agent
人工智能
yesyesyoucan22 分钟前
一键换背景,创意无界限——智能图片背景生成与替换平台,解锁视觉设计新可能
人工智能
monster000w23 分钟前
容器云2.7pytorch版本安装问题
人工智能·pytorch·python
云雾J视界26 分钟前
当AI下沉到MCU:嵌入式开发者的“能力护城河”正在被重写
人工智能·单片机·嵌入式硬件·mcu·freertos·岗位技能
Coding茶水间29 分钟前
基于深度学习的遥感地面物体检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
爱吃烤鸡翅的酸菜鱼39 分钟前
Catlass 模板库编程范式:昇腾高性能算子开发新高地
人工智能·cann
AI营销快线39 分钟前
AI营销内容生产:1人如何玩转抖音、小红书内容矩阵
大数据·人工智能·机器学习
小咖自动剪辑1 小时前
提升电商素材剪辑效率:多场景内容自动生成流程解析
人工智能·实时互动·音视频·语音识别·视频编解码