zoukankan      html  css  js  c++  java
  • HanLP汉语言分析框架

      HanLP(Han Language Processing)是由一系列模型与算法组成的Java工具包,目标是普及自然语言处理在生产环境中的应用。

    HanLP具备功能完善、性能高效、架构清晰、语料时新、可自定义的特点。

    环境搭建

    1.创建java项目,导入HanLP必要的包

    2.把对应的配置文件放置在src下

    3.修改hanlp.properties配置文件,使其指向data(data中包含词典和模型)的上级路径,修改如下,

    代码运行

    1.第一个Demo

    1. System.out.println(HanLP.segment("你好,欢迎使用HanLP汉语处理包!"));
    2. //标准分词
    3. List<Term> standardList = StandardTokenizer.segment("商品和服务");
    4. System.out.println(standardList);

    结果:

    注意:HanLP.segment其实是对StandardTokenizer.segment的包装。

    2.索引分词

    1. List<Term> indexList = IndexTokenizer.segment("主副食品");
    2. for (Term term : indexList)
    3. {
    4. System.out.println(term + " [" + term.offset + ":" + (term.offset + term.word.length()) + "]");
    5. }

    结果:

    注意:索引分词IndexTokenizer是面向搜索引擎的分词器,能够对长词全切分,另外通过term.offset可以获取单词在文本中的偏移量。

    3.自然语言分词

    1. List<Term> nlpList = NLPTokenizer.segment("中国科学院计算技术研究所的宗成庆教授正在教授自然语言处理课程");
    2. System.out.println(nlpList);

    结果:

    注意:自然语言分词NLPTokenizer会执行全部命名实体识别和词性标注。

    4.最短路径分词&N-最短路径分词

    1. String[] testCase = new String[]{
    2. "今天,刘志军案的关键人物,山西女商人丁书苗在市二中院出庭受审。",
    3. "刘喜杰石国祥会见吴亚琴先进事迹报告团成员",
    4. };
    5. //N-最短路径分词
    6. Segment nShortSegment = new NShortSegment().enableCustomDictionary(false).enablePlaceRecognize(true).enableOrganizationRecognize(true);
    7. for (String sentence : testCase)
    8. {
    9. System.out.println("N-最短分词:" + nShortSegment.seg(sentence));
    10. }
    11. //最短路径分词
    12. Segment shortestSegment = new DijkstraSegment().enableCustomDictionary(false).enablePlaceRecognize(true).enableOrganizationRecognize(true);
    13. for (String sentence : testCase)
    14. {
    15. System.out.println("最短路分词:" + shortestSegment.seg(sentence));
    16. }

    结果:

    注意:

    • N最短路分词器NShortSegment比最短路分词器慢,但是效果稍微好一些,对命名实体识别能力更强。

    • 一般场景下最短路分词的精度已经足够,而且速度比N最短路分词器快几倍,请酌情选择。

    5.CRF(条件随机场算法)分词

    1. Segment segment = new CRFSegment();
    2. segment.enablePartOfSpeechTagging(true);
    3. List<Term> crfList = segment.seg("你看过环太平洋吗");
    4. System.out.println(crfList);
    5. for (Term term : crfList)
    6. {
    7. if (term.nature == null)
    8. {
    9. System.out.println("识别到新词:" + term.word);
    10. }
    11. }

    结果:

    注意:CRF对新词有很好的识别能力,但是无法利用自定义词典。

    6.用户自定义词典

    1. // 动态增加
    2. CustomDictionary.add("攻城狮");
    3. // 强行插入
    4. CustomDictionary.insert("白富美", "nz 1024");
    5. // 删除词语(注释掉试试)
    6. //CustomDictionary.remove("攻城狮");
    7. System.out.println(CustomDictionary.add("单身狗", "nz 1024 n 1"));
    8. System.out.println("单身狗 : " + CustomDictionary.get("单身狗"));
    9. String text2 = "攻城狮逆袭单身狗,迎娶白富美,走上人生巅峰";
    10. String text23 = "王重阳和步惊云一起讨论盖聂的百步飞剑的诀窍! ";
    11. // AhoCorasickDoubleArrayTrie自动机分词
    12. final char[] charArray = text23.toCharArray();
    13. CustomDictionary.parseText(charArray, new AhoCorasickDoubleArrayTrie.IHit<CoreDictionary.Attribute>()
    14. {
    15. @Override
    16. public void hit(int begin, int end, CoreDictionary.Attribute value)
    17. {
    18. System.out.printf("[%d:%d]=%s %s ", begin, end, new String(charArray, begin, end - begin), value);
    19. }
    20. });

    结果:

    注意:

    • CustomDictionary是一份全局的用户自定义词典,可以随时增删,影响全部分词器。

    • 另外可以在任何分词器中关闭它。通过代码动态增删不会保存到词典文件。

     7.中国人名识别

    1. String[] testCase2 = new String[]{
    2. "签约仪式前,秦光荣、李纪恒、仇和等一同会见了参加签约的企业家。",
    3. "张浩和胡健康复员回家了",
    4. "编剧邵钧林和稽道青说",
    5. "这里有关天培的有关事迹",
    6. "龚学平等领导,邓颖超生前",
    7. };
    8. Segment segment2 = HanLP.newSegment().enableNameRecognize(true);
    9. for (String sentence : testCase2)
    10. {
    11. List<Term> termList = segment2.seg(sentence);
    12. System.out.println(termList);
    13. }

    结果:

    注意:目前分词器基本上都默认开启了中国人名识别,比如HanLP.segment()接口中使用的分词器等等,用户不必手动开启;

    8.关键字提取

    1. String content = "程序员(英文Programmer)是从事程序开发、维护的专业人员。一般将程序员分为程序设计人员和程序编码人员,但两者的界限并不非常清楚,特别是在中国。软件从业人员分为初级程序员、高级程序员、系统分析员和项目经理四大类。";
    2. List<String> keywordList = HanLP.extractKeyword(content, 5);
    3. System.out.println(keywordList);

    结果:

    注意:其内部采用TextRankKeyword(类谷歌的PageRank)实现,用户可以直接调用TextRankKeyword.getKeywordList(document, size)。

    9.简繁转换

    1. System.out.println(HanLP.convertToTraditionalChinese("用笔记本电脑写程序"));
    2. System.out.println(HanLP.convertToSimplifiedChinese("「以後等妳當上皇后,就能買士多啤梨慶祝了」"));

    结果:

    10.语义距离

    String[] wordArray2 = new String[]
    {
    "香蕉","苹果","白菜","水果","蔬菜"
    };
    for (String a : wordArray2){
      for (String b : wordArray2)
      {
        System.out.println(a + " " + b + " 之间的距离是 " + CoreSynonymDictionary.distance(a, b));
      }
    }

    结果:

    注意:

    • 说明

      • 设想的应用场景是搜索引擎对词义的理解,词与词并不只存在“同义词”与“非同义词”的关系,就算是同义词,它们之间的意义也是有微妙的差别的。

    • 算法

      • 为每个词分配一个语义ID,词与词的距离通过语义ID的差得到。语义ID通过《同义词词林扩展版》计算而来。

  • 相关阅读:
    存储过程使用收集
    网站伪静态技术(网页伪静态化)
    鼠标拖动层
    Oracle系统中用户权限的赋予,查看和管理(3)
    数据库中的锁查询及相关关系
    undo 管理
    grant 和 REVOKE权限
    Oracle系统中用户权限的赋予,查看和管理(2)
    了解数据库不同启动
    Oracle系统中用户权限的赋予,查看和管理(注意点)(4)
  • 原文地址:https://www.cnblogs.com/yszd/p/8664101.html
Copyright © 2011-2022 走看看