正则表达式的相关应用
2018-03-24 19:35
197 查看
1.通过re.findall()方法找出所有匹配的指定的正则表达式。例如:找出以下示例词中的所有元音,并计数。>>> word='supercalifragilisticexpialidocious'
>>> re.findall(r'[aeiou]',word)
['u', 'e', 'a', 'i', 'a', 'i', 'i', 'i', 'e', 'i', 'a', 'i', 'o', 'i', 'o', 'u']
>>> len(re.findall(r'[aeiou]',word))
16
>>> 2.查找文本中两个或两个以上的元音序列,并确定他们的相对频率。wsj=sorted(set(nltk.corpus.treebank.words()))
... fd= nltk.FreqDist(vs for word in wsj for vs in re.findall(r'[aeiou]{2,}',word))
>>> fid.items()3.将正则表达式与条件频率分布结合起来。例子中,从罗托卡特语词汇中提取所有的辅音-元音序列,如ka和si,因为其都是成对出现的,所以可以用频率分布表来表示。>>> rotokas_words=nltk.corpus.toolbox.words('rotokas.dic')
>>> cvs=[cv for w in rotokas_words for cv in re.findall(r'[ptksvr][aeiou]',w)]
>>> cfd=nltk.conditionalFreqDist(cvs)
>>> cfd.tabulate()4.查找词干
apples和apple对比中,apple就是词干。写一个简单脚本来查询词干。
def stem(word):
for suffix in ['ing','ly','ed','ious','ies','ive','es','s','ment']:
if word.endswith(suffix):
return word[:-len(suffix)]
return None而如果利用正则表达式,此问题将会变得很简单re.findall(r'^(.*?)(ing|ly|ed|ious|ies|ive|es|s|ment)$',word)
5.词干提取器和归并器
nltk提供了
porter = nltk.PorterStemmer()
print(porter.stem('lying'))如果需要处理像women这样的词,需要词性归并器:WordNetLemmatizerwnl = nltk.WordNetLemmatizer()
print(wnl.lemmatize('women'))6.利用词干提取器实现索引文本(concordance)
利用到nltk.Index这个函数,
def __init__(self,stemmer,text):
self._text = text
self._stemmer = stemmer
self._index = nltk.Index((self._stem(word),i) for (i,word) in enumerate(text))
def _stem(self,word):
return self._stemmer.stem(word).lower()
def concordance(self,word,width =40):
key = self._stem(word)
wc = width/4 #words of context
for i in self._index[key]:
lcontext = ' '.join(self._text[int(i-wc):int(i)])
rcontext = ' '.join(self._text[int(i):int(i+wc)])
ldisplay = '%*s' % (width,lcontext[-width:])
rdisplay = '%-*s' % (width,rcontext[:width])
print(ldisplay,rdisplay)
porter = nltk.PorterStemmer()
grail = nltk.corpus.webtext.words('grail.txt')
text = IndexText(porter,grail)
text.concordance('lie')
>>> re.findall(r'[aeiou]',word)
['u', 'e', 'a', 'i', 'a', 'i', 'i', 'i', 'e', 'i', 'a', 'i', 'o', 'i', 'o', 'u']
>>> len(re.findall(r'[aeiou]',word))
16
>>> 2.查找文本中两个或两个以上的元音序列,并确定他们的相对频率。wsj=sorted(set(nltk.corpus.treebank.words()))
... fd= nltk.FreqDist(vs for word in wsj for vs in re.findall(r'[aeiou]{2,}',word))
>>> fid.items()3.将正则表达式与条件频率分布结合起来。例子中,从罗托卡特语词汇中提取所有的辅音-元音序列,如ka和si,因为其都是成对出现的,所以可以用频率分布表来表示。>>> rotokas_words=nltk.corpus.toolbox.words('rotokas.dic')
>>> cvs=[cv for w in rotokas_words for cv in re.findall(r'[ptksvr][aeiou]',w)]
>>> cfd=nltk.conditionalFreqDist(cvs)
>>> cfd.tabulate()4.查找词干
apples和apple对比中,apple就是词干。写一个简单脚本来查询词干。
def stem(word):
for suffix in ['ing','ly','ed','ious','ies','ive','es','s','ment']:
if word.endswith(suffix):
return word[:-len(suffix)]
return None而如果利用正则表达式,此问题将会变得很简单re.findall(r'^(.*?)(ing|ly|ed|ious|ies|ive|es|s|ment)$',word)
5.词干提取器和归并器
nltk提供了
PorterStemmer和
LancasterStemmer两个词干提取器,Porter比较好,可以处理lying这样的单词。
porter = nltk.PorterStemmer()
print(porter.stem('lying'))如果需要处理像women这样的词,需要词性归并器:WordNetLemmatizerwnl = nltk.WordNetLemmatizer()
print(wnl.lemmatize('women'))6.利用词干提取器实现索引文本(concordance)
利用到nltk.Index这个函数,
nltk.Index((word , i) for (i,word) in enumerate(['a','b','a']))class IndexText:
def __init__(self,stemmer,text):
self._text = text
self._stemmer = stemmer
self._index = nltk.Index((self._stem(word),i) for (i,word) in enumerate(text))
def _stem(self,word):
return self._stemmer.stem(word).lower()
def concordance(self,word,width =40):
key = self._stem(word)
wc = width/4 #words of context
for i in self._index[key]:
lcontext = ' '.join(self._text[int(i-wc):int(i)])
rcontext = ' '.join(self._text[int(i):int(i+wc)])
ldisplay = '%*s' % (width,lcontext[-width:])
rdisplay = '%-*s' % (width,rcontext[:width])
print(ldisplay,rdisplay)
porter = nltk.PorterStemmer()
grail = nltk.corpus.webtext.words('grail.txt')
text = IndexText(porter,grail)
text.concordance('lie')
相关文章推荐
- javascript 中几个与正则表达式相关的应用
- 引用:正则表达式的相关应用
- 黑马程序员——基础学习(十)API中Arrays、Integer、正则表达式以及日期类的相关应用
- 正则表达式学习与应用及JAVA中相关函数
- grep与正则表达式02-相关练习题
- grep结合正则表达式应用
- [收藏] 正则表达式的入门与应用,1~4篇!grep/sed/perl/awk
- JavaScript的replace方法与正则表达式结合应用讲解
- Python 正则表达式应用【转载】
- 【记录】JS正则表达式的相关方法(正则学习笔记1)
- 关于java程序中的正则表达式的应用小解(1)
- javascript 正则表达式 g 的相关资料
- Java正则表达式基础应用复习
- 正则表达式应用:匹配email地址
- [收藏]JavaScript常用的正则表达式应用(ip,email,data,time)
- 正则表达式在STARLIMS中的应用总结
- ios系统自带正则表达式的应用
- 在.NET中读写INI文件 ——兼谈正则表达式的应用
- Java正则表达式应用总结
- 21.1 应用正则表达式对象RegExp