This repository has been archived by the owner on Dec 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.go
129 lines (109 loc) · 2.89 KB
/
analysis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"github.com/ChimeraCoder/anaconda"
)
type AnalysisResult struct {
AnalysedTweet AnalysedTweet
Failed bool
}
type AnalysedTweet struct {
TweetId int64 `json:"tweetId"`
Text string `json:"text"`
Lang string `json:"lang"`
User TweetUser `json:"user"`
Sentiment Sentiment `json:"sentiment"`
}
type TweetUser struct {
Id int64 `json:"id"`
Name string `json:"name"`
ScreenName string `json:"screenName"`
FollowersCount int `json:"followersCount"`
FriendsCount int `json:"friendsCount"`
}
type AlchemySentimentScore struct {
Type string `json:"type"`
Score string `json:"score"`
}
type Sentiment struct {
Type string `json:"type"`
Score float64 `json:"score"`
}
type AlchemySentiment struct {
Status string `json:"status"`
Sentiment AlchemySentimentScore `json:"docSentiment"`
}
func analyseTweets(tweets []anaconda.Tweet) []AnalysedTweet {
var processed []AnalysedTweet
for _, tweet := range tweets {
//log.Printf("Analyse lang: %s and tweet: %s\n", tweet.Lang, tweet.Text)
analysed := analyseTweet(tweet)
if !analysed.Failed {
processed = append(processed, analysed.AnalysedTweet)
}
}
return processed
}
func analyseTweet(tweet anaconda.Tweet) AnalysisResult {
data := url.Values{}
data.Set("apikey", config.Alchemy.ApiKey)
data.Add("text", tweet.Text)
data.Add("outputMode", "json")
req, err := http.NewRequest("POST", config.Alchemy.Url, bytes.NewBufferString(data.Encode()))
if err != nil {
log.Printf("Cannot form sentiment request: %s\n", err)
}
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Printf("Failed to process request: %s\n", err)
return AnalysisResult{
Failed: true,
}
}
defer res.Body.Close()
//log.Printf("Res status: %s\n", res.Status)
body, _ := ioutil.ReadAll(res.Body)
//log.Printf("Res: %s\n", body)
alchemySentiment := AlchemySentiment{}
err = json.Unmarshal(body, &alchemySentiment)
if err != nil {
log.Printf("Failed to marshal alchemy response: %s\n%s\n", err, body)
}
if alchemySentiment.Status != "OK" {
return AnalysisResult{
Failed: true,
}
}
sentiment := Sentiment{
Type: alchemySentiment.Sentiment.Type,
}
score, err := strconv.ParseFloat(alchemySentiment.Sentiment.Score, 32)
if err != nil {
score = 0.0
}
sentiment.Score = score
return AnalysisResult{
AnalysedTweet: AnalysedTweet{
TweetId: tweet.Id,
Text: tweet.Text,
Lang: tweet.Lang,
User: TweetUser{
Id: tweet.User.Id,
Name: tweet.User.Name,
ScreenName: tweet.User.ScreenName,
FollowersCount: tweet.User.FollowersCount,
FriendsCount: tweet.User.FriendsCount,
},
Sentiment: sentiment,
},
Failed: false,
}
}