我正在尝试编写一个程序来确定两个词是否是同源词。我写了两个类:featTup(基本上是一个包含字母值的元组的包装器)和featWord(基本上是一个featTup对象的包装器。)
(对不起,这一切都这么长!)
这是一些(希望是相关的)代码:
class featTup(object):
def __init__(self,char):
self.char = char
self.phone_vals = None
self.dia_vals = None
if self.char in phone_codes:
self.phone_vals = phone_feats[phone_codes.index(char)]
elif self.char in dia_codes:
self.dia_vals = dia_feats[dia_codes.index(char)]
...
class featWord(list):
def do_dia(self,char_feats,dia_feats):
#This method handles the changes diacritics make to preceding phones
for val in dia_feats:
if dia_val:
char_feats.change_val(tup,char_feats.index(dia_val),dia_val)
def get_featWord(self):
return self.word_as_feats
def __init__(self,word):
self.word = word
self.word_as_feats = [featTup(char) for char in self.word]
for char in self.word_as_feats:
if char.is_dia():
i = self.word_as_feats.char_index(char)
self.word_as_feats.do_dia(self.word_as_feats[i-1],self.word_as_feats[i])
def word_len(self):
return len(self.get_featWord())
def char_index(self,char):
return self.word_as_feats.index(char)
问题是我想获取一个单词列表并为所有单词制作 featWord 对象。我不知道每个列表会有多长,也不知道每个单词有多少个字符。更多代码:
def get_words(text1,text2):
import codecs
textin1 = codecs.open(text1,encoding='utf8')
word_list1 = textin1.readlines()
textin1.close()
textin2 = codecs.open(text2,encoding='utf8')
word_list2 = textin2.readlines()
textin2.close()
print word_list1,word_list2
fixed_words1 = []
fixed_words2 = []
for word in word_list1:
fixed_word = word.replace('\n','')
fixed_words1.append(fixed_word)
for word in word_list2:
fixed_word = word.replace('\n','')
fixed_words2.append(fixed_word)
print fixed_words1,fixed_words2
words1 = [(featWord(word)) for word in fixed_words1]
words2 = [(featWord(word)) for word in fixed_words2]
# for word1 in fixed_words1:
# for x in xrange(len(fixed_words1)):
words1.append(featWord(word))
for word2 in fixed_words2:
#for x in xrange(len(fixed_words2)):
words2.append(featWord(word))
print words1
#words1 = [featWord(word) for word in fixed_words1]
#words2 = [featWord(word) for word in fixed_words2]
return words1,words2
def get_cog_dict(text1,text2,threshold=10,print_results=True):
#This is the final method, running are_cog over all words in
#both lists.
word_list1,word_list2 = get_words(text1,text2)
print word_list1, word_list2
就目前而言,当我调用这最后两种方法中的任何一种时,我都会得到空列表的列表;当我从字符串实例化新的 featWord 对象时,我只是给它(例如 x = featWord("ten") 或其他)它工作正常。一个相关的事情是,featWord 似乎返回一个空列表,而不是(当我从 IDLE 实例化 featWord 时,如上所述,它作为一个 featTup 实例列表返回,这很好)。我不确定为什么/如果这是问题。
在我看来,(至少部分)我的问题源于不正确地初始化featWord。我正在构建它们,或者其他什么,但没有给它们命名。我已经尝试了我能想到的所有东西(正如注释掉的部分所证明的那样),我很困惑。这里有关于使用字典来命名类实例等的答案,但由于我无法预先定义字典(每个单词和单词表的长度可能不同),我不确定该怎么做。
任何帮助将不胜感激。我有点把自己逼疯了。谢谢。