I'm trying to write a program that determines whether two words are cognates. I've written two classes: featTup (basically a wrapper around a tuple containing the values of a letter), and featWord (basically a wrapper around of featTup objects.)
(Sorry this is all so long!)
Here's some (hopefully relevant) code:
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)
The issue is that I want to take a list of words and make featWord objects for all of them. I don't know how long each list will be, nor do I know how many characters will be in each word. More code:
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
As it stands, when I call either of these last two methods, I get lists of empty lists; when I instantiate new featWord objects from strings I just give it (e.g. x = featWord("ten"), or whatever) it works fine. A relevant thing is that featWord seems to return an empty list instead of (when I instantiate featWord from IDLE, as above, it comes back as a list of featTup instances, which is good). I'm not sure why/if that's the problem.
It seems to me that (at least part of) my problem stems from improperly initializing the featWord. I'm constructing them, or whatever, but not assigning them names. I've tried just about everything I can think of (as the commented-out sections prove), and I'm stumped. There're answers on here about using dictionaries to name class instances and such, but since I can't pre-define a dictionary (each word and wordlist is potentially a different length), I'm not sure what to do.
Any help would be GREATLY appreciated. I'm kind of driving myself insane over here. Thanks.
-
Please indent your code accurately (that is, the same way you have it in your own code)- it doesn't run. ETA: Disregard that, @KarlKnechtel fixed it!David Robinson– David Robinson2012年05月01日 15:43:49 +00:00Commented May 1, 2012 at 15:43
-
@DavidRobinson It took a couple tries; I think I have it now.Karl Knechtel– Karl Knechtel2012年05月01日 15:44:55 +00:00Commented May 1, 2012 at 15:44
-
2Anyway, what exactly is your question? Can you show sample input and expected corresponding output? Can you show what is actually happening? Could you give us a version without all the commented-out stuff that shows what you're currently trying? Or at least organize your description of attempts better?Karl Knechtel– Karl Knechtel2012年05月01日 15:47:11 +00:00Commented May 1, 2012 at 15:47
1 Answer 1
your featWord class derives from list, but you never append anything to self, and you have overridden __init__, so lists __init__ never gets called, too.
So a featWord instance is just an empty list with some attributes and methods.
Their __repr__ is list's __repr__, that's why a list of featwords displays as a list of empty lists.
So: implement a meaningful __repr__, do not subclass from list, append something meaninful to self. Any of that will solve your problem.