2017-05-08 19 views
-4

87行目でこのエラーが発生する: ValueError:int型の無効なリテラル()と基数10: '' "Python ValueError:基数10のint()の無効なリテラル

私は問題が何か助けになると分かりません。参考のため

#get a word from the user to analyze 
word=str.lower(input("Enter a word to test: ")) 

#open up all of the movie reviews 
file=open("movie_reviews.txt","r") 

alldata=str.lower(file.read()) 

file.close() 

#create accumulators 
total=0 
numreviews=0 

#split data into a list 
splitreviews=alldata.split("\n") 


#iterate over each review 
for review in splitreviews: 

    #further split the review into individual words 

    words=review.split(" ") 

    if word in words: 

     total+=int(words[0]) 

     numreviews+=1 
#determine average and if positive or negative or not enough info 
if numreviews==0: 
    print(word,"appears",numreviews,"times") 
    print("There is no average score for reviews containing the word",word) 
    print("Cannot determine if this word is positive or negative") 
else: 
    print(word,"appears",numreviews,"times") 
    print("Total score was:",total) 
    print("Average score for this word is:",total/numreviews) 

    if total/numreviews>=2: 
     print("This is a POSITIVE word!") 
    else: 
     print("This is a NEGATIVE word!") 


#set up an empty dictionary to hold all words 
words={} 

#import time 
import time 

t=time.time() 
print("Initializing sentiment database") 

#open up all of movie reviews 
file=open("movie_reviews.txt","r") 

alldata=str.lower(file.read()) 

file.close() 

splitreviews=alldata.split("\n") 

total=0 
for review in splitreviews: 

    word=review.split(" ") 

    #examine every word in this review 
    #add it to the sentiment dictionary if necessary 
    #or update it if it already exists 
    for w in word: 

     if w not in words: 
      #add it to the dictionary! 
      words[w]=[int(word[0]),1] 
      total+=1 

     else: 
      #update the word using the new review 
      words[w][1]+=1 
      words[w][0]+=(str(word[0])) 

#all done 

print("Sentiment database initialization complete") 
print("Total unique words analyzed:",total) 
print("Analysis took",time.time()-t,"seconds to complete") 


phrase=str.lower(input("Enter a phrase to test: ")) 

phraseparts=phrase.split(" ") 

totalaverage=0 
num=0 

#iterate over each element of phrase pulling info from dictionary 
for p in phraseparts: 
    if p not in words: 
     print("*",p,"does not appear in any movie reviews") 
    else: 
     num+=1 
     print("*",p,"appears",words[p][1],"times with an average score of",words[p][0]/words[p][1]) 
     totalaverage+=words[p][0]/words[p][1] 

if num==0: 
    print("Not enough words to determine sentiment.") 

#determine average if words in the phrace appear in the dictionary 
else: 
    print("Average score for this phrase is: ",totalaverage/num) 

#determine if it's a positive or negative word 
    if totalaverage/num>=2: 
     print("This is a POSITIVE phrase") 
    else: 
     print("This is a NEGATIVE phrase")  

次のようになりますmoviereviews.txtファイル:

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 . 
4 This quiet , introspective and entertaining independent is worth seeking .  
1 Even fans of Ismail Merchant 's work , I suspect , would have a hard time sitting through this one . 
3 A positively thrilling combination of ethnography and all the intrigue , betrayal , deceit and murder of a Shakespearean tragedy or a juicy soap opera . 
1 Aggressive self-glorification and a manipulative whitewash . 
4 A comedy-drama of nearly epic proportions rooted in a sincere performance by the title character undergoing midlife crisis . 
1 Narratively , Trouble Every Day is a plodding mess . 
3 The Importance of Being Earnest , so thick with wit it plays like a reading from Bartlett 's Familiar Quotations 
1 But it does n't leave you with much . 
1 You could hate it for the same reason . 
+0

すべてのコードの代わりに**最小サンプル**を含めることができれば助かります([mcve]を参照)。 'ValueError'を投げるとき' word [0] 'とまったく何ですか? – MSeifert

+2

例外をキャッチし、キャストしようとしている単語をintに出力します。私はそれが空であるためクラッシュする最後の行だと思う。 – Maresh

+1

上部に記載されているエラーラインはコードに含まれていません –

答えて

0

ので、ここで私が持っている情報であなたを助けるために少し難しいかもしれないが、私はあなたどう思うかの実施例でありますやろうとしている。

string = "This movie sucks so badly, it is the worst thing I have seen in my entire life" 

score = 0 

positive = ["good", "best", "happy", "happiest", "amazing", "perfect"] 
negative = ["bad", "worst", "sad", "saddest", "stupid", "disgusting"] 

words = string.split(" ") 

for i in words: 
    if any(i in item for item in positive): 
     score += 1 

    if any(i in item for item in negative): 
     score -= 1 

if score == 0: 
    print("Not enough words to determine sentiment") 

if score > 0: 
    print("Happy with a score of:", score) 
else: 
    print("Sad with a score of:", score) 

この質問は非常に古いですが、他の人がこれを必要とする可能性があります。

関連する問題