高中物理的题库里提取所有物理关键词

本文介绍了一种利用TF-IDF算法对中文文本进行关键词抽取的方法,并通过jieba分词工具实现了具体的功能。文章详细展示了如何加载用户自定义词典、停用词表,以及如何对输入文本进行预处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 29 18:41:47 2018


@author: loka
"""
import jieba
from jieba import analyse
import codecs
# 引入TF-IDF关键词抽取接口
tfidf = analyse.extract_tags
#添加用户自定义词
jieba.load_userdict("D:\\software\\development\\anaconda\\dictronary\\gzwl.txt")
#添加停用词
stoplist = [line.strip() for line in codecs.open("D:\\software\\development\\anaconda\\dictronary\\stopword.txt",encoding='utf-8')]
#数据预处理
def is_ustr(in_str):
    out_str=''
    for i in range(len(in_str)):
        if is_uchar(in_str[i]):
            out_str=out_str+in_str[i]
        else:
            out_str=out_str+' '
    return out_str
def is_uchar(uchar):
    """判断一个unicode是否是汉字"""
    if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
            return True
    """判断一个unicode是否是数字"""
    if uchar >= u'\u0030' and uchar<=u'\u0039':
            return False        
    """判断一个unicode是否是英文字母"""
    if (uchar >= u'\u0041' and uchar<=u'\u005a') or (uchar >= u'\u0061' and uchar<=u'\u007a'):
            return False
    if uchar in ('-',',',',','。','.','>','?'):
            return False
    return False
#输入输出文件存放位置
inputs = open('D:\\software\\development\\anaconda\\dictronary\\input.txt', 'r', encoding='utf-8')  
outputs = open('D:\\software\\development\\anaconda\\dictronary\\output.txt', 'w')  
for line in inputs:
    text=is_ustr(line)
# 基于TF-IDF算法进行关键词抽取
keywords = tfidf(text,10)
for key in keywords:
    #print(key)
    segs=[word for word in list(keywords) if word not in stoplist]
    # 对句子去除停用词
for i in range(3):
    print(segs[i])
    outputs.write(segs[i] + '\n')  
outputs.close()  
inputs.close()  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值