{"id":13572151,"url":"https://github.com/chatopera/Synonyms","last_synced_at":"2025-04-04T09:31:51.515Z","repository":{"id":37681107,"uuid":"104722806","full_name":"chatopera/Synonyms","owner":"chatopera","description":":herb: 中文近义词：聊天机器人，智能问答工具包","archived":false,"fork":false,"pushed_at":"2023-11-24T22:55:49.000Z","size":144112,"stargazers_count":5065,"open_issues_count":31,"forks_count":899,"subscribers_count":172,"default_branch":"master","last_synced_at":"2025-04-03T01:05:56.960Z","etag":null,"topics":["chatbot","nlp","synonyms"],"latest_commit_sha":null,"homepage":"https://bot.chatopera.com/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chatopera.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-09-25T08:23:35.000Z","updated_at":"2025-04-02T05:57:02.000Z","dependencies_parsed_at":"2022-07-13T18:20:57.739Z","dependency_job_id":"d99822c3-0bf7-4b3a-b67c-9eb1215c3646","html_url":"https://github.com/chatopera/Synonyms","commit_stats":{"total_commits":143,"total_committers":14,"mean_commits":"10.214285714285714","dds":0.4545454545454546,"last_synced_commit":"6826536ad953593cc6251724470068758f8fbcea"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chatopera%2FSynonyms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chatopera%2FSynonyms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chatopera%2FSynonyms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chatopera%2FSynonyms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chatopera","download_url":"https://codeload.github.com/chatopera/Synonyms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247153649,"owners_count":20892703,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatbot","nlp","synonyms"],"created_at":"2024-08-01T14:01:15.007Z","updated_at":"2025-04-04T09:31:51.475Z","avatar_url":"https://github.com/chatopera.png","language":"Python","funding_links":[],"categories":["Python","NLP语料和数据集","Other AI Tools"],"sub_categories":["其他_文本生成、文本对话"],"readme":"[![PyPI](https://img.shields.io/pypi/v/synonyms.svg)](https://pypi.python.org/pypi/synonyms) [![PyPI download month](https://img.shields.io/pypi/dm/synonyms.svg)](https://pypi.python.org/pypi/synonyms/) [![](https://img.shields.io/pypi/pyversions/synonyms.svg)](https://pypi.org/pypi/synonyms/) [![License](https://cdndownload2.chatopera.com/cskefu/licenses/chunsong1.0.svg)](https://www.cskefu.com/licenses/v1.html \"开源许可协议\") [![](https://img.shields.io/pypi/format/synonyms.svg)](https://pypi.org/pypi/synonyms/)\n\n# Synonyms\n\nChinese Synonyms for Natural Language Processing and Understanding.\n\n更好的中文近义词：聊天机器人、智能问答工具包。\n\n`synonyms`可以用于自然语言理解的很多任务：文本对齐，推荐算法，相似度计算，语义偏移，关键字提取，概念提取，自动摘要，搜索引擎等。\n\n为提供稳定、可靠、长期优化的服务，Synonyms 改为使用 [春松许可证, v1.0](https://www.cskefu.com/licenses/v1.html) 并针对机器学习模型的下载进行收费，详见[证书商店](https://store.chatopera.com/product/syns001)。之前的贡献者（突出贡献的代码贡献者），可与我们联系，讨论收费问题。-- [Chatopera Inc.](https://www.chatopera.com) @ Oct. 2023\n\n# Table of Content:\n\n- [Install](https://github.com/chatopera/Synonyms#welcome)\n- [Usage](https://github.com/chatopera/Synonyms#usage)\n- [Quick Get Start](https://github.com/chatopera/Synonyms#quick-get-start)\n- [Valuation](https://github.com/chatopera/Synonyms#valuation)\n- [Benchmark](https://github.com/chatopera/Synonyms#benchmark)\n- [Statement](https://github.com/chatopera/Synonyms#statement)\n- [References](https://github.com/chatopera/Synonyms#references)\n- [Frequently Asked Questions](https://github.com/chatopera/Synonyms#frequently-asked-questions-faq)\n- [License](https://github.com/chatopera/Synonyms#license)\n\n# Welcome\n\nFollow steps below to install and activate packages.\n\n## 1/3 Install Sourcecodes Package\n\n```bash\npip install -U synonyms\n```\n\n当前稳定版本 v3.x。\n\n## 2/3 Config license id\n\nSynonyms's machine learning model package(s) requires a License from [Chatopera License Store](https://store.chatopera.com/product/syns001), first purchase a License and get the `license id` from **Licenses** page on Chatopera License Store(`license id`：在证书商店，证书详情页，点击【复制证书标识】).\n\n![image](https://cdndownload2.chatopera.com/store/imgs/syn_order_post.jpg)\n\nSecondly, set environment variable in your terminal or shell scripts as below.\n\n* For Shell Users\n\ne.g. Shell, CMD Scripts on Linux, Windows, macOS.\n\n```bash\n# Linux / macOS\nexport SYNONYMS_DL_LICENSE=YOUR_LICENSE\n## e.g. if your license id is `FOOBAR`, run `export SYNONYMS_DL_LICENSE=FOOBAR`\n\n# Windows\n## 1/2 Command Prompt\nset SYNONYMS_DL_LICENSE=YOUR_LICENSE\n## 2/2 PowerShell\n$env:SYNONYMS_DL_LICENSE='YOUR_LICENSE'\n```\n\n* For Python Code Users\n\nJupyter Notebook, etc.\n\n```python\nimport os\nos.environ[\"SYNONYMS_DL_LICENSE\"] = \"YOUR_LICENSE\"\n_licenseid = os.environ.get(\"SYNONYMS_DL_LICENSE\", None)\nprint(\"SYNONYMS_DL_LICENSE=\", _licenseid)\n```\n\n![](./assets/screenshot_20231124180125.png)\n\n**提示：安装后初次使用会下载词向量文件，下载速度取决于网络情况。**\n\n## 3/3 Download Model Package\n\nLast, download the model package by command or script -\n\n```bash\npython -c \"import synonyms; synonyms.display('能量')\" # download word vectors file\n```\n\n![](./assets/3.gif)\n\n## Usage\n\n支持使用环境变量配置分词词表和 word2vec 词向量文件。\n\n| 环境变量                            | 描述                                                                                                                                                                                               |\n| ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| _SYNONYMS_WORD2VEC_BIN_MODEL_ZH_CN_ | 使用 word2vec 训练的词向量文件，二进制格式。                                                                                                                                                       |\n| _SYNONYMS_WORDSEG_DICT_             | 中文分词[**主字典**](https://github.com/fxsjy/jieba#%E5%BB%B6%E8%BF%9F%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6)，格式和使用[参考](https://github.com/fxsjy/jieba#%E8%BD%BD%E5%85%A5%E8%AF%8D%E5%85%B8) |\n| _SYNONYMS_DEBUG_                    | [\"TRUE\"\\|\"FALSE\"], 是否输出调试日志，设置为 “TRUE” 输出，默认为 “FALSE”                                                                                                                            |\n\n### synonyms#nearby(word [, size = 10])\n\n```python\nimport synonyms\nprint(\"人脸: \", synonyms.nearby(\"人脸\"))\nprint(\"识别: \", synonyms.nearby(\"识别\"))\nprint(\"NOT_EXIST: \", synonyms.nearby(\"NOT_EXIST\"))\n```\n\n`synonyms.nearby(WORD [,SIZE])`返回一个元组，元组中包含两项：`([nearby_words], [nearby_words_score])`，`nearby_words`是 WORD 的近义词们，也以 list 的方式存储，并且按照距离的长度由近及远排列，`nearby_words_score`是`nearby_words`中**对应位置**的词的距离的分数，分数在(0-1)区间内，越接近于 1，代表越相近；`SIZE` 是返回词汇数量，默认 10。比如:\n\n```python\nsynonyms.nearby(人脸, 10) = (\n    [\"图片\", \"图像\", \"通过观察\", \"数字图像\", \"几何图形\", \"脸部\", \"图象\", \"放大镜\", \"面孔\", \"Mii\"],\n    [0.597284, 0.580373, 0.568486, 0.535674, 0.531835, 0.530\n095, 0.525344, 0.524009, 0.523101, 0.516046])\n```\n\n在 OOV 的情况下，返回 `([], [])`，目前的字典大小: 435,729。\n\n### synonyms#compare(sen1, sen2 [, seg=True])\n\n两个句子的相似度比较\n\n```python\n    sen1 = \"发生历史性变革\"\n    sen2 = \"发生历史性变革\"\n    r = synonyms.compare(sen1, sen2, seg=True)\n```\n\n其中，参数 seg 表示 synonyms.compare 是否对 sen1 和 sen2 进行分词，默认为 True。返回值：[0-1]，并且越接近于 1 代表两个句子越相似。\n\n```python\n旗帜引领方向 vs 道路决定命运: 0.429\n旗帜引领方向 vs 旗帜指引道路: 0.93\n发生历史性变革 vs 发生历史性变革: 1.0\n```\n\n### synonyms#display(word [, size = 10])\n\n以友好的方式打印近义词，方便调试，`display(WORD [, SIZE])`调用了 `synonyms#nearby` 方法。\n\n```python\n\u003e\u003e\u003e synonyms.display(\"飞机\")\n'飞机'近义词：\n  1. 飞机:1.0\n  2. 直升机:0.8423391\n  3. 客机:0.8393003\n  4. 滑翔机:0.7872388\n  5. 军用飞机:0.7832081\n  6. 水上飞机:0.77857226\n  7. 运输机:0.7724742\n  8. 航机:0.7664748\n  9. 航空器:0.76592904\n  10. 民航机:0.74209654\n```\n\n`SIZE` 是打印词汇表的数量，默认 10。\n\n### synonyms#describe()\n\n打印当前包的描述信息：\n\n```\n\u003e\u003e\u003e synonyms.describe()\nVocab size in vector model: 435729\nmodel_path: /Users/hain/chatopera/Synonyms/synonyms/data/words.vector.gz\nversion: 3.18.0\n{'vocab_size': 435729, 'version': '3.18.0', 'model_path': '/chatopera/Synonyms/synonyms/data/words.vector.gz'}\n```\n\n### synonyms#v(word)\n\n获得一个词语的向量，该向量为 numpy 的 array，当该词语是未登录词时，抛出 KeyError 异常。\n\n```python\n\u003e\u003e\u003e synonyms.v(\"飞机\")\narray([-2.412167  ,  2.2628384 , -7.0214124 ,  3.9381874 ,  0.8219283 ,\n       -3.2809453 ,  3.8747153 , -5.217062  , -2.2786229 , -1.2572327 ],\n      dtype=float32)\n```\n\n### synonyms#sv(sentence, ignore=False)\n\n获得一个分词后句子的向量，向量以 BoW 方式组成\n\n```python\n    sentence: 句子是分词后通过空格联合起来\n    ignore: 是否忽略OOV，False时，随机生成一个向量\n```\n\n### synonyms#seg(sentence)\n\n中文分词\n\n```python\nsynonyms.seg(\"中文近义词工具包\")\n```\n\n分词结果，由两个 list 组成的元组，分别是单词和对应的词性。\n\n```python\n(['中文', '近义词', '工具包'], ['nz', 'n', 'n'])\n```\n\n**该分词不去停用词和标点。**\n\n### synonyms#keywords(sentence [, topK=5, withWeight=False])\n\n提取关键词，默认按照重要程度提取关键词。\n\n```\nkeywords = synonyms.keywords(\"9月15日以来，台积电、高通、三星等华为的重要合作伙伴，只要没有美国的相关许可证，都无法供应芯片给华为，而中芯国际等国产芯片企业，也因采用美国技术，而无法供货给华为。目前华为部分型号的手机产品出现货少的现象，若该形势持续下去，华为手机业务将遭受重创。\")\n```\n\n## Contribution\n\nGet more logs for debugging, set environment variable.\n\n```\nSYNONYMS_DEBUG=TRUE\n```\n\n## PCA\n\n以“人脸”为例主要成分分析：\n\n![](assets/1.png)\n\n## Quick Get Start\n\n```bash\n$ pip install -r Requirements.txt\n$ python demo.py\n```\n\n## Change logs\n\n更新情况[说明](./CHANGELOG.md)。\n\n## Voice of Users\n\n用户怎么说：\n\n\u003cimg src=\"https://github.com/chatopera/Synonyms/raw/master/assets/4.png\" width=\"600\"\u003e\n\n## Data\n\ndata is built based on [wikidata-corpus](https://github.com/Samurais/wikidata-corpus).\n\n## Valuation\n\n### 同义词词林\n\n《同义词词林》是梅家驹等人于 1983 年编纂而成，现在使用广泛的是哈工大社会计算与信息检索研究中心维护的《同义词词林扩展版》，它精细的将中文词汇划分成大类和小类，梳理了词汇间的关系，同义词词林扩展版包含词语 7 万余条，其中 3 万余条被以开放数据形式共享。\n\n### 知网, HowNet\n\nHowNet，也被称为知网，它并不只是一个语义字典，而是一个知识系统，词汇之间的关系是其一个基本使用场景。知网包含词语 8 余条。\n\n国际上对词语相似度算法的评价标准普遍采用 Miller\u0026Charles 发布的英语词对集的人工判定值。该词对集由十对高度相关、十对中度相关、十对低度相关共 30 个英语词对组成,然后让 38 个受试者对这 30 对进行语义相关度判断，最后取他们的平均值作为人工判定标准。然后不同近义词工具也对这些词汇进行相似度评分，与人工判定标准做比较，比如使用皮尔森相关系数。在中文领域，使用这个词表的翻译版进行中文近义词比较也是常用的办法。\n\n### 对比\n\nSynonyms 的词表容量是 435,729，下面选择一些在同义词词林、知网和 Synonyms 都存在的几个词，给出其近似度的对比：\n\n![](./assets/5.png)\n\n注：同义词林及知网数据、分数[来源](https://github.com/yaleimeng/Final_word_Similarity)。Synonyms 也在不断优化中，新的分数可能和上图不一致。\n\n更多[比对结果](./VALUATION.md)。\n\n## Used by\n\n[Github 关联用户列表](https://github.com/chatopera/Synonyms/network/dependents?package_id=UGFja2FnZS01MjY2NDc1Nw%3D%3D)\n\n![](./assets/6.png)\n\n## Benchmark\n\nTest with py3, MacBook Pro.\n\n```\npython benchmark.py\n```\n\n++++++++++ OS Name and version ++++++++++\n\nPlatform: Darwin\n\nKernel: 16.7.0\n\nArchitecture: ('64bit', '')\n\n++++++++++ CPU Cores ++++++++++\n\nCores: 4\n\nCPU Load: 60\n\n++++++++++ System Memory ++++++++++\n\nmeminfo 8GB\n\n`synonyms#nearby: 100000 loops, best of 3 epochs: 0.209 usec per loop`\n\n## Live Sharing\n\n[52nlp.cn](http://www.52nlp.cn/synonyms-%E4%B8%AD%E6%96%87%E8%BF%91%E4%B9%89%E8%AF%8D%E5%B7%A5%E5%85%B7%E5%8C%85)\n\n[机器之心](https://www.jiqizhixin.com/articles/2018-01-14-3)\n\n[线上分享实录: Synonyms 中文近义词工具包 @ 2018-02-07](http://gitbook.cn/gitchat/activity/5a563545a8b23d387720ccd5)\n\n## Statement\n\n[Synonyms](https://github.com/chatopera/Synonyms)发布证书 MIT。数据和程序可用于研究和商业产品，必须注明引用和地址，比如发布的任何媒体、期刊、杂志或博客等内容。\n\n```\n@online{Synonyms:hain2017,\n  author = {Hai Liang Wang, Hu Ying Xi},\n  title = {中文近义词工具包Synonyms},\n  year = 2017,\n  url = {https://github.com/chatopera/Synonyms},\n  urldate = {2017-09-27}\n}\n```\n\n# References\n\n[wikidata-corpus](https://github.com/Samurais/wikidata-corpus)\n\n[word2vec 原理推导与代码分析](http://www.hankcs.com/nlp/word2vec.html)\n\n# Frequently Asked Questions (FAQ)\n\n1. 是否支持添加单词到词表中？\n\n不支持，欲了解更多请看 [#5](https://github.com/chatopera/Synonyms/issues/5)\n\n2. 词向量的训练是用哪个工具？\n\nGoogle 发布的[word2vec](https://code.google.com/archive/p/word2vec/)，该库由 C 语言编写，内存使用效率高，训练速度快。gensim 可以加载 word2vec 输出的模型文件。\n\n3. 相似度计算的方法是什么？\n\n[详见 #64](https://github.com/chatopera/Synonyms/issues/64)\n\n4. [#118 词向量文件一直下载不下来？](https://github.com/chatopera/Synonyms/issues/118)\n\n# Authors\n\n[Hai Liang Wang](https://pre-angel.com/peoples/hailiang-wang/)\n\n[Hu Ying Xi](https://github.com/huyingxi)\n\n# 自然语言处理推荐入门\u0026工具书\n\n本书由 [Synonyms](https://github.com/chatopera/Synonyms) 作者参与著作。\n\n\u003cp align=\"center\"\u003e\n  \u003cb\u003e快速购书\u003ca href=\"https://item.jd.com/12479014.html\" target=\"_blank\"\u003e链接\u003c/a\u003e\u003c/b\u003e\u003cbr\u003e\n  \u003ca href=\"https://item.jd.com/12479014.html\" target=\"_blank\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/3538629/48657619-bcd24880-ea6e-11e8-8c4e-8bcb00761942.png\" width=\"400\"\u003e      \n  \u003c/a\u003e\n\u003c/p\u003e\n\n[《智能问答与深度学习》](https://item.jd.com/12479014.html) 这本书是服务于准备入门机器学习和自然语言处理的学生和软件工程师的，在理论上介绍了很多原理、算法，同时也提供很多示例程序增加实践性，这些程序被汇总到示例程序代码库，这些程序主要是帮助大家理解原理和算法的，欢迎大家下载和执行。代码库的地址是：\n\n[https://github.com/l11x0m7/book-of-qna-code](https://github.com/l11x0m7/book-of-qna-code)\n\n# Give credits to\n\n[Word2vec by Google](https://code.google.com/archive/p/word2vec/)\n\n[Wikimedia: 训练语料来源](https://dumps.wikimedia.org/)\n\n[gensim: word2vec.py](https://github.com/RaRe-Technologies/gensim)\n\n[SentenceSim: 相似度评测语料](https://github.com/fssqawj/SentenceSim/)\n\n[jieba: 中文分词](https://github.com/fxsjy/jieba)\n\n# License\n\n[Chunsong Public License, version 1.0](./LICENSE)\n\n# Project Sponsor\n\n## Chatopera 云服务\n\n[https://bot.chatopera.com/](https://bot.chatopera.com/)\n\n[Chatopera 云服务](https://bot.chatopera.com)是一站式实现聊天机器人的云服务，按接口调用次数计费。Chatopera 云服务是 [Chatopera 机器人平台](https://docs.chatopera.com/products/chatbot-platform/index.html)的软件即服务实例。在云计算基础上，Chatopera 云服务属于**聊天机器人即服务**的云服务。\n\nChatopera 机器人平台包括知识库、多轮对话、意图识别和语音识别等组件，标准化聊天机器人开发，支持企业 OA 智能问答、HR 智能问答、智能客服和网络营销等场景。企业 IT 部门、业务部门借助 Chatopera 云服务快速让聊天机器人上线！","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchatopera%2FSynonyms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchatopera%2FSynonyms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchatopera%2FSynonyms/lists"}