Search code examples
pythonmodulenlp

Properly installing nlp


My code:

import nlp
def tokenize_sentences(text):
    tokens = nlp(text)
    sentences = [sent.text for sent in nlp(text).sents]
    return sentences
text = "Some phrases that I use as a test. The context is not important. Test sentence.

sentences = tokenize_sentences(text)

The error is:

TypeError                                 Traceback (most recent call last)
<ipython-input-4-ec796e2a8070> in <module>
----> 1 sentences = tokenize_sentences(text)

<ipython-input-2-aa259d17fc09> in tokenize_sentences(text)
      1 def tokenize_sentences(text):
----> 2     tokens = nlp(text)
      3     sentences = [sent.text for sent in nlp(text).sents]
      4     #print(sentences)
      5     #for sent in tokens.sents:

TypeError: 'module' object is not callable

I tried installing again !pip install NLP-python, !pip install NLP, !pip install nlp. Then when I tried the following, 'NLP' wasn't found.

from NLP import NLP
nlp = NLP()

I know the error is somewhere in the import, but I don't know where.


Solution

  • Judging by the attributes in your code, it seems the library you are looking for is not NLP-python but spacy.

    pip3 install spacy
    python3 -m spacy download en_core_web_sm
    

    Then in your code:

    import spacy
    nlp = spacy.load("en_core_web_sm")