What I need to do is score the words in a sentence that are closest to a predefined concept. What I have managed to do is exactly that but with only one sentence. I am attaching an example where if the answer phrase contains the word house, it is awarded 4 points, little house 3 and so on. If there are no matches it is zero and if there are more than one they are added.
puntaje = {'casa':4, 'casita':3, 'casona':2, 'hogar':1}
respuesta = ['Esta', 'es', 'mi', 'casa', 'mi', 'hogar']
suma=[]
for k in respuesta:
if k in puntaje:
suma.append(puntaje[k])
print sum(suma)
In this example, it gives me 5 as a result , which is correct since in the sentence there are two matches with the dictionary: House that is worth 4 and Home that is worth 1 . Perfect.
My problem (apart from not knowing much about python) is that I can't do this with a list of multiple phrases (the idea is not to do another cycle, for
although I couldn't either). I have been trying with compressed lists but I have not been able to find the result.
What I need is to parse a list like this:
respuestas = [['Esta', 'es', 'mi', 'casa', 'mi', 'hogar'],
['Esta', 'es', 'una', 'casa', 'grande'],
['Esto', 'parece', 'una', 'casona'],
['Esto' 'es' 'un' 'departamento'],
[...etc]]
And the expected result would be the following:
{'Esta es mi casa mi hogar':5,
'Esta es una casa grande':4,
'Esto parece una casona':2,
'Esto es un departamento':0}
You will need two
for
but it can be done in a single statement if you want the result to be that (a dictionary with the phrase as the key and the punctuation as the value). We simply use list comprehensions and the methodjoin
for strings:Another option is to use the method
count()
to count the occurrences of each dictionary key in the lists:Both ways can be done without using list comprehensions. The first code can be equivalent to:
And the second to:
And thanks to the 'magic' of Python in all four cases it comes out:
Note : All code is valid for Python 2.7 and Python3.x
A bit simpler than @FJSevilla's first answer using
get
the dictionaries method: