java对文字按照语义切分

实现目标

把一段文本按照一个完整的一句话为单元进行切分。如:以逗号,感叹号结尾看作是一个句子。

实现方案

StanfordCoreNLP切分

引入依赖
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>4.5.4</version>
        </dependency>
测试验证

import edu.stanford.nlp.pipeline.*;
import java.util.Properties;

public class CoreNLPSentenceSplitter {
    public static void main(String[] args) {
        // 设置属性
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit");

        // 创建管道
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        // 创建文档
        String text = """
                你好吗?我今天去了公园。你知道公园在哪里吗?天气真好啊!你喜欢什么运动?
                """;
        CoreDocument document = new CoreDocument(text);

        // 分析文本
        pipeline.annotate(document);

        // 获取句子
        for (CoreSentence sentence : document.sentences()) {
            System.out.println(sentence.text());
        }
    }
}
输出
你好吗?
我今天去了公园。
你知道公园在哪里吗?
天气真好啊!
你喜欢什么运动?

说明

通过StanfordCoreNLP知识对文本进行切割,如果需要进行句法分析还需要引入对应语言模型的依赖。由于我们没有用到这种功能,所以暂时就不引入了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值