Неправильный вывод из анализа настроений? - PullRequest
0 голосов
/ 25 марта 2019

Я кратко объясню свое назначение кодирования:

1) Я должен рассчитать оценку счастья твитов из 4 разных часовых поясов. Есть два файла, один из которых содержит ключевые слова, каждое из которых имеет соответствующее значение настроения, а другой содержит сами твиты. 2) Сначала мне нужно было прочитать файл ключевых слов и распределить ключевые слова по 4 спискам (в зависимости от их настроения). Файл твитов представлен в формате [широта, долгота] значение, дата, время, текст.
3) «Оценка счастья» для часового пояса - это просто сумма баллов (оценка настроения) для всех твитов в этом регионе, деленная на количество твитов. Моя программа должна игнорировать твиты без ключевых слов, а также игнорировать твиты из вне часовых поясов. Так что могут быть некоторые за пределами часовых поясов. Timezones

latitude,longitude latitude,longitude

tweets file

keywords

keywordsfile=input("Enter name of keyword file: ")
infile=open(keywordsfile,"r",encoding="utf-8")
depressed=[] #keywords with sentiment value 1
okay=[] #keywords with sentiment value 5
good=[] #keywords with sentiment value 7
happy=[] #keywords with sentiment value 10
for line in infile:
    line=line.rstrip()
    keyWords=line.split(",")
    keyWords[1]=int(keyWords[1])
    if keyWords[1]==1:
        depressed.append(keyWords[0])
    elif keyWords[1]==5:
        okay.append(keyWords[0])
    elif keyWords[1]==7:
        good.append(keyWords[0])
    elif keyWords[1]==10:
        happy.append(keyWords[0])
    else:
        pass
infile.close()
tweetfile=input("Enter name of tweet file: ")
infile2=open(tweetfile,"r",encoding="utf-8")
DEPRESSEDVALUE=1
OKAYVALUE=5
GOODVALUE=7
HAPPYVALUE=10
depressedKeys=0
okayKeys=0
goodKeys=0
happyKeys=0
numOfTweetsEastern=0
numOfTweetsCentral=0
numOfTweetsMountain=0
numOfTweetsPacific=0
for line in infile2:
    line=line.rstrip()
    words=line.split()
    firststriplat=words[0].rstrip(",")
    lat=firststriplat.lstrip("[")
    lat=float(lat)
    long=words[1].rstrip("]")
    long=float(long)
    easternLat= 24.660845 <= lat and lat<=49.189787
    easternLong= -87.518395 <= long <= -67.444574
    centralLat= 24.660845 <= lat and lat<=49.189787
    centralLong= -101.998892 <= long <= -87.518395
    mountainLat=24.660845 <= lat and lat<=49.189787
    mountainLong=-115.236428 <= long <= -101.998892
    pacificLat=24.660845 <= lat and lat<=49.189787
    pacificLong= -125.242264<= long <= -115.236428
    if easternLat and easternLong:
        for word in words:
            if word in depressed:
                depressedKeys=depressedKeys+1
            elif word in okay:
                okayKeys=okayKeys+1
            elif word in good:
                goodKeys=goodKeys+1
            elif word in happy:
                happyKeys=happyKeys+1
            else:
                pass
        numOfTweetsEastern=numOfTweetsEastern+1
        sentimentValueEastern=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
    elif centralLat and centralLong:
        for word in words:
            if word in depressed:
                depressedKeys=depressedKeys+1
            elif word in okay:
                okayKeys=okayKeys+1
            elif word in good:
                goodKeys=goodKeys+1
            elif word in happy:
                happyKeys=happyKeys+1
            else:
                pass
        numOfTweetsCentral=numOfTweetsCentral+1
        sentimentValueCentral=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
    elif mountainLat and mountainLong:
        for word in words:
            if word in depressed:
                depressedKeys=depressedKeys+1
            elif word in okay:
                okayKeys=okayKeys+1
            elif word in good:
                goodKeys=goodKeys+1
            elif word in happy:
                happyKeys=happyKeys+1
            else:
                pass
        numOfTweetsMountain=numOfTweetsMountain+1
        sentimentValueMountain=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
    elif pacificLat and pacificLong:
        for word in words:
            if word in depressed:
                depressedKeys=depressedKeys+1
            elif word in okay:
                okayKeys=okayKeys+1
            elif word in good:
                goodKeys=goodKeys+1
            elif word in happy:
                happyKeys=happyKeys+1
            else:
                pass
        numOfTweetsPacific=numOfTweetsPacific+1
        sentimentValuePacific=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
    else:
        pass
happScoreEastern=sentimentValueEastern/numOfTweetsEastern
happScoreCentral=sentimentValueCentral/numOfTweetsCentral
happScoreMountain=sentimentValueMountain/numOfTweetsMountain
happScorePacific=sentimentValuePacific/numOfTweetsPacific
print("The happiness score for the Eastern timezone is",happScoreEastern,"and the total number of tweets were",numOfTweetsEastern)
print("The happiness score for the Central timezone is",happScoreCentral,"and the total number of tweets were",numOfTweetsCentral)
print("The happiness score for the Mountain timezone is",happScoreMountain,"and the total number of tweets were",numOfTweetsMountain)
print("The happiness score for the Pacific timezone is",happScorePacific,"and the total number of tweets were",numOfTweetsPacific)

Тем не менее, по-видимому, мои оценки счастья и количество твитов из каждого часового пояса слишком далеки (по-видимому, большие цифры). Где я ошибся в своем коде? Я думал, что все сделал правильно

...