-
Notifications
You must be signed in to change notification settings - Fork 2
/
Snetkit.py
185 lines (155 loc) · 6.41 KB
/
Snetkit.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
"""
For: UMR TETIS RESEARCH UNIT
Author: rodrique_kafando
"""
import sys
import pandas as pd
import nltk
import spacy
import json
from titlecase import titlecase
import src.params as params
import src.utils as utils
from src.disambiguate import Disambiguate as ds
#nlp_enL = spacy.load('en_core_web_trf')
######### geonames var
import geocoder
def delStrInStr(string: str, find: str, replace: str) -> str:
temp = string.rsplit(find)
return replace.join(temp).strip()
# extract spatial named entities from document
def spacySne(text):
utils.manageDir()
dc = params.nlp_model(text)
# only LOC and GPE lables are used
listOfSne = [str(ent) for ent in dc.ents if ent.label_ in ['LOC' ,'GPE']] # only consider GPE & LOC spatatial named entities
prefix = 'the '
# SneList = [string.lower()[string.lower().startswith(prefix) and len(prefix):].strip() for string in listOfSne]
SneList = [titlecase(delStrInStr(string=string.lower(), find=prefix, replace='')) for string in listOfSne]
SneList = [titlecase(st) for st in SneList]
return SneList
def getCandidates(listOfSne:list,lang:str):
"""
for each SNE extracted from the document, return its potential candidate
@listOfSne: list of sne extracted from the document
@lang: specify the language of the text
@SameNameAs: list of candidate that have the same name as the input @sne in the for loop
@NotAsSne: list of candidate that do not match exactly with the input @sne
"""
SameNameAs = {}
NotAsSne = {}
for sne in listOfSne:
# print('sne :', sne)
# geo = geocoder.geonames(sne, key=key, maxRows=1000,featureClass=['A','P'], lang =lang) #,country=['GB']
geo = geocoder.geonames(sne, key=params.key, maxRows=50,featureClass=['A','P'], lang =lang) #,country=['GB']
Candidates = list(set([(r.address, r.lat, r.lng, r.country_code ,r.feature_class, r.population) for r in geo]))
SneSet = list(set([sn[0] for sn in Candidates]))
if len(SneSet) == 1 and SneSet[0].lower()==sne.lower():
SameNameAs[sne] = Candidates
else:
Candidates = [tuple(sn) for sn in Candidates]
NotAsSne[sne] = Candidates
# print(SameNameAs)
return SameNameAs, NotAsSne
def longlistToJson(Llist, fil_name):
with open('./candidates/'+fil_name + '.json', 'a') as fout:
#with open('../defaultgeo/'+fil_anme + '.json', 'a') as fout:
json.dump(Llist , fout)
def getCandidFromCorpora(corpora:str, out_fname, lang='en'):
files_list = glob.glob(corpora)
List = []
doclist = []
Final_AllSneCandidateD = []
Final_NotAsSne = []
AllSneCandidateD = {}
for doc in files_list:
doclist.append(doc)
print(doc)
fnamesrc = Path(doc).stem
AllSneCandidateD[fnamesrc] = {}
doc_data = read_txt(doc)
SneList = spacySne(doc_data,params.nlp_enL)
SneList = list(set(SneList))
SameAsSne, NotAsSne = getCandidates(SneList,params.key,lang='en')
AllSneCandidateD[fnamesrc]['assne'] = SameAsSne
AllSneCandidateD[fnamesrc]['notassne'] = NotAsSne
List.append(AllSneCandidateD)
longlistToJson(List, out_fname)
return doclist
def dicToDf(Dic:dict):
df = pd.DataFrame(Dic.items())
columns=['name', 'lat', 'lng', 'Country Code', 'Type', 'Population']
df[columns] = pd.DataFrame(df[1].tolist(), index=df.index)
df = df[columns]
return df
"""
for each SNE extracted from the document, return its potential candidate
"""
def DefltCandidate(listOfSne:list,lang:str):
AllSneCandidateD = {}
for sne in listOfSne:
geo = geocoder.geonames(sne, key= params.key, lang =lang) #,country=['GB']
Candidates = list(set([(r.address, r.lat, r.lng, r.country_code ,r.feature_class, r.population) for r in geo]))
if len(Candidates)>0:
AllSneCandidateD[sne] = list(Candidates[0])
else:
pass
df = dicToDf(AllSneCandidateD)
return df
def getCandidFromInputText_(input_text, lang='en'):
SneList = spacySne(input_text)
SneList = list(set(SneList))
df = getDefltCandidate(SneList,lang)
return df
def getDefltCand(SneList, lang='en'):
#SneList = spacySne(input_text,nlp_enL)
SneList = list(set(SneList))
df = DefltCandidate(SneList,lang)
df.to_csv('./defaultgeocoding/defaultgeocoding.csv', index = None)
return df
def MltCandidates(listOfSne:list, maxRows:int, lang:str):
"""
for each SNE extracted from the document, return its potential candidate
@listOfSne: list of sne extracted from the document
@lang: specify the language of the text
@SameNameAs: list of candidate that have the same name as the input @sne in the for loop
@NotAsSne: list of candidate that do not match exactly with the input @sne
"""
SameNameAs = {}
NotAsSne = {}
for sne in listOfSne:
geo = geocoder.geonames(sne, key=params.key, maxRows=maxRows,featureClass=['A','P'], lang =lang) #,country=['GB']
Candidates = list(set([(r.address, r.lat, r.lng, r.country_code ,r.feature_class, r.population) for r in geo]))
SneSet = list(set([sn[0] for sn in Candidates]))
if len(SneSet) == 1 and SneSet[0].lower()==sne.lower():
SameNameAs[sne] = Candidates
else:
Candidates = [tuple(sn) for sn in Candidates]
NotAsSne[sne] = Candidates
# print(SameNameAs)
return SameNameAs, NotAsSne
def getMultiCand(SneList:str, out_fname, maxRows=5, lang='en'):
List = []
AllSneCandidateD = {}
AllSneCandidateD['doc'] = {}
#SameAsSne, NotAsSne = getCandidates_tobeexplored(key,SneList,lang='en')
SameAsSne, NotAsSne = MltCandidates(SneList, maxRows, lang)
AllSneCandidateD['doc']['assne'] = SameAsSne
AllSneCandidateD['doc']['notassne'] = NotAsSne
List.append(AllSneCandidateD)
longlistToJson(List, out_fname)
print('Data saved in ./Candidates')
# return doclist
def applyDisamb(candidates_file, version_list =['f','fa','fas']):
orig_stdout = sys.stdout
f = open('./logs/logs.txt', 'w')
sys.stdout = f
for v in version_list:
desambiguated_df, NonAmbigusSne, toBeDesambAgain = ds(candidates_file, v)
desambiguated_df.to_csv('./disambiguated/'+'disambiguated'+v+'.csv', index = None)
NonAmbigusSne.to_csv('./disambiguated/NonAmbigusSne_'+v+'.csv', index = None)
toBeDesambAgain.to_csv('./remaining/toBeDisambAgain_'+v+'.csv', index = None)
sys.stdout = orig_stdout
print("logs saved to ./logs")
f.close()