值错误无法转换字符串

2024-09-27 02:51:40 发布

您现在位置:Python中文网/ 问答频道 /正文

import re

    class WordStatistic:

    def __init__(self, keyword, averageScore = 0, occurences = 0):
        self.keyword = keyword
        self.averageScore = averageScore
        self.occurences = occurences

    def getWord(self) :
        return self.keyword

    def getAverageScore(self) :
        return self.averageScore

    def getOccurences(self) :
        return self.occurences

    def addNewScore(self, newScore) :
        oldScoreSum = self.averageScore * self.occurences
        self.occurences = self.occurences + 1
        self.averageScore = (oldScoreSum + newScore) / (self.occurences)

    def printWordStatistic(self) :
           print ("Word          : "), self.keyword
           print ("Occurences    : "), self.occurences
           print ("Average Score : ", self.occurences, "\n\n")

# Starting Training Phase

wordDictionary = {}
fileInstance = open("movieReviews.txt",'r')
fileText = fileInstance.read()

# Assuming, that each review is seperated by following delimeter
reviewSplits = fileText.split("$$EOD_REVIEW$$")
for review in reviewSplits :
        review = review.strip()
        if review == "" :
            continue
        # In each review, first line contains the score and the
        # subsequent lines contains the text
        lineSplits = review.split("\n")
        score = float(lineSplits[0].strip())
        for i in range(1, len(lineSplits)) :
            # Splitting out the words in each line of the review
            wordSplits = re.split("\t| ", lineSplits[i])
            for word in wordSplits :
                if word == "" :
                    continue
                # If it is already present, then update the score and count
                # Otherwise just add the new entry to the dictionary
                if wordDictionary.has_key(word) :
                    wordStatistic = wordDictionary.get(word)
                    wordStatistic.addNewScore(score)
                else :
                    wordStatistic = WordStatistic(word, score, 1)
                    wordDictionary[word] = wordStatistic

# Training Phase Completed


# To print the statistics of all words in the dictionary
def printAllWordStatistic(wordDictionary) :
    for wordStatistic in wordDictionary.values() :
        wordStatistic.printWordStatistic()

# To rate a review based on the training data
def calculateAverageOfReview(review) :
    review.replace("\t", " ")
    review.replace("\n", " ")
    wordSplits = review.split(" ")

    averageScore = 0.0
    totalCount = 0;
    for word in wordSplits :
        if wordDictionary.has_key(word) :
            averageScore += wordDictionary.get(word).getAverageScore()
            totalCount = totalCount + 1
    if totalCount != 0 :
        return averageScore / totalCount
    return -1


# User Review Input
while (True) :
    print ("\nEnter a review, (enter empty-line to save) : ")
    multiLines = []
    while True:
        line = raw_input()
        if line:
            multiLines.append(line)
        else:
            break
    inputReview = '\n'.join(multiLines)

    averageScore = calculateAverageOfReview(inputReview)
    if averageScore != -1 :
        if averageScore >= 2.50 :
            print ("Positive Review")
        else :
            print ("Negative Review")
    else :
        print ("Unable to rate the review")

    if raw_input("\nDo you want to continue ? (Y/N) : ") != "Y" :
        print ("Quitting the session. Good Bye !")
        exit()

因此,我试图阅读电影评论并显示评级的输入,但我在下面得到了一个转换错误

Traceback (most recent call last):
  File "C:/Users/Adil Ali/Documents/moviereview.py", line 44, in <module>
    score = float(lineSplits[0].strip("\""))
ValueError: could not convert string to float: '1 A series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story .\t'

我试图寻找类似的解决方案,但我找不到任何东西。我应该在strip函数中放入一个字符串,还是需要修改一些内容。请让我知道我正在阅读这个项目的电影评论文本文件


Tags: oftheinselfforifdefline
1条回答
网友
1楼 · 发布于 2024-09-27 02:51:40

错误:请按顺序显示

 score = float(lineSplits[0].strip("\""))

你试着

 score = float('1 A series of escapades ...')

您无法将其转换为数字。您只需从此元素获取1。您可以尝试split(" ")[0]将行拆分为包含元素的列表,并获取第一个元素-1

 score = float(lineSplits[0].strip("\"").split(" ")[0] )

相关问题 更多 >

    热门问题