I am trying to extract relevant address info form an string and discard the garbage. So this:
al domicilio social de la Compañía, Avenida de Burgos, 109 - 28050 Madrid (Indicar Asesoría Jurídica – Protección de Datos)
Should be:
Avenida de Burgos, 109 - 28050 Madrid
What i've done:
I am using stanza NER to find locations from text.
After that, I am using the indexes of the found entities to get the full address. For eg: If A Madrid (Spanish city) is found in text[120:128] i will extract the string text[60:101] to get the full address.
My current code is:
##
##STANZA NER FOR LOCATIONS
##
!pip install stanza
#Download the spanish model
import stanza
stanza.download('es')
#create and run the ner tagger
nlp = stanza.Pipeline(lang='es', processors='tokenize,ner')
text = 'al domicilio social de la Compañía, Avenida de Burgos, 109 - 28050 Madrid (Indicar Asesoría Jurídica – Protección de Datos) '
doc = nlp(text)
#print results of NER tagger
print([ent for ent in doc.ents if ent.type=="LOC"], sep='\n')
print(*[text[int(ent.start_char)-60:int(ent.end_char)+15] for ent in doc.ents if ent.type=="LOC"], sep='\n')
After this, in this particular case, which should be reproducible. I get the next address.
cilio social de la Compañía, Avenida de Burgos, 109 - 28050 Madrid (Indicar Aseso
Which contains extra "garbage" info --> " cilio social de la Compañía," and "(Indicar Aseso"
In the next part of the process,I am using the libpostal library to parse the address as it follows:
!pip install postal
from postal.parser import parse_address
parse_address('Avenida de Burgos, 109 - 28050 Madrid')
Which works reliably in most cases, but only with clean addresses.
[('avenida de burgos', 'road'),
('109', 'house_number'),
('28050', 'postcode'),
('madrid', 'city')]
So, to sum up, I am searching from another tecnique apart from regex to help me discard garbage info from addresses apart from regex. (Libraries which do this if they exist or a new NLP approach ... ) Thanks