-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordNet.py
executable file
·56 lines (43 loc) · 1.63 KB
/
WordNet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from subprocess import check_output, CalledProcessError
from pdb import set_trace as st
class WordNetHelper:
@staticmethod
def shell_command(args):
try:
res = check_output(args).decode()
except CalledProcessError as e:
res = e.output.decode()
for r in ['\r', '\n\n']:
res = res.replace(r, '')
return res.strip()
@staticmethod
def get_syn(word, t):
res = WordNetHelper.shell_command(['wn', word, '-syns{}'.format(t)])
res = [x[3::].replace(' =>', '').replace(' Also See-> ', '')
.replace('#1', '').replace('#2', '').replace('#3', '').replace('#4', '')
.replace('#5', '').replace('#6', '').replace('#7', '').replace('#8', '')
for x in res.split('Sense')]
del res[0]
res = '\n'.join(res).split('\n')
res2 = []
for i in list(range(0, len(res))):
if res[i] == '' or 'Derived from' in res[i]:
pass
else:
res2.append(res[i])
return res2
class WordNet:
default_types = ['n', 'v', 'a', 'r']
@staticmethod
def get_syn(word, types=default_types, most_freq=99):
s = []
for t in types:
syns = WordNetHelper.get_syn(word, t)
if len(syns) != 0:
s += syns[0:most_freq]
return s
@staticmethod
def get_freq(word, t):
res = WordNetHelper.shell_command(['wn', word, '-faml{}'.format(t)])
res = res[res.find('=') + 2:-1]
return int(res) if res != '' else 0