2014-11-21 175 views
0

這裏是我的代碼: 創建一個類,rit_object是一個私有的類比對參數具有類型:字典映射蟒蛇

class YearCount(rit_object): 
    __slots__ = ('year', 'count') 
    _types = (int, int) 

返回YearCount對象:

def createYearCount(year, count): 
    return YearCount(year, count) 

通讀文件。輸出應該類似於:

import wordData 
words = wordData.readWordFile(’very_short.csv’) 
print(words) 
{’airport’: [YearCount(year=2007, count=175702), YearCount(year=2008, 
count=173294)], ’wandered’: [YearCount(year=2005, count=83769), 
YearCount(year=2006, count=87688), YearCount(year=2007, count=108634), 
YearCount(year=2008, count=171015)], ’request’: [YearCount(year=2005, 
count=646179), YearCount(year=2006, count=677820), YearCount(year=2007, 
count=697645), YearCount(year=2008, count=795265)]} 

readWordFile(文件名):

def readWordFile(fileName): 
    #read in the entire unigram dataset 

    words = {} 
    for line in fileName: 
     new = line.split(', ') 
     print(new) 
     id = new[0] 
     print(id) 
     yc = createYearCount(int(new[1]), int(new[2])) 
     # add to list or create a new list 
     if not id in words: 
      words[id] = [yc] 
     else: 
      words[id].append(yc) 
    print(words) 

如果從我的readWordFile我總出現用途「字」,是我totaloccurences功能corrctly工作對生產總數每年?

def totalOccurences(word, words): 
    count = 0 
    if words[id] == word: 
     count += YearCount.count 
    return count 

文本文件:

airport, 2007, 175702 
airport, 2008, 173294 
request, 2005, 646179 
request, 2006, 677820 
request, 2007, 697645 
request, 2008, 795265 
wandered, 2005, 83769 
wandered, 2006, 87688 
wandered, 2007, 108634 
wandered, 2008, 171015 

回答

1

totalOccurences您使用的變量id但它不是在函數中的任何地方定義:if words[id] == word。我認爲你要做的是總結所有字數在words[word]之內。因此,該函數將變成:

def totalOccurences(word, words): 
    if word not in words: 
     return 0 
    count = 0 
    for item in words[word]: 
     count += item.count 
    return count 

如果單詞不存在words那麼函數返回0。否則,它會越過元素words[word](這是一個列表),它會加起來所有的.count值。然後,您將在words[word]中給出word的總次數。