jieba关键词提取(目前最佳)


中文语言处理社区:https://github.com/fighting41love/funNLP
项目地址:https://github.com/fxsjy/jieba
pip install jieba

示范:

python
import jieba.analyse

# 输入文本
text = """
人工智能是计算机科学的一个分支,它试图了解智能的本质,并生产出一种新的能以人类智能方式行事的机器。
研究这个问题的一个主要目标是使机器能胜任一些通常需要人类智能才能完成的复杂工作。
"""

# 提取关键词,默认使用 TF-IDF 算法
# topK:返回前几个关键词
# withWeight=True:返回每个关键词的权重
keywords = jieba.analyse.extract_tags(text, topK=8)

# 输出结果
print("关键词提取结果")
print(" ".join(keywords))

提取的关键词继续按原文顺序排序:

python
import jieba
import jieba.analyse

# 输入文本
text = """
复易信息成立于2021年,是一家专注于GPU算力服务的IT基础设施提供商。作为国内具备重要影响力的企业之一,我们致力于为客户提供高性能的数据中心服务和灵活的混合IT解决方案。依托在中国多个机房建立的专业数据中心,我们为客户提供覆盖广泛的GPU算力销售与租赁服务,助力企业高效应对各类计算需求,成为行业内值得信赖的首选合作伙伴。
"""

# 提取关键词(不按顺序)
keywords_unordered = jieba.analyse.extract_tags(text, topK=8)
print("提取的关键词(无序):", keywords_unordered)

# 将文本分词
seg_list = jieba.cut(text)
words_with_position = []

# 记录每个词在原文中的位置
position = 0
for word in seg_list:
    if word in keywords_unordered:
        words_with_position.append((word, position))
    position += 1

# 按位置排序
ordered_keywords = [word for word, pos in sorted(words_with_position, key=lambda x: x[1])]

# 去重(保留第一次出现的位置)
ordered_unique_keywords = []
for word in ordered_keywords:
    if word not in ordered_unique_keywords:
        ordered_unique_keywords.append(word)

print("按原文顺序排序的关键词:", ordered_unique_keywords)
print("按原文顺序排序的关键词(字符串):", " ".join(ordered_unique_keywords))

中文语义理解hanlp关键词提取

适用于,关键词提取,实体识别,情感分析等

文档:https://hanlp.hankcs.com/docs/configure.html
模型文件:https://ftp.hankcs.com/hanlp/
项目地址:https://github.com/hankcs/HanLP

安装本地包(也有RESTful API包)
pip install hanlp

下载好模型后解压成文件夹

python
import hanlp

# 加载预训练模型
tokenizer = hanlp.load('fine_electra_small') #假设已下载好模型并解压
tagger = hanlp.load(hanlp.pretrained.pos.CTB9_POS_ELECTRA_SMALL)

text = "复易信息成立于2021年,是一家专注于GPU算力服务的IT基础设施提供商。作为国内具备重要影响力的企业之一,我们致力于为客户提供高性能的数据中心服务和灵活的混合IT解决方案。依托在中国多个机房建立的专业数据中心,我们为客户提供覆盖广泛的GPU算力销售与租赁服务,助力企业高效应对各类计算需求,成为行业内值得信赖的首选合作伙伴。"

# 分词
tokens = tokenizer(text)
# 词性标注
pos_tags = tagger(tokens)

# 提取名词短语作为关键词
keywords = []
for word, tag in zip(tokens, pos_tags):
    # 选择名词、专有名词等
    if tag.startswith('N') and len(word) > 1:
        keywords.append(word)

print("HanLP提取的关键词:", keywords[:8])

StructBert关键词抽取-中文-base

项目https://www.modelscope.cn/models/iic/nlp_structbert_keyphrase-extraction_base-icassp2023-mug-track4-baseline

克隆项目git clone https://www.modelscope.cn/iic/nlp_structbert_keyphrase-extraction_base-icassp2023-mug-track4-baseline.git

依赖安装requirements.txt

ini
addict==2.4.0
aiohappyeyeballs==2.6.1
aiohttp==3.11.16
aiosignal==1.3.2
attrs==25.3.0
certifi==2025.1.31
charset-normalizer==3.4.1
colorama==0.4.6
datasets==2.16.0
dill==0.3.7
filelock==3.18.0
frozenlist==1.5.0
fsspec==2023.10.0
huggingface-hub==0.30.2
idna==3.10
Jinja2==3.1.6
MarkupSafe==3.0.2
modelscope==1.24.1
mpmath==1.3.0
multidict==6.4.2
multiprocess==0.70.15
networkx==3.4.2
numpy==2.2.4
packaging==24.2
pandas==2.2.3
pillow==11.1.0
propcache==0.3.1
pyarrow==19.0.1
pyarrow-hotfix==0.6
python-dateutil==2.9.0.post0
pytz==2025.2
PyYAML==6.0.2
regex==2024.11.6
requests==2.32.3
safetensors==0.5.3
simplejson==3.20.1
six==1.17.0
sortedcontainers==2.4.0
sympy==1.13.1
tokenizers==0.21.1
torch==2.6.0
tqdm==4.67.1
transformers==4.51.1
typing_extensions==4.13.1
tzdata==2025.2
urllib3==2.3.0
xxhash==3.5.0
yarl==1.19.0

演示代码:

python
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

ner_pipeline = pipeline(Tasks.named_entity_recognition, 'iic/nlp_structbert_keyphrase-extraction_base-icassp2023-mug-track4-baseline')
result = ner_pipeline('哎大家好啊欢迎大家准时来参加我们的会议,今天我们会议的主题呢是如何提升我们这个洗发水儿的品牌影响力啊。我们现在是主要的产品是这个霸王防脱洗发水儿,现在请大家发表自己的意见啊欢迎大家努力的发表,请先从这位男士开始。')

print(result)
# 会打印如下内容:
# {'output': [{'type': 'KEY', 'start': 39, 'end': 42, 'span': '洗发水'}, {'type': 'KEY', 'start': 44, 'end': 46, 'span': '品牌'}, {'type': 'KEY', 'start': 46, 'end': 49, 'span': '影响力'}, {'type': 'KEY', 'start': 59, 'end': 61, 'span': '产品'}, {'type': 'KEY', 'start': 68, 'end': 71, 'span': '洗发水'}]}