Python
 Computer >> コンピューター >  >> プログラミング >> Python

PythonでNLTKを使用したスト​​ップワードによる品詞タグ付け?


自然言語処理の背後にある主なアイデアは、機械が、テキストの意味の一部を理解したり、言おうとしたりするなど、少なくともある程度は人間の介入なしに、何らかの形の分析または処理を実行できることです。

コンピュータは、テキストを処理しようとしているときに、テキストから役に立たない、または重要性の低いデータ(単語)を除外する必要があります。 NLTKでは、役に立たない単語(データ)はストップワードと呼ばれます。

必要なライブラリのインストール

まず、nltkライブラリが必要です。ターミナルで、次のコマンドを実行するだけです。

$pip install nltk

したがって、これらのストップワードを削除して、データベース内のスペースを占有したり、貴重な処理時間を消費したりしないようにします。

ストップワードと見なすことができる独自の単語リストを作成できます。デフォルトでは、NLTKにはストップワードと見なされる単語がいくつか含まれています。NLTKコーパスを介して次のコマンドでアクセスできます。

>>> import nltk
>>> from nltk.corpus import stopwords

NLTKストップワードのリストは次のとおりです。

>>> set(stopwords.words('english'))
{'not', 'other', 'shan', "hadn't", 'she', 'did', 'through', 'and', 'does', "that'll", "weren't", 'your', "should've", "hasn't", 'myself', 'should', 'because', 'wasn', 'what', 'to', 'this', 'was', 'more', 'y', 'again', "needn't", 'into', 'above', 'themselves', 'd', "won't", 'during', 'haven', 'both', "shan't", 'their', 'on', 'hadn', 'up', 'once', 'its', 'against', 'before', 't', 'while', 'needn', 'doing', "don't", 'yourselves', 'until', 'is', 'all', 's', 'will', "you've", 'being', 'under', 'they', 'ours', 'wouldn', 'of', 'didn', 'below', 'just', 'ma', 'yours', "you'll", 'mightn', 'where', 'are', 'that', 'those', 'most', 'them', 'if', 'you', "shouldn't", 'off', 'for', 'her', 'such', 'now', 'than', 're', 'no', 'm', 'or', "aren't", 'further', 'here', "wasn't", 'after', "haven't", 'my', 'himself', 'at', 'had', 'yourself', 'by', 'weren', 'only', 'have', 'we', 'do', 'same', "isn't", 'herself', 'll', 'down', 'then', 'why', 'own', 'him', 'so', 'having', 'nor', 'isn', 'few', 'how', 'each', 'there', 'with', 'couldn', 'about', 'very', 'am', 'me', "didn't", "doesn't", 'which', "she's", 'doesn', 'were', 'he', 'in', "mightn't", 'when', 'our', 'who', 'his', "couldn't", 'the', "you'd", 'be', 'hers', 'hasn', 'between', 'it', 'mustn', 'but', 'out', 'can', "wouldn't", 'ourselves', 'whom', 'been', 'these', 'aren', 'over', 'itself', 'a', 'i', 'too', 'theirs', 'some', "you're", 'as', 'won', "it's", 'from', 'o', 'don', 'any', 've', 'ain', 'has', 'an', "mustn't", 'shouldn'}

以下は、ストップワードを使用してテキストからストップワードを削除する方法を説明する完全なプログラムです。

サンプルコード

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

example_sent = "Python is a powerful high-level, object-oriented programming language created by Guido van Rossum."\
"It has simple easy-to-use syntax, making it the perfect language for someone trying to learn computer programming for the first time."\
"This is a comprehensive guide on how to get started in Python, why you should learn it and how you can learn it. However, if you knowledge "\
"of other programming languages and want to quickly get started with Python."

stop_words = set(stopwords.words('english'))

word_tokens = word_tokenize(example_sent)

filtered_sentence = [w for w in word_tokens if not w in stop_words]

filtered_sentence = []

for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)

print(word_tokens)
print(filtered_sentence)

出力

テキスト出力:フィルターなし(ストップワードあり)

['Python', 'is', 'a', 'powerful', 'high-level', ',', 'object-oriented', 'programming', 'language', 'created', 'by', 'Guido', 'van', 'Rossum.It', 'has', 'simple', 'easy-to-use', 'syntax', ',', 'making', 'it', 'the', 'perfect', 'language', 'for', 'someone', 'trying', 'to', 'learn', 'computer', 'programming', 'for', 'the', 'first', 'time.This', 'is', 'a', 'comprehensive', 'guide', 'on', 'how', 'to', 'get', 'started', 'in', 'Python', ',', 'why', 'you', 'should', 'learn', 'it', 'and', 'how', 'you', 'can', 'learn', 'it', '.', 'However', ',', 'if', 'you', 'knowledge', 'of', 'other', 'programming', 'languages', 'and', 'want', 'to', 'quickly', 'get', 'started', 'with', 'Python', '.']

テキスト出力:フィルター付き(ストップワードの削除)

['Python', 'powerful', 'high-level', ',', 'object-oriented', 'programming', 'language', 'created', 'Guido', 'van', 'Rossum.It', 'simple', 'easy-to-use', 'syntax', ',', 'making', 'perfect', 'language', 'someone', 'trying', 'learn', 'computer', 'programming', 'first', 'time.This', 'comprehensive', 'guide', 'get', 'started', 'Python', ',', 'learn', 'learn', '.', 'However', ',', 'knowledge', 'programming', 'languages', 'want', 'quickly', 'get', 'started', 'Python', '.']

  1. Pythonを使用して最大の確率でパスを見つけるプログラム

    n個のノード(ノードには0から番号が付けられます)を持つ無向加重グラフがあるとします。このグラフは、エッジリストを使用して入力として与えられ、各エッジeについて、そのエッジ確率[e]を通過する成功の確率があります。開始ノードと終了ノードもあります。最初から最後まで成功の確率が最大のパスを見つけて、成功の確率を返す必要があります。パスが見つからない場合は、0を返します。 したがって、入力が次のような場合 ノード0から2へのパスが2つあるため、出力は0.24になります。1つは確率0.2、もう1つはノード1を経由するパスの確率は0.4 * 0.6 =0.24で、これが最大です。 これを解

  2. Pythonでpylabを使用してFigureをリモートで保存するにはどうすればよいですか?

    pyplotパッケージのsavefigメソッドを使用して、フィギュアの場所を指定することにより、フィギュアをリモートで保存できます。 ステップ 別のバックエンドを使用するには、matplotlib.use(Agg)メソッドを使用して設定します。 plot()メソッドを使用して線をプロットします。 savefig()メソッドを使用すると、ディレクトリを配置するだけで、画像をリモートで保存できます。 図を表示するには、plt.show()を使用します。 例 import matplotlib matplotlib.use('Agg') from mat