1.中英文分词的区别:
中文:启发式 Heuristic
英文:机器学习、统计学习 HMM,CRF
2.社交网络语言的分词处理
1)用正则表达式将特殊符号归并起来
2)用re.complie将其编译一下
3)自定义tokenize,返回tokens_re.findall(s)
4) 自定义preprocess函数包括大小写的转换
3.英文变换的词型
不影响词性
影响词性
词形归一化:
stemming:词干提取,把不影响词性的小尾巴砍掉
Lcmmatization词形归一,即把各种变形归为一个形式(wordnet)
复数归一化Lemma
POS Tag 词性标注
Stopwords he,the等歧义太多,删掉
4.一条typical的文本预处理流水线
Raw_text
Tokenize POS Tag
Lemma/Stemming
stopwords
Word_List
5.文本预处理得到了一个干净的词库,接下来做特征工程,变成数字表达的词
情感分析
sentiment dictionary(打分机制)
like 1,good 2,bad -2, AFINN-111
(但是打分机制存在缺陷,所以通常用ML的情感分析,这里用贝叶斯分类法:
对句子进行处理,使其变成二值式,并给其一个label,然后放进模型里训练)
文本相似度
用元素频率Frequency 表示文本特征
import nltk
nltk
from nltk import FreqDist,tokenize
corpus='you are a beautiful girl ' \
' I am a handsome boy ' \
'you love me'
tokens = nltk.word_tokenize(corpus)
#print(tokens)
fdist = FreqDist(tokens)
fdist
#print(fdist['alove'])
stand_vec = fdist.most_common(50)
size = len(stand_vec)
#print(stand_vec,size)
#>>>[('you', 2), ('a', 2), ('are', 1), ('beautiful', 1), ('girl', 1), ('I', 1), ('am', 1),
#('handsome', 1), ('boy', 1), ('love', 1), ('me', 1)] 11
# 按照频率大小,记录单词的位置
def postion_lookup(V):
res = {}
counter = 0
for word in V:
res[word[0]] = counter
counter += 1
return res
stand_position_sict = postion_lookup(stand_vec)
stand_position_sict
sentence = 'this girl is my love girl'
freq_vector = [0] * size #建立一个与标准vector同样大小的0向量,用来记录原来那些单词出现的次数
tokens = nltk.word_tokenize(sentence)
for word in tokens:
try:
freq_vector[stand_position_sict[word]] += 1
except KeyError:
continue
print(freq_vector)
#>>>[0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0]
文本分类(大骨头,需要慢慢啃)
TF-IDF
TF(term Frequency):衡量term的频率
TF(t)=(t次数)/(term次数)
IDF(Inverse Document Frequency):衡量term的重要性
IDF(t)=log_e(文档总数/含有t的文档总数)
TF-IDF= TF * IDF