NLTK + NLTK自然言語処理や自然言語処理ベース+ NLTK自然言語ツールキット+ +インストール手順コーパス+単語+トークン化+質問語形day27 []データ分析の研究ノート

NLTKと自然言語処理の基礎

NLTK(自然言語ツールキット)

NTLK有名なPythonの自然言語処理ツールキットが、主なターゲットは、英語のプロセスです。NLTKは本がありますが、コーパスがあり、文書をサポートします。

  • NLPフィールドは、最も一般的にPythonライブラリを使用しました
  • オープンソースプロジェクト
  • 独自の分類、セグメンテーションおよびその他の機能
  • 強力なコミュニティのサポート
  • コーパス、言語の実際の使用は、実際に言語材料が登場しました
  • http://www.nltk.org/py-modindex.html

ます。http:NLTKホームページの詳細にはマック、LinuxとWindowsのNLTKの下にインストールする方法パッケージのほとんどをインストールする必要がなくなり、//nltk.org/install.html、推奨直接ダウンロードアナコンダ、NLTKのインストールが完了し、あなたがインポートすることができますNLTKテスト、何の問題だけでなく、ダウンロードNLTK関連コーパス公式のオファーが存在しない場合。

インストール手順:

  1. ダウンロードNLTKパッケージ pip install nltk

  2. ファイル名を指定して実行Pythonは、以下のコマンドを入力します。

     import nltk
     nltk.download()
    
  3. 以下のポップアップウィンドウ、次のことがすべてのパッケージをインストールすることをお勧めしますall

    [画像のダンプはチェーンが失敗し、発信局は、直接アップロード(IMG-iMoJDH5K-1579959367202)(... /画像/ nltk_install.png)ダウン画像を保存することが推奨され、セキュリティチェーン機構を有していてもよいです]

  4. テスト使用:

    [画像のダンプはチェーンが失敗し、発信局は、(IMG-l3mGoGb2-1579959367203)(... /画像/ nltk_test.png)直接アップロードダウン画像を保存することが推奨され、セキュリティチェーン機構を有していてもよいです]

コーパス

nltk.corpus
import nltk
from nltk.corpus import brown # 需要下载brown语料库
# 引用布朗大学的语料库

# 查看语料库包含的类别
print(brown.categories())

# 查看brown语料库
print('共有{}个句子'.format(len(brown.sents())))
print('共有{}个单词'.format(len(brown.words())))

結果:

['adventure', 'belles_lettres', 'editorial', 'fiction', 'government', 'hobbies', 'humor', 'learned', 'lore', 'mystery', 'news', 'religion', 'reviews', 'romance', 'science_fiction']

共有57340个句子
共有1161192个单词

分词(トークン化)

  • 文は、言語の意味論上の意味の単語に分割されました
  • 、英語の単語の違い:
    • 英語では、自然の区切り文字として、単語間のスペースであります
    • 中国は正式な区切りの単語は英語よりも複雑であるされていません
  • 口ごもる単語:のような中国の単語分割ツールは、 pip install jieba
  • セグメンテーション結果を取得した後、その後の処理は、英語での大きな違いではありません
# 导入jieba分词
import jieba

seg_list = jieba.cut("欢迎来到黑马程序员Python学科", cut_all=True)
print("全模式: " + "/ ".join(seg_list))  # 全模式

seg_list = jieba.cut("欢迎来到黑马程序员Python学科", cut_all=False)
print("精确模式: " + "/ ".join(seg_list))  # 精确模式

結果:

全模式: 欢迎/ 迎来/ 来到/ 黑马/ 程序/ 程序员/ Python/ 学科
精确模式: 欢迎/ 来到/ 黑马/ 程序员/ Python/ 学科

Wordのフォームの問題

  • 一見、見て、見ています
  • 精度は、コーパスを学習に影響します
  • Wordのフォームの正規化

1.ステミング(ステミング)

例:

# PorterStemmer
from nltk.stem.porter import PorterStemmer

porter_stemmer = PorterStemmer()
print(porter_stemmer.stem('looked'))
print(porter_stemmer.stem('looking'))

# 运行结果:
# look
# look

例:

# SnowballStemmer
from nltk.stem import SnowballStemmer

snowball_stemmer = SnowballStemmer('english')
print(snowball_stemmer.stem('looked'))
print(snowball_stemmer.stem('looking'))

# 运行结果:
# look
# look

例:

# LancasterStemmer
from nltk.stem.lancaster import LancasterStemmer

lancaster_stemmer = LancasterStemmer()
print(lancaster_stemmer.stem('looked'))
print(lancaster_stemmer.stem('looking'))

# 运行结果:
# look
# look

2.マージワード形式(見出し語処理)

  • このようINGとして、ステミング、ステミング、edは唯一の単語トランクを残して、削除しました

  • 見出し語処理、、、など午前として、フォームに単語の様々な活用のマージ、ワード形式をしているされてマージ - went->行く>は、

  • ステマでNLTK

    PorterStemmer、SnowballStemmer、LancasterStemmer

  • NLTK補題

    WordNetLemmatizer

  • 問題

    動詞行ってきました - > GO、GOは名詞行ってきました - >ウェント、コヴェント

  • スピーチの示す部分は、より正確に補題することができ

例:

from nltk.stem import WordNetLemmatizer 
# 需要下载wordnet语料库

wordnet_lematizer = WordNetLemmatizer()
print(wordnet_lematizer.lemmatize('cats'))
print(wordnet_lematizer.lemmatize('boxes'))
print(wordnet_lematizer.lemmatize('are'))
print(wordnet_lematizer.lemmatize('went'))

# 运行结果:
# cat
# box
# are
# went

例:

# 指明词性可以更准确地进行lemma
# lemmatize 默认为名词
print(wordnet_lematizer.lemmatize('are', pos='v'))
print(wordnet_lematizer.lemmatize('went', pos='v'))

# 运行结果:
# be
# go

3.音声タギング(品詞)

  • 音声タグ付けのNLTK

    nltk.word_tokenize()

例:

import nltk

words = nltk.word_tokenize('Python is a widely used programming language.')
print(nltk.pos_tag(words)) # 需要下载 averaged_perceptron_tagger

# 运行结果:
# [('Python', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('widely', 'RB'), ('used', 'VBN'), ('programming', 'NN'), ('language', 'NN'), ('.', '.')]

4.削除ストップワード

  • ストレージ・スペースを節約し、検索効率を向上させるために、NLPは、自動的に特定の単語やフレーズを除外します

  • ストップワードは、手動で生成された非自動化された形ストップリストに入っています

  • 分類

    以下のような言語での関数言葉は、あります...

    通常、好きな言葉の広い範囲を使用して、語彙、

  • 中国のストップリスト

    中国無効シソーラス

    HITストップリスト

    四川大学無効シソーラスの機械知能研究所

    ストップワードのBaiduのリスト

  • 他の言語のストップリスト

    http://www.ranks.nl/stopwords

  • 使用NLTK削除ストップワード

    stopwords.words()

例:

from nltk.corpus import stopwords # 需要下载stopwords

filtered_words = [word for word in words if word not in stopwords.words('english')]
print('原始词:', words)
print('去除停用词后:', filtered_words)

# 运行结果:
# 原始词: ['Python', 'is', 'a', 'widely', 'used', 'programming', 'language', '.']
# 去除停用词后: ['Python', 'widely', 'used', 'programming', 'language', '.']

前処理は、一般的なテキストを処理します

例:

import nltk
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords

# 原始文本
raw_text = 'Life is like a box of chocolates. You never know what you\'re gonna get.'

# 分词
raw_words = nltk.word_tokenize(raw_text)

# 词形归一化
wordnet_lematizer = WordNetLemmatizer()
words = [wordnet_lematizer.lemmatize(raw_word) for raw_word in raw_words]

# 去除停用词
filtered_words = [word for word in words if word not in stopwords.words('english')]

print('原始文本:', raw_text)
print('预处理结果:', filtered_words)

結果:

原始文本: Life is like a box of chocolates. You never know what you're gonna get.
预处理结果: ['Life', 'like', 'box', 'chocolate', '.', 'You', 'never', 'know', "'re", 'gon', 'na', 'get', '.']

ユースケース:

import nltk
from nltk.tokenize import WordPunctTokenizer

sent_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')  
paragraph = "The first time I heard that song was in Hawaii on radio.  I was just a kid, and loved it very much! What a fantastic song!"  

# 分句
sentences = sent_tokenizer.tokenize(paragraph) 
print(sentences)

sentence = "Are you old enough to remember Michael Jackson attending. the Grammys with Brooke Shields and Webster sat on his lap during the show?"  

# 分词
words = WordPunctTokenizer().tokenize(sentence.lower())  
print(words)

出力:

['The first time I heard that song was in Hawaii on radio.', 'I was just a kid, and loved it very much!', 'What a fantastic song!']

['are', 'you', 'old', 'enough', 'to', 'remember', 'michael', 'jackson', 'attending', '.', 'the', 'grammys', 'with', 'brooke', 'shields', 'and', 'webster', 'sat', 'on', 'his', 'lap', 'during', 'the', 'show', '?']
彼は192元の記事を発表 ウォン称賛56 ビュー10000 +

おすすめ

転載: blog.csdn.net/qq_35456045/article/details/104084955