简体   繁体   中英

Fuzzy matching to find the index of a word in a sentence Python

I have the following sentence which is Catalan language:

en aquest sentit, la llei preveu que quan l'administració s'hagi abstingut d' actuar per sancionar una determinada conducta, com a conseqüència d'un procés penal, i aquest procediment acabi en sentència absolutòria, podrà iniciar o continuar el corresponent procediment administratiu.

After tokenizing the sentence and it becomes a list of words , I need to find the index of the word " actuar ", but indexof() is not going to work. Moreover, I don't know Catalan language at all, but I have seen cases like "l'" could also be preceding the word I need.

Is there any easy way to fix this?

You can try this:

>>> sen = "en aquest sentit, la llei preveu que quan l'administració s'hagi abstingut d'actuar per sancionar una determinada conducta, com a conseqüència d'un procés penal, i aquest procediment acabi en sentència absolutòria, podrà iniciar o continuar el corresponent procediment administratiu."
>>> tokens = sen.split()
>>> tokens
['en', 'aquest', 'sentit,', 'la', 'llei', 'preveu', 'que', 'quan', "l'administraci\xc3\xb3", "s'hagi", 'abstingut', "d'actuar", 'per', 'sancionar', 'una', 'determinada', 'conducta,', 'com', 'a', 'conseq\xc3\xbc\xc3\xa8ncia', "d'un", 'proc\xc3\xa9s', 'penal,', 'i', 'aquest', 'procediment', 'acabi', 'en', 'sent\xc3\xa8ncia', 'absolut\xc3\xb2ria,', 'podr\xc3\xa0', 'iniciar', 'o', 'continuar', 'el', 'corresponent', 'procediment', 'administratiu.']
>>> def get_index(tokens, substr):
...    for i, tk in enumerate(tokens):
...       if substr in tk: return i
... 
>>> get_index(tokens, "actuar")
11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM