前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用opennlp自定义命名实体

使用opennlp自定义命名实体

作者头像
code4it
发布2018-09-17 16:07:57
1.2K0
发布2018-09-17 16:07:57
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下如何使用opennlp自定义命名实体,标注训练及模型运用。

maven

代码语言:javascript
复制
        <dependency>
            <groupId>org.apache.opennlp</groupId>
            <artifactId>opennlp-tools</artifactId>
            <version>1.8.4</version>
        </dependency>

实践

训练模型

代码语言:javascript
复制
// train the name finder
        String typedEntities = "<START:organization> NATO <END>\n" +
                "<START:location> United States <END>\n" +
                "<START:organization> NATO Parliamentary Assembly <END>\n" +
                "<START:location> Edinburgh <END>\n" +
                "<START:location> Britain <END>\n" +
                "<START:person> Anders Fogh Rasmussen <END>\n" +
                "<START:location> U . S . <END>\n" +
                "<START:person> Barack Obama <END>\n" +
                "<START:location> Afghanistan <END>\n" +
                "<START:person> Rasmussen <END>\n" +
                "<START:location> Afghanistan <END>\n" +
                "<START:date> 2010 <END>";
        ObjectStream<NameSample> sampleStream = new NameSampleDataStream(
                new PlainTextByLineStream(new MockInputStreamFactory(typedEntities), "UTF-8"));

        TrainingParameters params = new TrainingParameters();
        params.put(TrainingParameters.ALGORITHM_PARAM, "MAXENT");
        params.put(TrainingParameters.ITERATIONS_PARAM, 70);
        params.put(TrainingParameters.CUTOFF_PARAM, 1);

        TokenNameFinderModel nameFinderModel = NameFinderME.train("eng", null, sampleStream,
                params, TokenNameFinderFactory.create(null, null, Collections.emptyMap(), new BioCodec()));

opennlp使用<START>及 <END>来进行自定义标注实体,命名实体的话则在START之后用冒号标明,比如<START:person>

参数说明

  • ALGORITHM_PARAMOn the engineering level, using maxent is an excellent way of creating programs which perform very difficult classification tasks very well.
  • ITERATIONS_PARAMnumber of training iterations, ignored if -params is used.
  • CUTOFF_PARAMminimal number of times a feature must be seen

使用模型

上面训练完模型之后,就可以使用该模型进行解析

代码语言:javascript
复制
      NameFinderME nameFinder = new NameFinderME(nameFinderModel);

        // now test if it can detect the sample sentences

        String[] sentence = "NATO United States Barack Obama".split("\\s+");

        Span[] names = nameFinder.find(sentence);

        Stream.of(names)
                .forEach(span -> {
                    String named = IntStream.range(span.getStart(),span.getEnd())
                            .mapToObj(i -> sentence[i])
                            .collect(Collectors.joining(" "));
                    System.out.println("find type: "+ span.getType()+",name: " + named);
                });

输出如下:

代码语言:javascript
复制
find type: organization,name: NATO
find type: location,name: United States
find type: person,name: Barack Obama

小结

opennlp的自定义命名实体的标注,给以了一定定制空间,方便开发者定制各自领域特殊的命名实体,以提高特定命名实体分词的准确性。

doc

  • opennlp-1.8.4-docs
  • OpenNLP进行中文命名实体识别(上:预处理及训练模型)
  • OpenNLP进行中文命名实体识别(下:载入模型识别实体)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-03-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • maven
  • 实践
    • 训练模型
      • 使用模型
      • 小结
      • doc
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档