回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<pre><code>import re
import sys
stop=set(['biti','jesam','budem','sam','jesi','budeš','si','jesmo','budemo','smo','jeste','budete','ste','jesu','budu','su','bih','bijah','bjeh','bijaše','bi','bje','bješe','bijasmo','bismo','bjesmo','bijaste','biste','bjeste','bijahu','biste','bjeste','bijahu','bi','biše','bjehu','bješe','bio','bili','budimo','budite','bila','bilo','bile','ću','ćeš','će','ćemo','ćete','želim','želiš','želi','želimo','želite','žele','moram','moraš','mora','moramo','morate','moraju','trebam','trebaš','treba','trebamo','trebate','trebaju','mogu','možeš','može','možemo','možete'])
def istakniSlogotvornoR(niz):
return re.sub(r'(^|[^aeiou])r($|[^aeiou])',r'\1R\2',niz)
def imaSamoglasnik(niz):
if re.search(r'[aeiouR]',istakniSlogotvornoR(niz)) is None:
return False
else:
return True
def transformiraj(pojavnica):
for trazi,zamijeni in transformacije:
if pojavnica.endswith(trazi):
return pojavnica[:-len(trazi)]+zamijeni
return pojavnica
def korjenuj(pojavnica):
for pravilo in pravila:
dioba=pravilo.match(pojavnica)
if dioba is not None:
if imaSamoglasnik(dioba.group(1)) and len(dioba.group(1))>1:
return dioba.group(1)
return pojavnica
if __name__=='__main__':
if len(sys.argv)!=3:
print 'Usage: python Croatian_stemmer.py input_file output_file'
print 'input_file should be an utf8-encoded text file which is then tokenized, stemmed and written in the output_file in a tab-separated fashion.'
sys.exit(1)
output_file=open(sys.argv[2],'w')
pravila=[re.compile(r'^('+osnova+')('+nastavak+r')$') for osnova, nastavak in [e.decode('utf8').strip().split(' ') for e in open('rules.txt')]]
transformacije=[e.decode('utf8').strip().split('\t') for e in open('transformations.txt')]
for token in re.findall(r'\w+',open(sys.argv[1]).read().decode('utf8'),re.UNICODE):
if token.lower() in stop:
output_file.write((token+'\t'+token.lower()+'\n').encode('utf8'))
continue
output_file.write((token+'\t'+korjenuj(transformiraj(token.lower()))+'\n').encode('utf8'))
output_file.close()
</code></pre>
<p>我下载了克罗地亚语的词干分析器。现在我了解到它的调用/运行方式类似于“python input output”,但我需要它来对列表中的文本数据进行词干分析。我需要如何编辑代码才能在列表上运行它,而不需要将列表保存到文本文档中?你知道吗</p>