位于lis中的词干文本

2024-03-28 08:49:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

我下载了克罗地亚语的词干分析器。现在我了解到它的调用/运行方式类似于“python input output”,但我需要它来对列表中的文本数据进行词干分析。我需要如何编辑代码才能在列表上运行它,而不需要将列表保存到文本文档中?你知道吗


Tags: inretokenforoutputreturnifdef
2条回答

改变

    open(sys.argv[1]).read().decode('utf8') as your input list  

你的预期结果是:

  (token+'\t'+korjenuj(transformiraj(token.lower()))+'\n').encode('utf8')

这对你有用吗?只需在if __name__ == "__main__":之前添加这些函数

def fromListToList(wordlist):
    outputList = []
    global pravila, transformacije
    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 wordlist:
        if token.lower() in stop:
            outputList.append((token+'\t'+token.lower()+'\n').encode('utf8'))
            continue
        outputList.append((token+'\t'+korjenuj(transformiraj(token.lower()))+'\n').encode('utf8'))
    return outputList


def fromListToFile(wordlist, output_file):
    global pravila, transformacije
    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 wordlist:
        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()

然后可以在生成wordlist的文件中导入这些函数。你知道吗

注意:我添加了global pravila, transformacije,因为您正在更改其他函数中使用的函数中的这些列表,因此它们必须是global。你知道吗

相关问题 更多 >