ValueError:ELE概率分布必须至少有一个bin

时间:2015-08-11 09:18:06

标签: python python-2.7 classification nltk

我试图使用Naiive Bayes Classifier对推文的情绪进行分类。因此,当我运行以下代码时,我收到此错误,

ValueError:ELE概率分布必须至少有一个bin。

代码如下

import re,nltk

# start process_tweet
def processTweet(tweet):
    # process the tweets

    # Convert to lower case
    tweet = tweet.lower()
    # Convert www.* or https?://* to URL
    tweet = re.sub('((www\.[^\s]+)|(https?://[^\s]+))', 'URL', tweet)
    # Convert @username to AT_USER
    tweet = re.sub('@[^\s]+', 'AT_USER', tweet)
    # Remove additional white spaces
    tweet = re.sub('[\s]+', ' ', tweet)
    # Replace #word with word
    tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
    # trim
    tweet = tweet.strip('\'"')
    return tweet


# end
# Read the tweets one by one and process it
fp = open('/home/ashish/PyCharm_proj/twitter_sentiment/data/sampleData.txt', 'r')

line = fp.readline()
print "Processed tweets\n"
while line:
    processedTweet = processTweet(line)
    print processedTweet
    line = fp.readline()
# end loop

#start getfeatureVector
def getFeatureVector(tweet):
    featureVector = []
    #split tweet into words
    words = tweet.split()
    for w in words:
        #replace two or more with two occurrences
        w = replaceTwoOrMore(w)
        #strip punctuation
        w = w.strip('\'"?,.')
        #check if the word stats with an alphabet
        val = re.search(r"^[a-zA-Z][a-zA-Z0-9]*$", w)
        #ignore if it is a stop word
        if(w in stopWords or val is None):
            continue
        else:
            featureVector.append(w.lower())
    return featureVector
#end

#fp.close()
# initialize stopWords
stopWords = []

inpTweets=fp
featureList=[]
#Read the tweets one by one and process it
tweets = []
for row in inpTweets:
    sentiment = row[0]
    tweet = row[1]
    processedTweet = processTweet(tweet)
    featureVector = getFeatureVector(processedTweet, stopWords)
    featureList.extend(featureVector)
    tweets.append((featureVector, sentiment));
#end loop

#start extract_features
def extract_features(tweet):
    tweet_words = set(tweet)
    features = {}
    for word in featureList:
        features['contains(%s)' % word] = (word in tweet_words)
    #print "Features are: "+features
    return features

#end

#print "Feature List is:"+"\n"+featureList

# Remove featureList duplicates
featureList = list(set(featureList))
training_set = nltk.classify.util.apply_features(extract_features, tweets)
# start replaceTwoOrMore
def replaceTwoOrMore(s):
    # look for 2 or more repetitions of character and replace with the character itself
    pattern = re.compile(r"(.)\1{1,}", re.DOTALL)
    return pattern.sub(r"\1\1", s)


# end

# start getStopWordList
def getStopWordList(stopWordListFileName):
    # read the stopwords file and build a list
    stopWords = []
    stopWords.append('AT_USER')
    stopWords.append('URL')

    fp = open(stopWordListFileName, 'r')
    line = fp.readline()
    while line:
        word = line.strip()
        stopWords.append(word)
        line = fp.readline()
    fp.close()
    return stopWords


# end

# start getfeatureVector
def getFeatureVector(tweet):
    featureVector = []
    # split tweet into words
    words = tweet.split()
    for w in words:
        # replace two or more with two occurrences
        w = replaceTwoOrMore(w)
        # strip punctuation
        w = w.strip('\'"?,.')
        # check if the word stats with an alphabet
        val = re.search(r"^[a-zA-Z][a-zA-Z0-9]*$", w)
        # ignore if it is a stop word
        if (w in stopWords or val is None):
            continue
        else:
            featureVector.append(w.lower())
    return featureVector


# Train the classifier
NBClassifier = nltk.NaiveBayesClassifier.train(training_set)

# Test the classifier
testTweet = 'Congrats @ashish, The classifier works'
processedTestTweet = processTweet(testTweet)
print NBClassifier.classify(extract_features(getFeatureVector(processedTestTweet)))


# end

# Read the tweets one by one and process it
fp = open('/home/ashish/PyCharm_proj/twitter_sentiment/data/sampleData.txt', 'r')

line = fp.readline()

stopWords = getStopWordList('/home/ashish/PyCharm_proj/twitter_sentiment/data/feature_list/stopwords.txt')
print "\n Feature vectors are:\n "
while line:
    processedTweet = processTweet(line)
    featureVector = getFeatureVector(processedTweet)
    print featureVector
    line = fp.readline()
# end loop
fp.close()

我该如何解决这个问题。 感谢

1 个答案:

答案 0 :(得分:0)

您必须首先创建用于训练数据的字典格式。如果您查看.train()的文档,则会发现很多细节。