How to calculate a distance in python on 2 words

Viewed 45

I have to extract a database and put it in a script in python. I did it for all name of database. Now I have to calculate the difference of letter between the name 1 and the name 2 and between the name 1 and the name 3 .... and between the name 2 with the name 3 .....

I did it for the moment. I show you what a little part about what I did who gives me a problem :

valeur = []

c.execute("""SELECT  NAME FROM AUTHORS""")
rows = c.fetchall()
for row in rows:
    valeur.append(row)
x=0
liste=[]
for x in range(len(valeur)):
    for i in range(x+1,len(valeur)):
        ed = nltk.edit_distance(valeur[x],valeur[i])
        dico={"titre":valeur[x],"titre_compare":valeur[i], "distance":ed}
        dico["titre"],"&&",(dico["titre_compare"]),"&&",(dico["distance"])
        print(dico["titre"],"&&",(dico["titre_compare"]),"&&",(dico["distance"]))

And the result is here


('Khālid Muḥammad ʻAlī al-Ḥājj',) && ('K. Hardono',) && 1
('Khālid Muḥammad ʻAlī al-Ḥājj',) && ('Nūr al-Dīn Ṣammūd',) && 1
('Khālid Muḥammad ʻAlī al-Ḥājj',) && ('Mawlūd Qāsim Nāyit Bulqāsim',) && 1
('Khālid Muḥammad ʻAlī al-Ḥājj',) && ('Ali Ak',) && 1
('Khālid Muḥammad ʻAlī al-Ḥājj',) && ('Nazamī Rāmajī',) && 1

My problems are why I have only 1 at the end of the comparaison between the 2 words and why I have a "," at the end of the words, It should be different and I don't unterstand why I got this. I did the same code of a different database with tittle of books and it worked.

Thanks you very much if you can help me !

3 Answers

why I have a "," at the end of the words

You have tuples that contain a single string, instead of just having the strings. The reason you have these is because you got them from a database query, which gives you each row in a tuple, no matter how many columns there are.

why I have only 1 at the end of the comparaison between the 2 words

This appears to have the same cause: nltk is being given the wrong sort of data. It would compare strings character by character; it compares tuples element by element. There is one element in each tuple, and they are different between the two tuples, so of course the result is 1.

your issues are caused by the fact cursor.fetchall() returns all the rows as a list of tuples, to solve this you can use:

valeur = [r[0] for r in rows]

@KarlKnechtel already explained why you get your output in that form

Thanks for your answer, So I dit it :

valeur=[]

req = "select * from AUTHORS"
result = c.execute(req)
print(type(result))
for row in result:
    #print(row[1])
    #print(row[2])
    valeur.append(row[0])

x=0
liste=[]
for x in range(len(valeur)):
    for i in range(x+1,len(valeur)):
        ed = nltk.edit_distance(valeur[x],valeur[i])
        dico={"titre":valeur[x],"titre_compare":valeur[i], "distance":ed}
        (dico["titre"],"&&",(dico["titre_compare"]),"&&",(dico["distance"]))
        double=dico["distance"]+dico["distance"]
        print(dico["titre"],"&&",(dico["titre_compare"]),"&&",(double))

and it works perfectely but I have not the name, I have it :

/authors/OL1022167A && /authors/OL1022282A && 6
/authors/OL1022167A && /authors/OL102236A && 4
/authors/OL1022167A && /authors/OL102364A && 6
/authors/OL1022167A && /authors/OL1023681A && 8

But me I want the name and here it's not the name. SO I tried to say
"select name from authors"

But when i said it , it doesn't work. So I tried other things but it always doesn't work. I would like to know How I can do like I did but only for name and not for key.

Thanks again and thanks for your previous answer !!!!

Related