Commit f1e46889 authored by Fize Jacques's avatar Fize Jacques
Browse files

Create Element, an object to recover elegantly data

DEBUG
parent fb8bfb1b
......@@ -99,6 +99,14 @@ class Element():
else:
return objectify({lang: self.data[lang] for lang in self.p_holder.label_fields})
@property
def name(self):
if not isinstance(self.p_holder.label_fields, tuple) and not isinstance(self.p_holder.label_fields, list):
return self.data[self.p_holder.label_fields]
else:
return objectify({lang: self.data[lang] for lang in self.p_holder.label_fields}).en
@property
def alias(self):
if not isinstance(self.p_holder.alias_fields, tuple) and not isinstance(self.p_holder.label_fields, list):
......@@ -121,3 +129,7 @@ class Element():
@property
def other(self):
return self.data
def __contains__(self, item):
return item in self.data
......@@ -12,6 +12,21 @@ geo_term={
"fr":open(__geo_res_path.rstrip("/")+"/geo_term_fr").read().lower().strip().split("\n"),
"en":open(__geo_res_path.rstrip("/")+"/geo_term_en").read().strip().split("\n")
}
def return_on_failure(value):
def decorate(f):
def applicator(*args, **kwargs):
try:
f(*args,**kwargs)
except:
return value
print('Error')
return applicator
return decorate
def parse_label2(label : str,lang):
if not lang in geo_term:
return parse_label(label)
......@@ -139,5 +154,5 @@ class Base():
df[self.score_field] = df[self.score_field].apply(lambda x: float(x))
else:
df[self.score_field] = df.apply(lambda x: 0)
df[self.score_field].fillna(-1, inplace=True)
df[self.score_field].fillna(0, inplace=True)
return df
\ No newline at end of file
# coding = utf-8
from .base import Base,parse_label,parse_label2
from .base import Base, parse_label2, return_on_failure
class Geodict(Base):
""""""
def __init__(self,es_client):
def __init__(self, es_client):
"""Constructor for Geodict"""
Base.__init__(self,es_client)
Base.__init__(self, es_client)
def get_by_label(self, label, lang, score=True, size=1):
query=self.qb.query(term=True,field=lang,value=label,sorted=score,sorted_by=self.score_field,sized=True,size=size)
return self.to_element(self.es_client.search("gazetteer","place",query))
query = self.qb.query(term=True, field=lang, value=label, sorted=score, sorted_by=self.score_field, sized=True,
size=size)
return self.to_element(self.es_client.search("gazetteer", "place", query))
def get_by_alias(self, alias, lang, score=True, size=1):
query = self.qb.query(term=True,nested=True,nested_field=lang,field="aliases", value=alias, sorted=score, sorted_by=self.score_field, sized=True,
query = self.qb.query(term=True, nested=True, nested_field=lang, field="aliases", value=alias, sorted=score,
sorted_by=self.score_field, sized=True,
size=size)
return self.to_element(self.es_client.search("gazetteer", "place", query))
def get_n_label_similar(self, label, lang, n, score=True):
query = self.qb.query(query_string=True, regexp=True,regexp_value=parse_label2(label,lang), field=lang, value=label, sorted=score,
query = self.qb.query(query_string=True, regexp=True, regexp_value=parse_label2(label, lang), field=lang,
value=label, sorted=score,
sorted_by=self.score_field, sized=True,
size=n)
return self.to_element(self.es_client.search("gazetteer", "place", query))
try:
return self.to_element(self.es_client.search("gazetteer", "place", query))
except :
return []
def get_n_alias_similar(self, alias, lang, n, score=True):
query = self.qb.query(query_string=True, nested=True, nested_field=lang, regexp=True,regexp_value=parse_label2(alias,lang),field="aliases", value=alias, sorted=score,
query = self.qb.query(query_string=True, nested=True, nested_field=lang, regexp=True,
regexp_value=parse_label2(alias, lang), field="aliases", value=alias, sorted=score,
sorted_by=self.score_field, sized=True,
size=n)
return self.to_element(self.es_client.search("gazetteer", "place", query))
try:
return self.to_element(self.es_client.search("gazetteer", "place", query))
except :
return []
def get_in_radius(self, lon, lat, unit="km",distance=10, score=True, size=1):
query = self.qb.query(match_all=True,in_radius=True,radius_size=distance,radius_unit=unit,radius_centroid=(lon,lat), sorted=score,
sorted_by=self.score_field, sized=True,geo_field="coord",
def get_in_radius(self, lon, lat, unit="km", distance=10, score=True, size=1):
query = self.qb.query(match_all=True, in_radius=True, radius_size=distance, radius_unit=unit,
radius_centroid=(lon, lat), sorted=score,
sorted_by=self.score_field, sized=True, geo_field="coord",
size=size)
return self.to_element(self.es_client.search("gazetteer", "place", query))
......@@ -42,13 +55,13 @@ class Geodict(Base):
size=1)
return self.to_element(self.es_client.search("gazetteer", "place", query))
def get_by_other_id(self,id,identifier="wikidata"):
if not identifier in ['wikidata','geonames']:
def get_by_other_id(self, id, identifier="wikidata"):
if not identifier in ['wikidata', 'geonames']:
raise Exception("Identfier type must be taken from the following items : 'wikidata' or 'geonames'")
if identifier == 'wikidata':
id_field="wikidataID"
id_field = "wikidataID"
else:
id_field="geonameID"
id_field = "geonameID"
query = self.qb.query(term=True, field=id_field, value=id, sized=True,
size=1)
return self.to_element(self.es_client.search("gazetteer", "place", query))
\ No newline at end of file
return self.to_element(self.es_client.search("gazetteer", "place", query))
# coding = utf-8
from .base import Base,parse_label,parse_label2
from .base import Base,parse_label2,return_on_failure
class Geonames(Base):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment