-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-process.go
172 lines (147 loc) · 4.25 KB
/
text-process.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bytes"
"encoding/json"
"fmt"
textapi "github.com/AYLIEN/aylien_textapi_go"
"io/ioutil"
"net/http"
)
var TextApiClient *textapi.Client
type WatsonToneResponse struct {
DocumentTone struct {
ToneCategories []struct {
CategoryName string `json:"category_name"`
CategoryId string `json:"category_id"`
Tones []struct {
Score float64 `json:"score"`
ToneId string `json:"tone_id"`
ToneName string `json:"tone_name"`
} `json:"tones"`
} `json:"tone_categories"`
} `json:"document_tone"`
}
type SummaryResponse struct {
Keyword string `json:"keyword"`
Text string `json:"text"`
KeywordSentiment float32 `json:"keyword_sentiment"`
Reviews *GooglePlaceReviews `json:"google_place_review"`
}
type WatsonUnderstandRequest struct {
Text string `json:"text"`
Features struct {
Keywords struct {
Emotion bool `json:"emotion"`
Sentiment bool `json:"sentiment"`
Limit int32 `json:"limit"`
} `json:"keywords"`
} `json:"features"`
}
type WatsonUnderstandResponse struct {
Keywords []struct {
Text string `json:"text"`
Sentiment struct {
Score float32 `json:"score"`
} `json:"sentiment"`
} `json:"keywords"`
}
func Summarize(reviews string, creds *ApiKeys) (resp *SummaryResponse, err error) { // return summary for business reviews
if TextApiClient == nil {
auth := textapi.Auth{creds.TextApi.AppId, creds.TextApi.Key}
TextApiClient, err = textapi.NewClient(auth, true)
if err != nil {
fmt.Println(err)
}
}
fmt.Println("incoming review text: " + reviews)
summarizeParams := &textapi.SummarizeParams{
Title: "Review",
Text: reviews,
NumberOfSentences: 1,
Mode: "short",
}
summary, err := TextApiClient.Summarize(summarizeParams)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%v\n", TextApiClient.RateLimits)
resp = new(SummaryResponse)
summaryText := ""
for x := range summary.Sentences {
summaryText = summaryText + summary.Sentences[x]
}
understanding, err := FindKeyword(creds, reviews)
if err != nil {
fmt.Println(err)
}
title := understanding.Keywords[0].Text
keywordSentiment := understanding.Keywords[0].Sentiment.Score
fmt.Println("title: " + title)
resp.Text = summaryText
resp.Keyword = title
resp.KeywordSentiment = keywordSentiment
fmt.Println("outgoing summary" + summaryText)
return resp, nil
}
func AnalyzeTone(creds *ApiKeys, text string) (*WatsonToneResponse, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone", nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(creds.Watson.ToneAnalysis.Username, creds.Watson.ToneAnalysis.Password)
q := req.URL.Query()
q.Add("version", "2016-05-19")
q.Add("text", text)
q.Add("sentences", "false")
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var r = new(WatsonToneResponse)
err = json.Unmarshal([]byte(bodyBytes), &r)
if err != nil {
fmt.Println(err)
}
return r, err
}
func FindKeyword(creds *ApiKeys, text string) (*WatsonUnderstandResponse, error) {
reqBody := new(WatsonUnderstandRequest)
reqBody.Text = text
reqBody.Features.Keywords.Emotion = false
reqBody.Features.Keywords.Sentiment = true
reqBody.Features.Keywords.Limit = 1
reqBodyJson, err := json.Marshal(reqBody)
if err != nil {
fmt.Println(err)
}
client := &http.Client{}
req, err := http.NewRequest("POST", "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze", bytes.NewBuffer(reqBodyJson))
if err != nil {
return nil, err
}
req.SetBasicAuth(creds.Watson.LanguageUnderstanding.Username, creds.Watson.LanguageUnderstanding.Password)
q := req.URL.Query()
q.Add("version", "2017-02-27")
req.URL.RawQuery = q.Encode()
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var r = new(WatsonUnderstandResponse)
err = json.Unmarshal([]byte(bodyBytes), &r)
if err != nil {
fmt.Println(err)
}
return r, err
}