当前位置:   article > 正文

自然语言处理库——NLTK_nltk库主要用于处理什么

nltk库主要用于处理什么

        NLTK(www.nltk.org)是在处理预料库、分类文本、分析语言结构等多项操作中最长遇到的包。其收集的大量公开数据集、模型上提供了全面、易用的接口,涵盖了分词、词性标注(Part-Of-Speech tag, POS-tag)、命名实体识别(Named Entity Recognition, NER)、句法分析(Syntactic Parse)等各项 NLP 领域的功能。

目录

 1. 分词

(1)句子切分(断句)

(2)单词切分(分词)

2. 处理切词

(1)移除标点符号

(2)移除停用词

3. 词汇规范化(Lexicon Normalization)

(1)词形还原(lemmatization)

(2)词干提取(stem)

4. 词性标注

5. 获取近义词


NLTK模块及功能介绍:


 1. 分词

        文本是由段落(Paragraph)构成的,段落是由句子(Sentence)构成的,句子是由单词构成的。切词是文本分析的第一步,它把文本段落分解为较小的实体(如单词或句子),每一个实体叫做一个Token,Token是构成句子(sentence )的单词、是段落(paragraph)的句子。NLTK能够实现句子切分和单词切分两种功能。

(1)句子切分(断句)

       句子切分是指把段落切分成句子:

  1. from nltk.tokenize import sent_tokenize
  2. text="""Hello Mr. Smith, how are you doing today? The weather is great, and
  3. city is awesome.The sky is pinkish-blue. You shouldn't eat cardboard"""
  4. tokenized_text=sent_tokenize(text)
  5. print(tokenized_text)
  6. '''
  7. 结果:
  8. ['Hello Mr. Smith, how are you doing today?',
  9. 'The weather is great, and city is awesome.The sky is pinkish-blue.',
  10. "You shouldn't eat cardboard"]
  11. '''

(2)单词切分(分词)

    单词切分是把句子切分成单词

  1. import nltk
  2. sent = "I am almost dead this time"
  3. token = nltk.word_tokenize(sent)
  4. 结果:token['I','am','almost','dead','this','time']

2. 处理切词

对切词的处理,需要移除标点符号和移除停用词和词汇规范化。

(1)移除标点符号

       对每个切词调用该函数,移除字符串中的标点符号,string.punctuation包含了所有的标点符号,从切词中把这些标点符号替换为空格。

  1. # 方式一
  2. import string
  3. s = 'abc.'
  4. s = s.translate(str.maketrans(string.punctuation, " "*len(string.punctuation))) # abc
  5. # 方式二
  6. english_punctuations = [',', '.', ':', ';', '?', '(', ')', '[', ']', '&', '!', '*', '@', '#', '$', '%']
  7. text_list = [word for word in text_list if word not in english_punctuations]

(2)移除停用词

       停用词(stopword)是文本中的噪音单词,没有任何意义,常用的英语停用词,例如:is, am, are, this, a, an, the。NLTK的语料库中有一个停用词,用户必须从切词列表中把停用词去掉。

  1. nltk.download('stopwords')
  2. # Downloading package stopwords to C:\Users\Administrator\AppData\Roaming\nltk_data...Unzipping corpora\stopwords.zip.
  3. from nltk.corpus import stopwords
  4. stop_words = stopwords.words("english")
  5. text="""Hello Mr. Smith, how are you doing today? The weather is great, and city is awesome."""
  6. word_tokens = nltk.tokenize.word_tokenize(text.strip())
  7. filtered_word = [w for w in word_tokens if not w in stop_words]
  8. '''
  9. word_tokens:['Hello', 'Mr.', 'Smith', ',', 'how', 'are', 'you', 'doing', 'today', '?',
  10. 'The', 'weather', 'is', 'great', ',', 'and', 'city', 'is', 'awesome', '.']
  11. filtered_word:['Hello', 'Mr.', 'Smith', ',', 'today', '?', 'The', 'weather', 'great', ',', 'city', 'awesome', '.']
  12. '''

3. 词汇规范化(Lexicon Normalization)

词汇规范化是指把词的各种派生形式转换为词根,在NLTK中存在两种抽取词干的方法porter和wordnet。

(1)词形还原(lemmatization)

     利用上下文语境和词性来确定相关单词的变化形式,根据词性来获取相关的词根,也叫lemma,结果是真实的单词

(2)词干提取(stem)

     从单词中删除词缀并返回词干,可能不是真正的单词

  1. from nltk.stem.wordnet import WordNetLemmatizer # from nltk.stem import WordNetLemmatizer
  2. lem = WordNetLemmatizer() # 词形还原
  3. from nltk.stem.porter import PorterStemmer # from nltk.stem import PorterStemmer
  4. stem = PorterStemmer() # 词干提取
  5. word = "flying"
  6. print("Lemmatized Word:",lem.lemmatize(word,"v"))
  7. print("Stemmed Word:",stem.stem(word))
  8. '''
  9. Lemmatized Word: fly
  10. Stemmed Word: fli
  11. '''

4. 词性标注

      词性(POS)标记的主要目标是识别给定单词的语法组,POS标记查找句子内的关系,并为该单词分配相应的标签。

  1. sent = "Albert Einstein was born in Ulm, Germany in 1879."
  2. tokens = nltk.word_tokenize(sent)
  3. tags = nltk.pos_tag(tokens)
  4. '''
  5. [('Albert', 'NNP'), ('Einstein', 'NNP'), ('was', 'VBD'), ('born', 'VBN'),
  6. ('in', 'IN'), ('Ulm', 'NNP'), (',', ','), ('Germany', 'NNP'), ('in', 'IN'), ('1879', 'CD'), ('.', '.')]
  7. '''

5. 获取近义词

    查看一个单词的同义词集用synsets(); 它有一个参数pos,可以指定查找的词性。WordNet接口是面向语义的英语词典,类似于传统字典。它是NLTK语料库的一部分。

  1. import nltk
  2. nltk.download('wordnet') # Downloading package wordnet to C:\Users\Administrator\AppData\Roaming\nltk_data...Unzipping corpora\wordnet.zip.
  3. from nltk.corpus import wordnet
  4. word = wordnet.synsets('spectacular')
  5. print(word)
  6. # [Synset('spectacular.n.01'), Synset('dramatic.s.02'), Synset('spectacular.s.02'), Synset('outstanding.s.02')]
  7. print(word[0].definition())
  8. print(word[1].definition())
  9. print(word[2].definition())
  10. print(word[3].definition())
  11. '''
  12. a lavishly produced performance
  13. sensational in appearance or thrilling in effect
  14. characteristic of spectacles or drama
  15. having a quality that thrusts itself into attention
  16. '''

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/346335
推荐阅读
相关标签
  

闽ICP备14008679号