在特定字符之后获取令牌的一部分

时间:2018-10-28 17:53:05

标签: python regex

我想在文本文件中获得令牌的一部分。到目前为止,我编写了以下代码:

from collections import Counter
import re

freq_dist = set()
words = re.findall(r'[\w+]+', open('output.txt').read())
freq_dist = Counter(words).most_common(10)

print(freq_dist)

我的output.txt如下:

Türkiye+Noun ,+Punc terörizm+Noun+Gen ve+Conj kitle+Noun imha+Noun silah+Noun+A3pl+P3sg+Gen küresel+Adj düzey+Noun+Loc olus+Verb+Caus+PastPart+P3sg tehdit+Noun+Gen boyut+Noun+P3sg karsi+Adj+P3sg+Loc ,+Punc tüm+Det ülke+Noun+A3pl+Gen yay+Verb+Pass+Inf2+Gen önle+Verb+Pass+Inf2+P3sg hedef+Noun+A3pl+P3sg+Acc paylas+Verb+PastPart+P3pl ,+Punc daha+Noun güven+Noun+With ve+Conj istikrar+Noun+With bir+Num dünya+Noun düzen+Noun+P3sg için+PostpPCGen birlik+Noun+Loc çaba+Noun göster+Verb+PastPart+P3pl bir+Num asama+Noun+Dat gel+Verb+Pass+Inf2+P3sg+Acc samimi+Adj ol+Verb+ByDoingSo arzula+Verb+Prog2+Cop .+Punc 
Ab+Noun ile+PostpPCNom gümrük+Noun Alan+Noun+P3sg+Loc+Rel kurumsal+Adj iliski+Noun+A3pl 
club+Noun toplanti+Noun+A3pl+P3sg 
Türkiye+Noun+Gen -+Punc At+Noun gümrük+Noun isbirlik+Noun+P3sg komite+Noun+P3sg ,+Punc Ankara+Noun Anlasma+Noun+P3sg+Gen 6+Num madde+Noun+P3sg uyar+Verb+When ortaklik+Noun rejim+Noun+P3sg+Gen uygula+Verb+Pass+Inf2+P3sg+Acc ve+Conj gelis+Verb+Inf2+P3sg+Acc sagla+Verb+Inf1 üzere+PostpPCNom ortaklik+Noun Konsey+Noun+P3sg+Gen 2+Num /+Punc 69+Num sayili+Adj karar+Noun+P3sg ile+Conj teknik+Noun komite+Noun mahiyet+Noun+P3sg+Loc kur+Verb+Pass+Narr+Cop .+Punc 
nispi+Adj 
nisbi+Adj 
görece+Adj+With 
izafi+Adj 
obur+Adj 

我想获得第一个+号之后的零件,并将它们以降序形式保存在列表中。例如,在Türkiye+ Noun中,我想获得+ Noun部分,或者在terörizm+ Noun + Gen中,我想获得Noun + gen,或者在isbirlik + Noun + P3sg中,我想要获得Noun + P3sg,在此之后,我想按它们列出它们的计数按降序排列,例如在文本中出现了+ Noun或+ Noun + gen的次数。

1 个答案:

答案 0 :(得分:1)

如何在空格上分割输入?

from collections import Counter

words = [word.split('+', 1)[1].strip() for word in open('output.txt').read().split(' ') if len(word)]
freq_dist = Counter(words).most_common(10)

print(freq_dist)

这会给你:

[('Noun', 16), ('Punc', 8), ('Adj', 8), ('Noun+P3sg', 6), ('Num', 5), ('Conj', 4), ('Noun+Gen', 3), ('Noun+P3sg+Gen', 3), ('Noun+Loc', 2), ('Verb+PastPart+P3pl', 2)]