[英]Create a frequency table that captures popular substrings within strings of a certain length - Python
我正在尝试对正在编译的斯瓦希里语料库进行频率分析。 目前,这是我所拥有的:
import os
import sys
from collections import Counter
import re
path = 'C:\Python27\corpus\\'
cnt = Counter()
listing = os.listdir(path)
for infile in listing:
print "Currently parsing: " + path + infile
corpus = open(path+infile, "r")
for lines in corpus:
for words in lines.split(' '):
if len(words) >= 2 and re.match("^[A-Za-z]*$", words):
words = words.strip()
cnt[words] += 1
print "Completed parsing: " + path + infile
#output = open(n + ".out", "w")
#print "current file is: " + infile
corpus.close()
#output.close()
for (counter, content) in enumerate(cnt.most_common(1000)):
print str(counter+1) + " " + str(content)
因此,该程序将遍历给定路径中的所有文件,读入每个文件的文本,并显示1000个最常用的单词。 问题是:斯瓦希里语是一种凝集性语言,这意味着在单词中添加了中缀,后缀和前缀,以传达诸如时态,因果关系,虚拟语气,介词等内容。
因此,像“ -fanya”这样的动词词根意思是“要做”,可能是nitakufanya-“我要去做你”。 结果,该频率列表偏向于连接诸如“ for”,“ in”,“ out”之类的不使用所述中缀的词。
是否有一种简单的方法来查看诸如“ nitakufanya”或“ tunafanya”之类的单词,并将“ fanya”一词包括在总数中?
一些潜在的事情要看:
谢谢
首先进行频率分析,而不必担心前缀。 然后从频率列表中固定前缀。 为此,可以根据单词对列表进行排序,以使具有相同前缀的单词彼此相邻。 这将使手动修剪变得非常容易。
你可以做:
root_words = [re.sub(
'^(ni|u|a|wa|tu|m)(na|li|ta|ji|nge)',
'', x) for word in words]
删除每个单词的前缀,但是如果根单词也以这些序列开头,那么您将无能为力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.