自然语言处理综论
nlg-eval安装不上 git也不好使,直接下载到 site-packgaes执行python setup.py install
就行
字符串处理
去空格或特殊字符
str.strip() 去掉所有空格,rstrip() lstrip() 去掉两边的空格
str.strip(‘A’)去掉指定的字符
替换操作
查找操作
判断操作
分割合并操作
split()
split(‘ ‘,2)第一个是分割符号,2是maxsplitstr.split(' ')
.join()
<sep>.join(<iterable>)
' '.join(str)
以空格为分隔符合并
调取帮助文档
help(str)
正则表达式
选择匹配模式
pattern = re.compile(r'.')
分开所有的单个字符?pattern = re.comilpe(r'[abc]')
pattern = re.compile(r'[^a-zA-Z]')
根据字符集合
或方法pattern = re.compile(r'[a-zA-Z]|[0-9]')
\d
匹配数字 \D
匹配非数字
重复:* + ?
精确匹配和最小匹配:{m}
match 和 search
字符串的替换和修改
sub (rule, replace, target[,count])
subn
切片函数
命名组
NLTK
pip install nltk 之后 还要nltk.download() 需要手动下载包
分词
from nltk.tokenize import word_tokenize
from nltk.text import Text
tokens = nltk.tokenize(str)
tokens = [word.lower() for word in tokens]
t = Text(tokens) t.count t.index()
创建Text对象
停用词过滤
from nltk.corpus import stopwords
stopwords.raw(‘english’).replace(‘\n’,’ ‘)
使用set操作查看交集
词性标注
from nltk import pos_tag
tags = pos_tag(tokens) # 抽取词性
分块
命名实体识别
from nltk import ne_chunk
sentence = ‘abc de’
print(ne_chunk(pos_tag(wordtokenize(sentence))))
结巴分词器
Hierarchical Softmax
根据哈夫曼树,分层构造Softmax,常用的词频高的放在第一个