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

如何基于NLP实现语句逗号分割,排除作为值的逗号?

解决带内嵌逗号的语句分割问题:NLP替代正则方案

问题核心

需要按逗号分割多子句组成的语句,但部分逗号属于语义单元内部的分隔(比如示例中测试活动列表里的逗号),这类逗号不能作为子句分隔符。由于内嵌逗号位置随机,正则表达式无法覆盖所有场景,需用NLP方案解决。

NLP解决思路

1. 句法依存分析判断分隔符

利用NLP工具的句法解析能力,识别逗号的语法角色:

  • 当逗号连接的是同层级并列成分且属于同一语义范畴(如示例里的Test Assessment/Test Design/Test Optimization都是测试活动),则为内部逗号,不做分割;
  • 若逗号分隔的是独立子句(如“人工分类工作减负”和“搭建浏览器端测试应用”),则作为子句分隔符执行分割。

可使用spaCy、NLTK等工具实现,核心是通过逗号的依存关系标签(如cc/conj这类表示并列连接的标签)判断是否为内部逗号。

2. 语义块识别分组

先识别文本中的完整语义单元,再仅在单元间的逗号处分割:

  • 用短语分块(Chunking)或命名实体识别(NER)工具,将属于同一语义组的内容标记为整体;
  • 分割时跳过语义块内部的逗号,只处理块与块之间的逗号。

比如示例中,先识别出Test Assessment, Test Design, Test Optimization是一个完整的测试活动组,分割时直接将其作为单个元素保留。

3. 上下文分类模型(进阶)

如果场景复杂到规则无法覆盖,可训练二分类模型判断逗号类型:

  • 标注数据集:标记哪些逗号是子句分隔符,哪些是内部逗号;
  • 用BERT/RoBERTa等预训练模型微调,学习上下文语义特征;
  • 推理时对每个逗号做分类,仅在判定为“分隔符”的位置分割文本。

示例代码(spaCy实现)

import spacy

# 加载英文句法分析模型
nlp = spacy.load("en_core_web_sm")
input_text = "Reduction of manual triaging effort, Built browser based test application, Real-Time Monitoring of Audio and Video frame by frame,Test Assessment, Test Design, Test Optimization, Test Report and Test Bench creation, Providing high-level and low-level requirements for Automation of key modules, Test Analysis and Use-case generation and Scope identification for the CFRs, Network protocols testing and Security testing using in-home solutions and external third party tools, Effective Escape Defect Analysis strategy"

doc = nlp(input_text)
segments = []
current_segment = []

for token in doc:
    # 当逗号不是连接并列成分的标记时,执行分割
    if token.text == "," and token.dep_ not in ("cc", "conj"):
        segments.append("".join([t.text_with_ws for t in current_segment]).strip())
        current_segment = []
    else:
        current_segment.append(token)

# 处理最后一段内容
segments.append("".join([t.text_with_ws for t in current_segment]).strip())

# 输出结果
for seg in segments:
    print(seg)

执行后输出:

Reduction of manual triaging effort
Built browser based test application
Real-Time Monitoring of Audio and Video frame by frame
Test Assessment, Test Design, Test Optimization
Test Report and Test Bench creation
Providing high-level and low-level requirements for Automation of key modules
Test Analysis and Use-case generation and Scope identification for the CFRs
Network protocols testing and Security testing using in-home solutions and external third party tools
Effective Escape Defect Analysis strategy

注意事项

  • 工具适配:英文场景优先选spaCy,中文场景可使用HanLP或Jieba结合句法分析插件;
  • 规则调优:根据业务领域调整句法规则,比如特定行业术语组需单独处理;
  • 性能权衡:规则方案速度快,适合简单场景;预训练模型方案通用性强,但部署成本较高。

内容的提问来源于stack exchange,提问作者Shashank Joshi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 12:17:54