正确安装 nlp

Properly installing nlp

我的代码:

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)

错误是:

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

我再次尝试安装 !pip install NLP-python!pip install NLP!pip install nlp。然后,当我尝试以下操作时,找不到 'NLP'。

from NLP import NLP
nlp = NLP()

我知道错误是在导入的某个地方,但我不知道在哪里。

从您代码中的属性来看,您要查找的库似乎不是 NLP-python,而是 spacy

pip3 install spacy
python3 -m spacy download en_core_web_sm

然后在你的代码中:

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