M
M
Marina18022021-11-07 16:16:47
Python
Marina1802, 2021-11-07 16:16:47

Doesn't read Python code, no errors, but doesn't do anything?

It is necessary to calculate the Levanshtein distance, I wrote the code but it is not readable and does not display anything

import nltk
nltk.download()

name = input('Please, put your surname, name, group and number of Lab:')
a = name
print(a)


from nltk.corpus import wordnet as wn
for synset in wn.synsets('journal', wn.NOUN):
    print(synset.name() + ':', synset.definition())

for synset in wn.synsets('blog', wn.NOUN):
    print(synset.name() + ':', synset.definition())

print(wn.synset('diary.n.01').hypernyms())
print(wn.synset('journal.n.02').hypernyms())
print(wn.synset('daybook.n.01').hypernyms())
print(wn.synset('journal.n.04').hypernyms())
print(wn.synset('journal.n.05').hypernyms())
print(wn.synset('web_log.n.01').hypernyms())

print(wn.synset('diary.n.01').hyponyms())
print(wn.synset('journal.n.02').hyponyms())
print(wn.synset('daybook.n.01').hyponyms())
print(wn.synset('journal.n.04').hyponyms())
print(wn.synset('journal.n.05').hyponyms())
print(wn.synset('web_log.n.01').hyponyms())


diary = wn.synset('diary.n.01')
journal = wn.synset('journal.n.02')
daybook = wn.synset('daybook.n.01')
web_log = wn.synset('web_log.n.01')

print('diary:', diary.min_depth())
print('journal:', journal.min_depth())
print('daybook:', daybook.min_depth())
print('web_log:', web_log.min_depth())

print(journal.lowest_common_hypernyms(diary))
print(journal.lowest_common_hypernyms(daybook))
print(journal.lowest_common_hypernyms(web_log))



print(journal.path_similarity(journal))
print(journal.path_similarity(diary))
print(journal.path_similarity(daybook))
print(journal.path_similarity(web_log))

#Wu-Palmer Similarity
print(journal.wup_similarity(daybook))
print(journal.wup_similarity(web_log))


print(journal.lch_similarity(daybook))
print(journal.lch_similarity(web_log))

def levenshtein(s1, s2):
    d = {}
    s1_length = len(s1)
   s2_length = len(s2)
    for i in range(-1, s1_length + 1):
        d[(i,-1)] = i + 1
    for j in range(-1, s2_length + 1):
        d[(-1, j)] = j + 1

    for i in range(s1_length):
        for j in range(s2_length):
            if s1[i] == s2[j]:
                cost = 0
           else:
                cost = 1
        d[(i, j)] = min(
            d[(i - 1, j)] + 1,
            d[(i, j - 1)] + 1,
            d[(i - 1, j - 1)] + cost,
        )
        return d[s1_length - 1, s2_length - 1]
    word1 = 'blog'
    word2 = 'journal'
    d1 = levenshtein(word1, word2)
    print(f"Result for'{word1}' & '{word2}' :", d1)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad, 2021-11-07
@Marina1802

You have a problem with indents, be careful

import nltk
nltk.download()

name = input('Please, put your surname, name, group and number of Lab:')
a = name
print(a)


from nltk.corpus import wordnet as wn
for synset in wn.synsets('journal', wn.NOUN):
    print(synset.name() + ':', synset.definition())

for synset in wn.synsets('blog', wn.NOUN):
    print(synset.name() + ':', synset.definition())

print(wn.synset('diary.n.01').hypernyms())
print(wn.synset('journal.n.02').hypernyms())
print(wn.synset('daybook.n.01').hypernyms())
print(wn.synset('journal.n.04').hypernyms())
print(wn.synset('journal.n.05').hypernyms())
print(wn.synset('web_log.n.01').hypernyms())

print(wn.synset('diary.n.01').hyponyms())
print(wn.synset('journal.n.02').hyponyms())
print(wn.synset('daybook.n.01').hyponyms())
print(wn.synset('journal.n.04').hyponyms())
print(wn.synset('journal.n.05').hyponyms())
print(wn.synset('web_log.n.01').hyponyms())


diary = wn.synset('diary.n.01')
journal = wn.synset('journal.n.02')
daybook = wn.synset('daybook.n.01')
web_log = wn.synset('web_log.n.01')

print('diary:', diary.min_depth())
print('journal:', journal.min_depth())
print('daybook:', daybook.min_depth())
print('web_log:', web_log.min_depth())

print(journal.lowest_common_hypernyms(diary))
print(journal.lowest_common_hypernyms(daybook))
print(journal.lowest_common_hypernyms(web_log))



print(journal.path_similarity(journal))
print(journal.path_similarity(diary))
print(journal.path_similarity(daybook))
print(journal.path_similarity(web_log))

#Wu-Palmer Similarity
print(journal.wup_similarity(daybook))
print(journal.wup_similarity(web_log))


print(journal.lch_similarity(daybook))
print(journal.lch_similarity(web_log))

def levenshtein(s1, s2):
    d = {}
    s1_length = len(s1)
    s2_length = len(s2)
    for i in range(-1, s1_length + 1):
        d[(i,-1)] = i + 1
    for j in range(-1, s2_length + 1):
        d[(-1, j)] = j + 1

    for i in range(s1_length):
        for j in range(s2_length):
            if s1[i] == s2[j]:
                cost = 0
            else:
                cost = 1
            d[(i, j)] = min(
                                   d[(i - 1, j)] + 1,
                                   d[(i, j - 1)] + 1,
                                   d[(i - 1, j - 1)] + cost,
                                  )
            if i and j and s1[i]==s2[j-1] and s1[i-1] == s2[j]:
                d[(i,j)] = min (d[(i,j)], d[i-2,j-2] + 1)
    return d[s1_length - 1, s2_length - 1]
word1 = 'blog'
word2 = 'journal'
d1 = levenshtein(word1, word2)
print(f"Result for '{word1}' & '{word2}' :", d1)

V
Vindicar, 2021-11-07
@Vindicar

You have in def levenshtein (s1, s2): some kind of mess is going on with indents.
Starting with word1 = 'blog', the code seems to be outside the function, but you placed it inside for some reason.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question