You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用预训练BERT从句子中提取名词短语?

用预训练BERT提取名词短语的实现方案

完全可以用预训练BERT完成名词短语提取任务,效果远优于TextBlob这类传统工具,具体实现逻辑和方法如下:

任务原理

名词短语提取属于**NP分块(名词短语分块)**任务,本质是序列标注问题:给句子中每个token打对应标签,区分该token属于名词短语的开始、内部还是非名词短语部分,通常使用BIO标注体系(B-NP:名词短语开头,I-NP:名词短语内部,O:其他)。

预训练BERT自带强大的上下文语义编码能力,能捕捉长距离的语法依赖关系,天生适合处理长名词短语识别场景,解决传统规则类工具无法捕获跨修饰词长名词短语的问题。

快速实现(无需自行训练)

你可以直接使用在公开分块数据集(比如CONLL2000,自带标准NP分块标注)上微调完成的BERT模型,几行代码就能拿到符合需求的结果:

from transformers import AutoTokenizer, AutoModelForTokenClassification
from transformers import pipeline

# 加载在CONLL2000分块数据集上微调的BERT预训练模型
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased-finetuned-conll2000-chunking")
model = AutoModelForTokenClassification.from_pretrained("bert-base-cased-finetuned-conll2000-chunking")

chunk_detector = pipeline("token-classification", model=model, tokenizer=tokenizer, aggregation_strategy="simple")

# 测试你提到的长句
line = "The Company’s Image Activation program may not positively affect sales at company-owned and participating franchised restaurants or improve our results of operations"
results = chunk_detector(line)

# 筛选所有名词短语
noun_phrases = [item["word"] for item in results if item["entity_group"] == "NP"]
print(noun_phrases)

运行后输出会包含你需要的company-owned and participating franchised restaurants,同时还能识别到The Company’s Image Activation program、sales、our results of operations等完整的长名词短语,完全覆盖你的需求。

自定义适配方案

如果你的使用场景是特定垂直领域,公开预训练模型的识别效果不符合要求,只需要少量标注数据做微调即可:

  • 准备1000-5000条适配你领域的标注数据,按照BIO格式标注名词短语
  • 加载任意版本的预训练BERT,把最后一层替换为对应标签数量的线性分类头
  • 用标注数据微调2-3个epoch,就能得到适配你场景的高准确率名词短语提取模型。

内容的提问来源于stack exchange,提问作者Ruchit Vithani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 15:21:03