Python中的动态类实例命名

2024-09-30 20:22:41 发布

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

我想写一个程序来确定两个词是否是同源词。我编写了两个类: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(“十”),或者其他什么)它工作得很好。一件相关的事情是featWord似乎返回一个空列表,而不是(当我从IDLE实例化featWord时,如上所述,它作为featTup实例列表返回,这很好)。我不知道为什么/如果那是问题所在。你知道吗

在我看来,我的问题(至少有一部分)源于不正确地初始化单词。我在构造它们,或者别的什么,但不给它们命名。我已经尝试了我能想到的一切(正如注释掉的部分所证明的),我被难住了。这里有关于使用字典命名类实例之类的答案,但是由于我不能预先定义字典(每个单词和单词列表可能有不同的长度),所以我不确定该怎么做。你知道吗

任何帮助都将不胜感激。我都快疯了。谢谢。你知道吗


Tags: inselfforindexdefas单词word
1条回答
网友
1楼 · 发布于 2024-09-30 20:22:41

你的featWord类是从list派生的,但是你从不向self追加任何东西,而且你已经重写了__init__,所以列表__init__也永远不会被调用。你知道吗

所以featWord实例只是一个带有一些属性和方法的空列表。你知道吗

它们的__repr__list__repr__,这就是为什么featwords列表显示为空列表的原因。你知道吗

所以:实现一个有意义的__repr__,不要从list子类化,将一些有意义的东西附加到self。任何这些都能解决你的问题。你知道吗

相关问题 更多 >