Reputation: 5
i am computing the levenshtein distance between the sentences, and now i have a text with several sentences. I don't know how to write the for loop to generate the distance between each pair of the sentence.
sent = ['motrin 400-600 mg every 8 hour as need for pai . ', 'the depression : continue escitalopram ; assess need to change medication as an outpatient . ', 'Blood cltures from 11-30 grow KLEBSIELLA PNEUMONIAE and 12-01 grow KLEBSIELLA PNEUMONIAE and PROTEUS MIRABILIS both sensitive to the Meropenam which she have already be receive . ']
def similarity(sent):
feature_sim = []
for a,b in sent:
feature_sim[a,b] = pylev.levenshtein(a,b)
print (feature_sim)
Upvotes: 0
Views: 218
Reputation: 15045
Use a pair of nested for-loops.
Simplest version:
for a in sent:
for b in sent:
...
Skip identical pairs (Levenshtein distance would trivially be 0):
for a in sent:
for b in sent:
if a != b:
...
Avoid processing duplicate pairs (a, b
is the same as b, a
):
for i in range(0, len(sent)):
for j in range(i+1, len(sent)):
# a = sent[i], b = sent[j]
...
Problem:
feature_sim
is a list, which can only be indexed by integers, not strings or any other types.
Use a dictionary instead:
feature_sim = {}
for i in range(0, len(sent)):
for j in range(i+1, len(sent)):
feature_sim[(sent[i], sent[j])] = pylev.levenshtein(sent[i], sent[j])
Upvotes: 1