2012-05-01 57 views
0

我試圖編寫一個程序來確定兩個單詞是否相同。我已經寫了兩個類:featTup(基本上圍繞包含字母的值的元組的包裝),和featWord(基本上是featTup物體的周圍包裝。)Python中的動態類實例命名

(對不起,這是所有這麼久!)

下面是一些(希望相關)代碼:

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(「十」)或其他),它工作正常。相關的事情是,特技似乎返回一個空列表,而不是(當我從IDLE實例化featWord,如上所述,它返回作爲featTup實例列表,這是很好的)。我不確定爲什麼/如果那是問題。

在我看來,我的問題(至少部分)是由於不適當地初始化功能詞。我正在構建它們,或者其他什麼,但沒有分配它們的名字。我已經嘗試過所有我能想到的事情(如註釋部分所證明的),而且我很難過。這裏有關於使用字典命名類實例等的答案,但由於我無法預先定義字典(每個單詞和單詞列表可能有不同的長度),我不知道該怎麼做。

任何幫助將非常感激。我有點在這裏瘋狂。謝謝。

+0

請準確地縮進您的代碼(即您在自己的代碼中使用相同的方式) - 它不會運行。 ETA:無視這一點,@KarlKnechtel修復了它! –

+0

@DavidRobinson花了幾次嘗試;我想我現在擁有它。 –

+2

無論如何,你的**問題到底是什麼**?你能顯示樣本輸入和預期的相應輸出嗎?你能展示實際發生的事情嗎?你能給我們一個沒有所有評論的東西的版本,顯示你目前正在嘗試的東西嗎?或者至少組織你對企圖的描述更好? –

回答

1

featWord類從list派生,但你永遠不添加任何東西self,並已覆蓋__init__,所以名單__init__不會被調用了。

所以一個featWord實例只是一個具有一些屬性和方法的空列表。

他們的__repr__list__repr__,這就是爲什麼特殊字符列表顯示爲空列表列表。

所以:實現一個有意義的__repr__,不要從list繼承,附加一些有意義的self。任何這些都可以解決你的問題。