分词与词形还原

Python 中的 NLP 特征工程

Rounak Banik

Data Scientist

文本来源

  • 新闻报道
  • 推文
  • 评论
Python 中的 NLP 特征工程

让文本更易于机器处理

  • Dogs, dog
  • reduction, REDUCING, Reduce
  • don't, do not
  • won't, will not
Python 中的 NLP 特征工程

文本预处理技术

  • 转为小写
  • 移除首尾空白
  • 移除标点
  • 移除停用词
  • 展开缩写
  • 移除特殊字符(数字、表情等)
Python 中的 NLP 特征工程

分词

"I have a dog. His name is Hachi."

词元(Tokens):

["I", "have", "a", "dog", ".", "His", "name", "is", "Hachi", "."]
"Don't do this."

词元(Tokens):

["Do", "n't", "do", "this", "."]
Python 中的 NLP 特征工程

使用 spaCy 进行分词

import spacy

# Load the en_core_web_sm model nlp = spacy.load('en_core_web_sm')
# Initiliaze string string = "Hello! I don't know what I'm doing here."
# Create a Doc object doc = nlp(string)
# Generate list of tokens tokens = [token.text for token in doc] print(tokens)
['Hello','!','I','do',"n't",'know','what','I',"'m",'doing','here','.']
Python 中的 NLP 特征工程

词形还原

  • 将词转为其基本形式
    • reducing, reduces, reduced, reductionreduce
    • am, are, isbe
    • n'tnot
    • 'vehave
Python 中的 NLP 特征工程

使用 spaCy 进行词形还原

import spacy

# Load the en_core_web_sm model
nlp = spacy.load('en_core_web_sm')
# Initiliaze string
string = "Hello! I don't know what I'm doing here."
# Create a Doc object
doc = nlp(string)

# Generate list of lemmas lemmas = [token.lemma_ for token in doc] print(lemmas)
['hello','!','-PRON-','do','not','know','what','-PRON','be','do','here', '.']
Python 中的 NLP 特征工程

Vamos praticar!

Python 中的 NLP 特征工程

Preparing Video For Download...