Macropodus
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,195 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# near-synonym
|
2 |
+
>>> near-synonym, 中文反义词/近义词/同义词(antonym/synonym)工具包.
|
3 |
+
|
4 |
+
# 一、安装
|
5 |
+
## 1.1 注意事项
|
6 |
+
默认不指定numpy版本(标准版numpy==1.20.4)
|
7 |
+
标准版本的依赖包详见 requirements-all.txt
|
8 |
+
|
9 |
+
## 1.2 通过PyPI安装
|
10 |
+
```
|
11 |
+
pip install near-synonym
|
12 |
+
使用镜像源, 如:
|
13 |
+
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple near-synonym
|
14 |
+
不带依赖安装, 之后缺什么包再补充什么
|
15 |
+
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple near-synonym --no-dependencies
|
16 |
+
```
|
17 |
+
|
18 |
+
## 1.3 模型文件
|
19 |
+
### 版本v0.2.0
|
20 |
+
- 新增一种生成反义词/近义词的算法, 构建提示词prompt, 基于BERT-MLM等继续训练, 类似beam_search方法, 生成反义词/近义词;
|
21 |
+
```
|
22 |
+
prompt: "xx"的反义词是"[MASK][MASK]"。
|
23 |
+
```
|
24 |
+
### 版本v0.1.0
|
25 |
+
- github项目源码自带模型文件只有1w+词向量, 完整模型文件在near_synonym/near_synonym_model,
|
26 |
+
- pip下载pypi包里边没有数据和模型(只有代码), 第一次加载使用huggface_hub下载, 大约为420M;
|
27 |
+
- 完整的词向量详见[huggingface](https://huggingface.co/)网站的[Macropodus/near_synonym_model](https://huggingface.co/Macropodus/near_synonym_model),
|
28 |
+
|
29 |
+
### 版本v0.0.3
|
30 |
+
- github项目源码自带模型文件只有1w+词向量, 完整模型文件在near_synonym/near_synonym_model,
|
31 |
+
- pip下载的软件包里边只有5w+词向量, 放在data目录下;
|
32 |
+
- 完整的词向量详见[huggingface](https://huggingface.co/)网站的[Macropodus/near_synonym_model](https://huggingface.co/Macropodus/near_synonym_model),
|
33 |
+
- 或完整的词向量详见百度网盘分享链接[https://pan.baidu.com/s/1lDSCtpr0r2hKrGrK8ZLlFQ](https://pan.baidu.com/s/1lDSCtpr0r2hKrGrK8ZLlFQ), 密码: ff0y
|
34 |
+
|
35 |
+
|
36 |
+
# 二、使用方式
|
37 |
+
|
38 |
+
## 2.1 快速使用, 反义词, 近义词, 相似度
|
39 |
+
```python3
|
40 |
+
import near_synonym
|
41 |
+
|
42 |
+
word = "喜欢"
|
43 |
+
word_antonyms = near_synonym.antonyms(word)
|
44 |
+
word_synonyms = near_synonym.synonyms(word)
|
45 |
+
print("反义词:")
|
46 |
+
print(word_antonyms)
|
47 |
+
print("近义词:")
|
48 |
+
print(word_synonyms)
|
49 |
+
"""
|
50 |
+
反义词:
|
51 |
+
[('讨厌', 0.6857), ('厌恶', 0.5406), ('憎恶', 0.485), ('不喜欢', 0.4079), ('冷漠', 0.4051)]
|
52 |
+
近义词:
|
53 |
+
[('喜爱', 0.8813), ('爱好', 0.8193), ('感兴趣', 0.7399), ('赞赏', 0.6849), ('倾向', 0.6137)]
|
54 |
+
"""
|
55 |
+
|
56 |
+
w1 = "桂林"
|
57 |
+
w2 = "柳州"
|
58 |
+
score = near_synonym.sim(w1, w2)
|
59 |
+
print(w1, w2, score)
|
60 |
+
"""
|
61 |
+
桂林 柳州 0.8947
|
62 |
+
"""
|
63 |
+
```
|
64 |
+
|
65 |
+
|
66 |
+
## 2.2 详细使用, 反义词, 相似度
|
67 |
+
```python3
|
68 |
+
import near_synonym
|
69 |
+
|
70 |
+
word = "喜欢"
|
71 |
+
word_antonyms = near_synonym.antonyms(word, topk=8, annk=256, annk_cpu=128, batch_size=32,
|
72 |
+
rate_ann=0.4, rate_sim=0.4, rate_len=0.2, rounded=4, is_debug=False)
|
73 |
+
print("反义词:")
|
74 |
+
print(word_antonyms)
|
75 |
+
|
76 |
+
word1, word2 = "桂林", "柳州"
|
77 |
+
score = near_synonym.sim(word1, word2, rate_ann=4, rate_sim=4, rate_len=2,
|
78 |
+
rounded=4, is_debug=False)
|
79 |
+
print(score)
|
80 |
+
|
81 |
+
# 当前版本速度很慢, 召回数量annk_cpu/annk可以调小
|
82 |
+
```
|
83 |
+
|
84 |
+
|
85 |
+
## 2.3 基于继续训练+promt的bert-mlm形式
|
86 |
+
```python3
|
87 |
+
import traceback
|
88 |
+
import os
|
89 |
+
os.environ["FLAG_MLM_ANTONYM"] = "1" # 必须先指定
|
90 |
+
|
91 |
+
from near_synonym import mlm_synonyms, mlm_antonyms
|
92 |
+
|
93 |
+
|
94 |
+
word = "喜欢"
|
95 |
+
word_antonyms = mlm_antonyms(word)
|
96 |
+
word_synonyms = mlm_synonyms(word)
|
97 |
+
print("反义词:")
|
98 |
+
print(word_antonyms)
|
99 |
+
print("近义词:")
|
100 |
+
print(word_synonyms)
|
101 |
+
|
102 |
+
"""
|
103 |
+
反义词:
|
104 |
+
[('厌恶', 0.77), ('讨厌', 0.72), ('憎恶', 0.56), ('反恶', 0.49), ('忌恶', 0.48), ('反厌', 0.46), ('厌烦', 0.46), ('反感', 0.45)]
|
105 |
+
近义词:
|
106 |
+
[('喜好', 0.75), ('喜爱', 0.64), ('爱好', 0.54), ('倾爱', 0.5), ('爱爱', 0.49), ('喜慕', 0.49), ('向好', 0.48), ('倾向', 0.48)]
|
107 |
+
"""
|
108 |
+
```
|
109 |
+
|
110 |
+
# 三、技术原理
|
111 |
+
## 3.1 技术详情
|
112 |
+
```
|
113 |
+
near-synonym, 中文反义词/近义词工具包.
|
114 |
+
流程一(neg_antonym): Word2vec -> ANN -> NLI -> Length
|
115 |
+
|
116 |
+
# Word2vec, 词向量, 使用skip-ngram的词向量;
|
117 |
+
# ANN, 近邻搜索, 使用annoy检索召回;
|
118 |
+
# NLI, 自然语言推断, 使用Roformer-sim的v2版本, 区分反义词/近义词;
|
119 |
+
# Length, 惩罚项, 词语的文本长度惩罚;
|
120 |
+
|
121 |
+
流程二(mlm_antonym): 构建提示词prompt等重新训练BERT类模型("引号等着重标注, 带句号, 不训练效果很差) -> BERT-MLM(第一个char取topk, 然后从左往右依次beam_search)
|
122 |
+
# 构建prompt:
|
123 |
+
- "xxx"的反义词是"[MASK][MASK][MASK]"。
|
124 |
+
- "xxx"的近义词是"[MASK][MASK][MASK]"。
|
125 |
+
# 训练MLM
|
126 |
+
# 一个char一个char地预测, 同beam_search
|
127 |
+
```
|
128 |
+
|
129 |
+
## 3.2 TODO
|
130 |
+
```
|
131 |
+
1. 推理加速, 训练小的NLI模型, 替换掉笨重且不太合适的roformer-sim-ft;【20240320已完成ERNIE-SIM,但转为ONNX为340M太大, 考虑浅层网络, 转第四点4.】
|
132 |
+
2. 使用大模型构建更多的NLI语料;
|
133 |
+
3. 使用大模型直接生成近义词, 同义词表, 用于前置索引+训练相似度;【20240407已完成】
|
134 |
+
4. 近义词反义词识别考虑使用经典NLP分类模型, text_cnn/text-rcnn, 基于字向量;【do-ing, 仿transformers写config/tokenizer/model, 方便余预训练模型集成】
|
135 |
+
5. word2vec召回不太行, 考虑直接使用大模型qwen1.5-0.5b生成;
|
136 |
+
```
|
137 |
+
|
138 |
+
## 3.3 其他实验
|
139 |
+
```
|
140 |
+
choice, prompt + bert-mlm;
|
141 |
+
choice, 如何处理数据/模型文件, 1.huggingface_hub("√") 2.gzip compress whitin 100M in pypi("×");
|
142 |
+
fail, 使用情感识别, 取得不同情感下的词语(失败, 例如可爱/漂亮同为积极情感);
|
143 |
+
fail, 使用NLI自然推理, 已有的语料是句子, 不是太适配;
|
144 |
+
```
|
145 |
+
|
146 |
+
# 四、对比
|
147 |
+
## 4.1 相似度比较
|
148 |
+
| 词语 | 2016词林改进版 | 知网hownet | Synonyms | near-synonym |
|
149 |
+
|--------------|-----------------|---------------|-----------------| ----------------- |
|
150 |
+
| "轿车","汽车" | 0.82 | 1.0 | 0.73 | 0.86 |
|
151 |
+
| "宝石","宝物" | 0.83 | 0.17 | 0.71 | 0.81 |
|
152 |
+
| "旅游","游历" | 1.0 | 1.0 | 0.59 | 0.72 |
|
153 |
+
| "男孩子","小伙子" | 0.81 | 1.0 | 0.88 | 0.83 |
|
154 |
+
| "海岸","海滨" | 0.94 | 1.0 | 0.68 | 0.9 |
|
155 |
+
| "庇护所","精神病院" | 0.96 | 0.58 | 0.64 | 0.62 |
|
156 |
+
| "魔术师","巫师" | 0.85 | 0.58 | 0.66 | 0.78 |
|
157 |
+
| "火炉","炉灶" | 1.0 | 1.0 | 0.81 | 0.83 |
|
158 |
+
| "中午","正午" | 0.98 | 0.58 | 0.85 | 0.88 |
|
159 |
+
| "食物","水果" | 0.35 | 0.14 | 0.74 | 0.82 |
|
160 |
+
| "鸟","公鸡" | 0.64 | 1.0 | 0.67 | 0.72 |
|
161 |
+
| "鸟","鹤" | 0.1 | 1.0 | 0.64 | 0.81 |
|
162 |
+
| "工具","器械" | 0.53 | 1.0 | 0.62 | 0.75 |
|
163 |
+
| "兄弟","和尚" | 0.37 | 0.80 | 0.59 | 0.7 |
|
164 |
+
| "起重机","器械" | 0.53 | 0.35 | 0.61 | 0.65 |
|
165 |
+
注:2016词林改进版/知网hownet/Synonyms数据、分数来源于[chatopera/Synonyms](https://github.com/chatopera/Synonyms)。同义词林及知网数据、分数的次级来源为[liuhuanyong/SentenceSimilarity](https://github.com/liuhuanyong/SentenceSimilarity)。
|
166 |
+
|
167 |
+
|
168 |
+
# 五、参考
|
169 |
+
- [https://ai.tencent.com/ailab/nlp/en/index.html](https://ai.tencent.com/ailab/nlp/en/index.html)
|
170 |
+
- [https://github.com/ZhuiyiTechnology/roformer-sim](https://github.com/ZhuiyiTechnology/roformer-sim)
|
171 |
+
- [https://github.com/liuhuanyong/SentenceSimilarity](https://github.com/liuhuanyong/SentenceSimilarity)
|
172 |
+
- [https://github.com/yongzhuo/Macropodus](https://github.com/yongzhuo/Macropodus)
|
173 |
+
- [https://github.com/chatopera/Synonyms](https://github.com/chatopera/Synonyms)
|
174 |
+
|
175 |
+
# 六、日志
|
176 |
+
```
|
177 |
+
2024.10.06, 完成prompt + bert-mlm形式生成反义词/近义词;
|
178 |
+
2024.04.14, 修改词向量计算方式(句子级别), 使得句向量的相似度/近义词/反义词更准确一些(依旧很不准, 待改进);
|
179 |
+
2024.04.13, 使用huggface_hub下载数据, 即near_synonym_model目录, 在[Macropodus/near_synonym_model](https://huggingface.co/Macropodus/near_synonym_model);
|
180 |
+
2024.04.07, qwen-7b-chat模型构建28w+词典的近义词/反义词表, 即ci_atmnonym_synonym.json, v0.1.0版本;
|
181 |
+
2024.03.14, 初始化near-synonym, v0.0.3版本;
|
182 |
+
```
|
183 |
+
|
184 |
+
# Reference
|
185 |
+
For citing this work, you can refer to the present GitHub project. For example, with BibTeX:
|
186 |
+
```
|
187 |
+
@misc{Macropodus,
|
188 |
+
howpublished = {https://github.com/yongzhuo/near-synonym},
|
189 |
+
title = {near-synonym},
|
190 |
+
author = {Yongzhuo Mo},
|
191 |
+
publisher = {GitHub},
|
192 |
+
year = {2024}
|
193 |
+
}
|
194 |
+
```
|
195 |
+
|