web-dev-qa-db-ja.com

spaCyを使用してテキストの前処理を行う方法は?

Pythonを使用して、spaCyでストップワードの削除、句読点の削除、ステミング、レンマ化などの前処理手順を実行する方法。

段落や文などのテキストデータがcsvファイルにあります。テキストのクリーニングをしたいです。

pandas dataframeにcsvを読み込んで例を挙げてください

3
RVK

これは、誰がこの質問に対する答えを探しているのかを助けるかもしれません。

import spacy #load spacy
nlp = spacy.load("en", disable=['parser', 'tagger', 'ner'])
stops = stopwords.words("english")



 def normalize(comment, lowercase, remove_stopwords):
    if lowercase:
        comment = comment.lower()
    comment = nlp(comment)
    lemmatized = list()
    for Word in comment:
        lemma = Word.lemma_.strip()
        if lemma:
            if not remove_stopwords or (remove_stopwords and lemma not in stops):
                lemmatized.append(lemma)
    return " ".join(lemmatized)


Data['Text_After_Clean'] = Data['Text'].apply(normalize, lowercase=True, remove_stopwords=True)
5
RVK

いくつかのコマンドで簡単に実行できます。また、spacyはステミングをサポートしていないことに注意してください。これを参照できます スレッド

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

# sample text
text = """Lorem Ipsum is simply dummy text of the printing and typesetting industry. \
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown \
printer took a galley of type and scrambled it to make a type specimen book. It has survived not \
only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. \
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, \
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration \
in some form, by injected humour, or randomised words which don't look even slightly believable. If you are \
going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the \
middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, \
making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined \
with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated \
Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc."""

# convert the text to a spacy document
document = nlp(text) # all spacy documents are tokenized. You can access them using document[i]
document[0:10] # = Lorem Ipsum is simply dummy text of the printing and

#the good thing about spacy is a lot of things like lemmatization etc are done when you convert them to a spacy document `using nlp(text)`. You can access sentences using document.sents
list(document.sents)[0]

# lemmatized words can be accessed using document[i].lemma_ and you can check 
# if a Word is a stopword by checking the `.is_stop` attribute of the Word.
# here I am extracting the lemmatized form of each Word provided they are not a stop Word
lemmas = [token.lemma_ for token in document if not token.is_stop]
5
Clock Slave

彼らのドキュメントを読んでください、ここに一例があります:

https://nicschrading.com/project/Intro-to-NLP-with-spaCy/

1
Steven Du