anto_Judger.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Author: Yalei Meng yaleimeng@sina.com
  4. License: Created with VS Code, (C) Copyright 2022
  5. Description: 读取反义词典(暂无十分完善的反义词典),判断是否反义词对。
  6. Date: 2022-02-15 23:11:36
  7. LastEditTime: 2022-02-16 08:54:06
  8. FilePath: \Final_word_Similarity\fanyi\anto_Judger.py
  9. '''
  10. import os
  11. class AntonymJudger(object):
  12. def __init__(self):
  13. """
  14. 'code_word' 以编码为key,单词list为value的dict,一个编码有多个单词
  15. """
  16. self.fanyi = {}
  17. self.file = os.path.join(os.path.dirname(__file__), 'antonym.txt')
  18. self.read_fan()
  19. def read_fan(self):
  20. """
  21. 读入反义词库,
  22. 单词为key,反义词为value, 保存在self.vocab
  23. """
  24. with open(self.file, 'r', encoding='utf-8') as f:
  25. for line in f.readlines():
  26. res = line.strip().split('@')
  27. code = res[0] # 原始词
  28. word = res[1] # 反义词词
  29. if code in self.fanyi:
  30. self.fanyi[code].append(word) # 如果已存在,就添加到列表
  31. else:
  32. self.fanyi[code] = [word,] # 如果不存在,构造一个列表
  33. def is_anti_pair(self,w1,w2):
  34. if w1 in self.fanyi and w2 in self.fanyi[w1]:
  35. return True
  36. else:
  37. return False