Stripped whitespace from citynames

This commit is contained in:
Computerboer
2024-01-07 15:06:57 +01:00
parent b834333f56
commit fb8ca5eec8
2 changed files with 15 additions and 12 deletions

View File

@@ -20,7 +20,7 @@ class Cache():
log('removing cacheobject ' + key) log('removing cacheobject ' + key)
del cache[key] del cache[key]
return None return None
log( 'returning cacheobject ' + key) # log( 'returning cacheobject ' + key)
return cacheobj.obj return cacheobj.obj
def add(key, obj): def add(key, obj):
@@ -36,7 +36,7 @@ class CacheObj:
self.time=datetime.now() self.time=datetime.now()
def isOlderThanHours(self, hours): def isOlderThanHours(self, hours):
log('checking time cacheobject ' + self.key + ': ' + str(self.time) + " < " + str(datetime.now() - timedelta(hours=hours))) # log('checking time cacheobject ' + self.key + ': ' + str(self.time) + " < " + str(datetime.now() - timedelta(hours=hours)))
return self.time < datetime.now() - timedelta(hours=hours) return self.time < datetime.now() - timedelta(hours=hours)

View File

@@ -1,9 +1,9 @@
import re import re
import os import os
from pathlib import Path from pathlib import Path
from cache import Cache from cache import Cache, FileCache
from models.location import Countrycode, GeonameLocation from models.location import Countrycode, GeonameLocation, JsonEncoder
from utils.helperutils import log;
def getLocationArray(countrycode: Countrycode): def getLocationArray(countrycode: Countrycode):
cachename = 'locations_' + countrycode cachename = 'locations_' + countrycode
@@ -23,26 +23,29 @@ def getLocationArray(countrycode: Countrycode):
data = line.split("\t") data = line.split("\t")
alternatenames = [] alternatenames = []
if data[3] != "": if data[3] != "":
alternatenames = data[3].lower().split(",") alternatenames = [d.strip() for d in data[3].lower().split(",")] #makes from comma seperated a lowercase array and strips leading and trailing white spaces
geoname = GeonameLocation(data[0], data[1].lower(), data[2].lower(), alternatenames, data[4], data[5], data[8], data[18]) geoname = GeonameLocation(data[0], data[1].strip().lower(), data[2].strip().lower(), alternatenames, data[4], data[5], data[8], data[18])
geonames.append(geoname) geonames.append(geoname)
Cache.add(cachename,geonames) Cache.add(cachename,geonames)
return geonames return geonames
def getGeoLocationByCity(city = "", countrycode: Countrycode = Countrycode.NL ): def getGeoLocationByCity(city = "", countrycode: Countrycode = Countrycode.NL ):
city = city.lower(); city = city.strip().lower(); #strips leading and trailing white spaces and makes it lowercase
cityname = city cityname = city
if(not "gemeente" in cityname): if(not "gemeente" in cityname):
cityname = "gemeente "+ cityname cityname = "gemeente " + cityname
geonames = getLocationArray(countrycode) geonames = getLocationArray(countrycode)
# log('cityname and city: ' + cityname + " , " + city)
#first tries name with 'gemeente as prefix' #first tries name with 'gemeente as prefix'
geo = list(filter(lambda g: g.name == cityname, geonames)) geo = list(filter(lambda g: g.name == cityname, geonames))
if(geo): geo = geo[0] if(geo): geo = geo[0]
#print('first try' + repr(geo)) # print('first try' + repr(geo))
if (geo): return geo; if (geo): return geo;
#also tries in the alternatenames #also tries in the alternatenames
geo = list(filter(lambda g: inAlternatenames(g.alternatenames, cityname), geonames)) geo = list(filter(lambda g: inAlternatenames(g.alternatenames, cityname), geonames))
@@ -63,11 +66,11 @@ def getGeoLocationByCity(city = "", countrycode: Countrycode = Countrycode.NL ):
#removes everything between () and then removes the leading and trailing spaces; #removes everything between () and then removes the leading and trailing spaces;
print('name before regex ' + city) log('name before regex ' + city)
#city = re.sub('/\([^()]*\)/g', '', city) #city = re.sub('/\([^()]*\)/g', '', city)
city = re.sub("[\(].*?[\)]", "", city) city = re.sub("[\(].*?[\)]", "", city)
city = city.strip(); city = city.strip();
print('name after regex ' + city) log('name after regex ' + city)
geo = list(filter(lambda g: g.name == city, geonames)) geo = list(filter(lambda g: g.name == city, geonames))
if(geo): geo = geo[0] if(geo): geo = geo[0]