python - 值错误 : A ELE probability distribution must have at least one bin

标签 python python-2.7 classification nltk

我正在尝试使用朴素贝叶斯分类器对推文的情绪进行分类。所以当我运行下面的代码时,我收到了这个错误,

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()

我怎么解决这个问题。
谢谢

最佳答案

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

关于python - 值错误 : A ELE probability distribution must have at least one bin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31937797/

相关文章:

python - 使用 lxml xml 模式验证打印回溯

python - 拆分名称列表,其中两个名字可能有共同的姓氏

machine-learning - 回归与分类的准确一般描述

python - 如何处理图像分类的可变图像尺寸?

python - 在 python 中将日期列表转换为正确的日期类型格式

python - 正则表达式拆分包装模式

python - 有Python轻量级GUI库吗?

matlab - Matlab 中的感知器训练

python - 导入错误: cannot import name 'RandomizedLogisticRegression'

python - 为什么相等整数的行为与相等列表不同?