清华大学张钹院士做了一场题为《AI科学突破的前夜,教授们应当看到什么?》提出:

AI未来的科学突破是建立一种同时基于知识和数据的AI系统。


HowNet

知网(HowNet)的构建秉承还原论思想,即所有词语的含义可以由更小的语义单位构成,而这种语义单位被称为“义原”(Sememe),即最基本的、不宜再分割的最小语义单位。知网构建了包含 2000 多个义原的精细的语义描述体系,并为十几万个汉语和英语词所代表的概念标注了义原。

清华大学人工智能研究院发布OpenHowNet,首次将知网的核心数据开源,并且开发了丰富的调用接口,实现义原查询、基于义原的词相似度计算等功能。整体包含有 229,767 个中英文词条, 35,202 个概念以及 2,196 个义原。

获取义原个数
print(len(hownet.get_all_sememes()))
2187
获取词语义原

获取词语苹果的义原:

import OpenHowNet

hownet = OpenHowNet.HowNetDict()
result_list = hownet.get("苹果")
print(len(result_list))
print(result_list[0])

输出:

6
{
  "Def": "{computer|电脑:modifier={PatternValue|样式值:CoEvent={able|能:scope={bring|携带:patient={$}}}}{SpeBrand|特定牌子}}",
  "en_grammar": "noun",
  "ch_grammar": "noun",
  "No": "127151",
  "syn": [
    {
      "id": "004024",
      "text": "IBM"
    },
    {
      "id": "041684",
      "text": "戴尔"
    },
    {
      "id": "049006",
      "text": "东芝"
    },
    {
      "id": "106795",
      "text": "联想"
    },
    {
      "id": "156029",
      "text": "索尼"
    },
    {
      "id": "004203",
      "text": "iPad"
    },
    {
      "id": "019457",
      "text": "笔记本"
    },
    {
      "id": "019458",
      "text": "笔记本电脑"
    },
    {
      "id": "019459",
      "text": "笔记本电脑"
    },
    {
      "id": "019460",
      "text": "笔记本电脑"
    },
    {
      "id": "019461",
      "text": "笔记本电脑"
    },
    {
      "id": "019463",
      "text": "笔记簿电脑"
    },
    {
      "id": "019464",
      "text": "笔记簿电脑"
    },
    {
      "id": "020567",
      "text": "便携式电脑"
    },
    {
      "id": "020568",
      "text": "便携式计算机"
    },
    {
      "id": "020569",
      "text": "便携式计算机"
    },
    {
      "id": "127224",
      "text": "平板电脑"
    },
    {
      "id": "127225",
      "text": "平板电脑"
    },
    {
      "id": "172264",
      "text": "膝上型电脑"
    },
    {
      "id": "172265",
      "text": "膝上型电脑"
    }
  ],
  "ch_word": "苹果",
  "en_word": "apple"
}

共6个义原,这里只列出作为作为电脑的这个义原。其中每个“xx|yy”代表一个义原,“|”左边为英文右边为中文;义原之间还被标注了复杂的语义关系,如host、modifier、belong等,从而能够精确地表示词语的语义信息。此外,还包括同义词信息。

可视化:

hownet.visualize_sememe_trees("苹果", K=2)
Find 6 result(s)
Display #0 sememe tree
[sense]苹果
└── [None]computer|电脑
    ├── [modifier]PatternValue|样式值
    │   └── [CoEvent]able|能
    │       └── [scope]bring|携带
    │           └── [patient]$
    └── [patient]SpeBrand|特定牌子
Display #1 sememe tree
[sense]苹果
└── [None]fruit|水果

K=2 表示列出两个义原。

获取英文单词的义原,结果有点奇怪,同义词还是中文,例如:

hownet = OpenHowNet.HowNetDict()
result_list = hownet.get("finally", language="en")
print(len(result_list))
print(result_list[0])
10
{
  "Def": "{TimingValue|时间特性值:TimeFeature={ending|末}}",
  "en_grammar": "adv",
  "ch_grammar": "adv",
  "No": "043966",
  "syn": [
    {
      "id": "037833",
      "text": "从头"
    },
    {
      "id": "037834",
      "text": "从头"
    },
    {
      "id": "037861",
      "text": "从小"
    },
    {
      "id": "037877",
      "text": "从一开始"
    },
    {
      "id": "037878",
      "text": "从一开始"
    },
    {
      "id": "039900",
      "text": "打小儿"
    },
    {
      "id": "043988",
      "text": "到了儿"
    },
    {
      "id": "043989",
      "text": "到了儿"
    },
    {
      "id": "044015",
      "text": "到头"
    },
    {
      "id": "044016",
      "text": "到头"
    },
    {
      "id": "044018",
      "text": "到头来"
    },
    {
      "id": "044019",
      "text": "到头来"
    },
    {
      "id": "076000",
      "text": "后冷战"
    },
    {
      "id": "078235",
      "text": "环比"
    },
    {
      "id": "099626",
      "text": "可算"
    },
    {
      "id": "099627",
      "text": "可算"
    },
    {
      "id": "099628",
      "text": "可算"
    },
    {
      "id": "099629",
      "text": "可算"
    },
    {
      "id": "108348",
      "text": "临场"
    },
    {
      "id": "108352",
      "text": "临床"
    }
  ],
  "ch_word": "到底",
  "en_word": "finally"
}

显示有10个义原,但是每个的定义Def也是一样的。

获取K个最接近输入词的词
ownet_dict_advanced = OpenHowNet.HowNetDict(use_sim=True)
hownet.initialize_sememe_similarity_calculation()
# 初始化耗时比较长
query_result = hownet_dict_advanced.get_nearest_words_via_sememes("苹果", 20)
example = query_result[0]
print("word_name:", example["word"])
print("id:", example["id"])
print("synset and corresonding word&id&score:")
for item in example["synset"]:
    print(item)

输出:

word_name: 苹果
id: 127151
synset and corresonding word&id&score:
{'id': 4024, 'word': 'IBM', 'score': 1.0}
{'id': 41684, 'word': '戴尔', 'score': 1.0}
{'id': 49006, 'word': '东芝', 'score': 1.0}
{'id': 106795, 'word': '联想', 'score': 1.0}
{'id': 156029, 'word': '索尼', 'score': 1.0}
{'id': 4203, 'word': 'iPad', 'score': 0.865}
{'id': 19457, 'word': '笔记本', 'score': 0.865}
{'id': 19458, 'word': '笔记本电脑', 'score': 0.865}
{'id': 19459, 'word': '笔记本电脑', 'score': 0.865}
{'id': 19460, 'word': '笔记本电脑', 'score': 0.865}
{'id': 19461, 'word': '笔记本电脑', 'score': 0.865}
{'id': 19463, 'word': '笔记簿电脑', 'score': 0.865}
{'id': 19464, 'word': '笔记簿电脑', 'score': 0.865}
{'id': 20567, 'word': '便携式电脑', 'score': 0.865}
{'id': 20568, 'word': '便携式计算机', 'score': 0.865}
{'id': 20569, 'word': '便携式计算机', 'score': 0.865}
{'id': 127224, 'word': '平板电脑', 'score': 0.865}
{'id': 127225, 'word': '平板电脑', 'score': 0.865}
{'id': 172264, 'word': '膝上型电脑', 'score': 0.865}
{'id': 172265, 'word': '膝上型电脑', 'score': 0.865}
计算两个指定词的相似度
res = hownet_dict_advanced.calculate_word_similarity("苹果", "笔记本电脑")
print(res)
# 0.865

如果其中的任何一个词不在HowNet中,函数将返回0。

res = hownet_dict_advanced.calculate_word_similarity("苹果", "梨")
print(res)
# 0

OpenHowNet安装以及其他使用方法见文档


其他占个坑:

学术上:
https://zhuanlan.zhihu.com/p/32688983

以及如何用来计算情感倾向值

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐