-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_ontology_based_hy.py
80 lines (66 loc) · 3.02 KB
/
auto_ontology_based_hy.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 15 22:19:37 2020
@author: samialsayadi
"""
from pattern.en import wordnet
from nltk.tokenize import word_tokenize
from pattern.text.en import singularize
from pattern.en import tenses, PAST, PL
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.corpus import wordnet as wn
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
Lem = WordNetLemmatizer()
data = open("reuters10_path.ont","w")
#list1=['associations', 'consulting', 'electricity', 'employment','fuel', 'hydrogen', 'management', 'oil', 'renewable', 'utilities']
list1=['acq', 'corn', 'crude', 'earn','grain', 'interest', 'money', 'ship','trade','wheat']
#list1=['acq','copper', 'earn','grain', 'interest','rapeseed','oilseed', 'money', 'ship','wheat']
#list1=['policy', 'transportation', 'agriculture', 'health', 'climate_change', 'air', 'atmosphere', 'water']
def glossdef(token):
tokens=[]
for term in token:
for token in wordnet.synsets(term):
token=token.gloss
token=tokens.append(token)
return tokens
def Removedub(list2):
final_list = []
for num in list2:
if num not in final_list:
final_list.append(num)
return final_list
def hy(token):
tokens=[]
for term in token:
for token in wn.synsets(term):
typesOfVehicles = list(set([w for s in token.closure(lambda s:s.hyponyms()) for w in s.lemma_names()]))
token=tokens.append(typesOfVehicles)
return tokens
def main():
for i in range(len(list1)):
for label in [list1[i]]:
for def_label in wn.synsets(label):
tkn=list(set([w for s in def_label.closure(lambda s:s.hyponyms()) for w in s.lemma_names()]))
for keywords in tkn:
# remove stop words
if keywords not in stop_words and len(keywords)>1:
#b. Convert all plural nouns form (irregular and regular) to singular noun form
#using pattern library in python.
token=singularize(keywords)
token = ''.join(i for i in token if not i.isdigit())
token=token.lower()
token=WordNetLemmatizer().lemmatize(token,'v')
# # adjective: pos='a'
token=WordNetLemmatizer().lemmatize(token,'a')
# adverb: pos='r'
token=WordNetLemmatizer().lemmatize(token,'r')
wordFromList1 = wn.synsets(label)
wordFromList2 = wn.synsets(token)
if wordFromList1 and wordFromList2:
degree = wordFromList1[0].path_similarity(wordFromList2[0])
if degree != None and degree > 0.01:
#save all ontologies in a txt file
print(label,'~',token,'=',degree,'.', file=data)
main()