2016-11-30 84 views
0

我有以下功能:類型錯誤:列表對象是不可調用的

def sample_handling(sample, lexicon, classification): 
    featureset = [] 

    with open(sample, 'r') as f: 
     contents = f.readlines() 
     for l in contents[:hm_lines]: 
      current_words = word_tokenize(l.lower()) 
      current_words = [lemmatizer.lemmatize(i) for i in current_words] 
      features = np.zeros(len(lexicon)) 
      for word in current_words(): 
       if word.lower() in lexicon: 
        index_value = lexicon.index(word.lower()) 
        features[index_value] += 1 
      features = list(features) 
      featureset.append([features, classification]) 

    return featureset 

當我運行的代碼,它給了我下面的錯誤:

TypeError: 'list' object is not callable

是否有任何掩蓋回事?我跟着很多線程處理這個錯誤,但無法解決我的問題。

這是我的全碼:

import nltk 
from nltk.tokenize import word_tokenize 
from nltk.stem import WordNetLemmatizer 
import numpy as np 
import random 
import pickle 
from collections import Counter 

lemmatizer = WordNetLemmatizer() 
hm_lines = 10000000 

def create_lexicon(pos, neg): 
    lexicon = [] 
    for fi in [pos, neg]: 
     with open(fi, 'r') as f: 
      contents = f.readlines() 
      for l in contents[:hm_lines]: 
       all_words = word_tokenize(l.lower()) 
       lexicon += list(all_words) 
    lexicon = [lemmatizer.lemmatize(i) for i in lexicon] 
    w_counts = Counter(lexicon) 
    #w_counts = {'the': 52521, 'and': 25242} 

    l2 = [] 
    for w in w_counts: 
     if 1000 > w_counts[w] > 50: 
      l2.append(w) 

    print(l2) 
    return l2 

def sample_handling(sample, lexicon, classification): 
    featureset = [] 

    with open(sample, 'r') as f: 
     contents = f.readlines() 
     for l in contents[:hm_lines]: 
      current_words = word_tokenize(l.lower()) 
      current_words = [lemmatizer.lemmatize(i) for i in current_words] 
      features = np.zeros(len(lexicon)) 
      for word in current_words(): 
       if word.lower() in lexicon: 
        index_value = lexicon.index(word.lower()) 
        features[index_value] += 1 
      features = list(features) 
      featureset.append([features, classification]) 

    return featureset 

def create_feature_sets_and_lables(pos, neg, test_size = 0.1): 
    lexicon = create_lexicon(pos, neg) 
    features = [] 
    features += sample_handling('pos.txt', lexicon,[1,0]) 
    features += sample_handling('neg.txt', lexicon,[0,1]) 
    random.shuffle(features) 

    features = np.array(features) 

    testing_size = int(test_size * len(features)) 

    train_x = list(features[:,0][:-testing_size]) 
    train_y = list(features[:,1][:-testing_size]) 

    test_x = list(features[:,0][-testing_size:]) 
    test_y = list(features[:,1][-testing_size:]) 

    return train_x, train_y, test_x, test_y 

if __name__ == '__main__': 
    train_x, train_y, test_x, test_y = create_feature_sets_and_lables('pos.txt', 'neg.txt') 
    with open('sentiment_set.pickle', 'wb') as f: 
     pickle.dump([train_x, train_y, test_x, test_y], f) 

回答

0

如果您打印完整的堆棧跟蹤,它會更有幫助。由於這是一個相對簡單的錯誤,因此在這種情況下,問題很容易識別。這是這條線,

for word in current_words(): 

你不必通話列表循環,同時它。只是這樣做,

for word in current_words: 
+0

謝謝!這是一個愚蠢的錯誤... –

0

好了,開始調試,我會用

python -m pdb whatever_your_file_is.py 

這將啓動一個PDB調試控制檯運行程序。一旦出現,請按'c'運行該程序。過了一段時間,假設程序崩潰,您將停在發生錯誤的確切位置。

從那裏,你可以參考thisthis(只是谷歌蟒蛇PDB),以弄清楚到底發生了什麼事情在你的代碼。

祝你好運!

+0

它崩潰在'features = list(features)'部分。 –

+0

如果您只是在控制檯中輸入'features',會發生什麼? –

+1

我解決了它。謝謝你的幫助! –

相關問題