Search code examples
python-3.xnlpspacy

The example of NLP in my homework documentation is crashing


So I'm new to NLP and I was trying out the example code my documentation but its giving me errors.

Such as

"ModelsWarning: [W007] The model you're using has no word vectors loaded, so the result of the Token. similarity method will be based on the tagger, parser, and NER, which may not give useful similarity judgments. This may happen if you're using one of the small models, e.g. en_core_web_sm, which don't ship with word vectors and only use context-sensitive tensors. You can always add your own word vectors, or use one of the larger models instead if available"

and the second sentence that I typed exactly over from the documentation is giving this error

"can only concatenate str (not "numpy.float64") to str"

I could be just doing something silly but I'd appreciate some insights why is this happening

import spacy
nlp = spacy.load('en')

tokens = nlp('cat apple monkey banana')

for token1 in tokens:
    for token2 in tokens:
        print(token1.text, token2.text, token1.similarity(token2))


print("\nWorking With Sentences\n")

sentence_to_compare = 'Why is my cat on the car'

sentences = ["Where did my dog go",
             'hello, where is my car',
             'I\'ve lost my car in my car',
             'i\'d like my boat back',
             'I will name my dog Diana'
             ]

model_sentences = nlp(sentence_to_compare)

for sentence in sentences:
    similarity = nlp(sentence).similarity(model_sentences)
    print(sentence + "-" + similarity)

Solution

  • en_core_web_md used this instead of en