Tabinda
Tabinda

Reputation: 1

AttributeError: 'Word2Vec' object has no attribute 'wmdistance'

When I run my.py file containing the following code: The following error is generated: Traceback (most recent call last): File "Checking.py", line 34, in distance = model.wmdistance(sentance_a,sentance_b) AttributeError: 'Word2Vec' object has no attribute 'wmdistance'

 from time import time
    from nltk.tokenize import sent_tokenize,word_tokenize
    from nltk.corpus import stopwords
    start_nb = time()
    
    data = 'The different Modi TV host in prime minister  chat Jim Corbett meet the'
    sentences = [sent_tokenize(x.lower()) for x in data]
    #sentences = [[w for w in sentence if w not in stopwords.words("english")] for x in sentence]
    
    sentance_a = 'Modi has a chat with Bear Grylls and Jim Corbett'
    sentance_b ='The prime minister meet the TV host in a National Park'
    sentance_a = sentance_a.lower().split()
    sentance_b = sentance_b.lower().split()
    
    from nltk.corpus import stopwords
    from nltk import download
    download('stopwords')
    
    stop_words = stopwords.words('english')
    sentance_a = [w for w in sentance_a if w not in stop_words]
    sentance_b = [w for w in sentance_b if w not in stop_words]
    
    start = time()
    import os
    from gensim import models as gsm
    
    from gensim.models import Word2Vec
    bigram = gsm.phrases.Phrases(sentences)
    bigram = gsm.phrases.Phraser(bigram) 
    trigram = gsm.phrases.Phrases(bigram[sentences])
    trigram = gsm.phrases.Phraser(trigram)
        
    model = gsm.Word2Vec(trigram[bigram[sentences]], min_count=2, workers=3, sg=1)
    distance = model.wmdistance(sentance_a,sentance_b)
    print("It took: %.4f"%(time()-start))
    
    print(distance)

Upvotes: 0

Views: 2582

Answers (2)

CheshireCat
CheshireCat

Reputation: 176

As Rivers pointed out, in recent Gensim versions you'll have to use KeyedVectors. They can be accessed by model.wv which adds then up to

distance = model.wv.wmdistance(sentance_a,sentance_b)

Upvotes: 1

Rivers
Rivers

Reputation: 1923

With recent version of Gensim you have to use KeyedVectors:

from gensim import models
w2vec_model = models.KeyedVectors.load_word2vec_format('model', binary=True)

Source code reference: https://github.com/RaRe-Technologies/gensim/blob/develop/gensim/models/keyedvectors.py

Upvotes: 1

Related Questions