2つの単語が同族であるかどうかを判断するプログラムを作成しようとしています。featTup(基本的に文字の値を含むタプルのラッパー)とfeatWord(基本的にfeatTupオブジェクトのラッパー)の2つのクラスを作成しました。
(申し訳ありませんが、これはすべてとても長いです!)
ここにいくつかの(うまくいけば関連する)コードがあります:
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
現状では、これらの最後の2つのメソッドのいずれかを呼び出すと、空のリストのリストが表示されます。文字列から新しいfeatWordオブジェクトをインスタンス化するときは、それを指定するだけです(たとえば、x = featWord( "ten")など)。関連することは、featWordが代わりに空のリストを返すように見えることです(上記のようにIDLEからfeatWordをインスタンス化すると、featTupインスタンスのリストとして返されます。これは良いことです)。なぜ/それが問題なのかわかりません。
私の問題(の少なくとも一部)は、featWordの不適切な初期化に起因しているように思われます。私はそれらを構築している、または何でも、それらに名前を割り当てていません。私は(コメントアウトされたセクションが証明するように)私が考えることができるほぼすべてを試しました、そして私は困惑しています。辞書を使用してクラスインスタンスなどに名前を付けることについてはここに答えがありますが、辞書を事前に定義できないため(各単語と単語リストは潜在的に異なる長さです)、どうすればよいかわかりません。
どんな助けでも大歓迎です。私はここで自分を狂わせているようなものです。ありがとう。