-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
280 lines (247 loc) · 8.89 KB
/
text.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.1/textanalytics"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
"log"
"os"
_ "github.com/mattn/go-sqlite3"
)
func GetTextAnalyticsClient() textanalytics.BaseClient {
var subscriptionKey string = os.Getenv("TEXT_ANALYTICS_KEY")
var endpoint string = os.Getenv("TEXT_ANALYTICS_ENDPOINT")
textAnalyticsClient := textanalytics.New(endpoint)
textAnalyticsClient.Authorizer = autorest.NewCognitiveServicesAuthorizer(subscriptionKey)
return textAnalyticsClient
}
func ExtractEntities(item_id int64, timestamp int64) {
stmt, err := database.Prepare("select value from item_text where item_id = ? order by timestamp limit 1")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var item_text string
//var last_insert_id int
err = stmt.QueryRow(item_id).Scan(&item_text)
//azure
stmt2, err := database.Prepare("select code from item_text_language where item_id = ? order by timestamp DESC limit 1")
if err != nil {
log.Fatal(err)
}
defer stmt2.Close()
var lang_code string
//var last_insert_id int
err = stmt2.QueryRow(item_id).Scan(&lang_code)
const uriPath = "/text/analytics/v2.1/entities"
textAnalyticsClient := GetTextAnalyticsClient()
ctx := context.Background()
inputDocuments := []textanalytics.MultiLanguageInput{
{
Language: to.StringPtr(lang_code),
ID: to.StringPtr("0"),
Text: to.StringPtr(item_text),
},
}
batchInput := textanalytics.MultiLanguageBatchInput{Documents: &inputDocuments}
result, _ := textAnalyticsClient.Entities(ctx, to.BoolPtr(false), &batchInput)
// Printing extracted entities results
for _, document := range *result.Documents {
fmt.Printf("Document ID: %s\n", *document.ID)
fmt.Printf("\tExtracted Entities:\n")
for _, entity := range *document.Entities {
fmt.Printf("\t\tName: %s\tType: %s", *entity.Name, *entity.Type)
if entity.SubType != nil {
fmt.Printf("\tSub-Type: %s\n", *entity.SubType)
}
fmt.Println()
for _, match := range *entity.Matches {
fmt.Printf("\t\t\tOffset: %v\tLength: %v\tScore: %f\n", *match.Offset, *match.Length, *match.EntityTypeScore)
statement, _ := database.Prepare("INSERT INTO item_text_entity (item_id, timestamp,value, length, offset, type, sub_type, score ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
result, err := statement.Exec(item_id,timestamp, *entity.Name, *match.Length, *match.Offset, *entity.Type, entity.SubType, *match.EntityTypeScore)
fmt.Println("Entity - Last Insert ID")
iid, err := result.LastInsertId()
fmt.Println(iid)
if err != nil {
fmt.Println(err)
return
}
}
}
fmt.Println()
}
// Printing document errors
fmt.Println("Document Errors")
for _, err := range *result.Errors {
fmt.Printf("Document ID: %s Message : %s\n", *err.ID, *err.Message)
}
}
func DetectLanguage(item_id int64, timestamp int64) {
stmt, err := database.Prepare("select value from item_text where item_id = ? order by timestamp DESC limit 1")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var item_text string
//var last_insert_id int
err = stmt.QueryRow(item_id).Scan(&item_text)
//azure
textAnalyticsClient := GetTextAnalyticsClient()
ctx := context.Background()
inputDocuments := []textanalytics.LanguageInput{
{
ID: to.StringPtr("0"),
Text: to.StringPtr(item_text),
},
}
batchInput := textanalytics.LanguageBatchInput{Documents: &inputDocuments}
result, _ := textAnalyticsClient.DetectLanguage(ctx, to.BoolPtr(false), &batchInput)
// Printing language detection results
for _, document := range *result.Documents {
fmt.Printf("Document ID: %s ", *document.ID)
fmt.Printf("Detected Languages with Score: ")
for _, language := range *document.DetectedLanguages {
fmt.Printf("%s %s %f,", *language.Name, *language.Iso6391Name,*language.Score)
statement, _ := database.Prepare("INSERT INTO item_text_language (item_id, timestamp, value, code, score ) VALUES (?, ?, ?, ?, ?)")
result, err := statement.Exec(item_id, timestamp, *language.Name, *language.Iso6391Name, *language.Score)
fmt.Println("Entity - Last Insert ID")
iid, err := result.LastInsertId()
fmt.Println(iid)
if err != nil {
fmt.Println(err)
return
}
}
fmt.Println()
}
// Printing document errors
fmt.Println("Document Errors")
for _, err := range *result.Errors {
fmt.Printf("Document ID: %s Message : %s\n", *err.ID, *err.Message)
}
}
func SentimentAnalysis(item_id int64, timestamp int64) {
stmt, err := database.Prepare("select value from item_text where item_id = ? order by timestamp DESC limit 1")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var item_text string
//var last_insert_id int
err = stmt.QueryRow(item_id).Scan(&item_text)
//azure
stmt2, err := database.Prepare("select code from item_text_language where item_id = ? order by timestamp DESC limit 1")
if err != nil {
log.Fatal(err)
}
defer stmt2.Close()
var lang_code string
//var last_insert_id int
err = stmt2.QueryRow(item_id).Scan(&lang_code)
textAnalyticsClient := GetTextAnalyticsClient()
ctx := context.Background()
inputDocuments := []textanalytics.MultiLanguageInput{
{
Language: to.StringPtr(lang_code),
ID: to.StringPtr("0"),
Text: to.StringPtr(item_text),
},
}
batchInput := textanalytics.MultiLanguageBatchInput{Documents: &inputDocuments}
result, _ := textAnalyticsClient.Sentiment(ctx, to.BoolPtr(false), &batchInput)
var batchResult textanalytics.SentimentBatchResult
jsonString, _ := json.Marshal(result)
_ = json.Unmarshal(jsonString, &batchResult)
// Printing sentiment results
for _, document := range *batchResult.Documents {
fmt.Printf("Document ID: %s ", *document.ID)
fmt.Printf("Sentiment Score: %f\n", *document.Score)
statement, _ := database.Prepare("INSERT INTO item_text_sentiment (item_id,timestamp, score ) VALUES (?, ?, ?)")
result, err := statement.Exec(item_id, timestamp, *document.Score)
fmt.Println("Entity - Last Insert ID")
iid, err := result.LastInsertId()
fmt.Println(iid)
if err != nil {
fmt.Println(err)
return
}
}
// Printing document errors
fmt.Println("Document Errors")
for _, err := range *batchResult.Errors {
fmt.Printf("Document ID: %s Message : %s\n", *err.ID, *err.Message)
}
}
func ExtractKeyPhrases(item_id int64, timestamp int64) {
stmt, err := database.Prepare("select value from item_text where item_id = ? order by timestamp DESC limit 1")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var item_text string
//var last_insert_id int
err = stmt.QueryRow(item_id).Scan(&item_text)
stmt2, err := database.Prepare("select code from item_text_language where item_id = ? order by timestamp DESC limit 1")
if err != nil {
log.Fatal(err)
}
defer stmt2.Close()
var lang_code string
//var last_insert_id int
err = stmt2.QueryRow(item_id).Scan(&lang_code)
//azure
textAnalyticsClient := GetTextAnalyticsClient()
ctx := context.Background()
inputDocuments := []textanalytics.MultiLanguageInput{
{
Language: to.StringPtr(lang_code),
ID: to.StringPtr("0"),
Text: to.StringPtr(item_text),
},
}
batchInput := textanalytics.MultiLanguageBatchInput{Documents: &inputDocuments}
result, _ := textAnalyticsClient.KeyPhrases(ctx, to.BoolPtr(false), &batchInput)
// Printing extracted key phrases results
for _, document := range *result.Documents {
fmt.Printf("Document ID: %s\n", *document.ID)
fmt.Printf("Document Language: %s\n", lang_code)
fmt.Printf("\tExtracted Key Phrases:\n")
for _, keyPhrase := range *document.KeyPhrases {
fmt.Printf("\t\t%s\n", keyPhrase)
statement, _ := database.Prepare("INSERT INTO item_text_key_phrase (item_id,timestamp, value ) VALUES (?, ?, ?)")
result, err := statement.Exec(item_id, timestamp, keyPhrase)
fmt.Println("Entity - Last Insert ID")
iid, err := result.LastInsertId()
fmt.Println(iid)
if err != nil {
fmt.Println(err)
return
}
}
fmt.Println()
}
// Printing document errors
fmt.Println("Document Errors")
for _, err := range *result.Errors {
fmt.Printf("Document ID: %s Message : %s\n", *err.ID, *err.Message)
}
}
func Get(item_id int64) {
fmt.Println("Print Items: Key Phrases")
stmt, err := database.Prepare("WITH RECURSIVE x as (select value, timestamp, max(timestamp) as max from item_text_key_phrase where item_id = ?) select item_id, value from item_text_key_phrase WHERE timestamp = (select max from x);")
result, err := stmt.Query(item_id)
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var value string
for result.Next() {
err := result.Scan(&item_id, &value)
if err != nil {
log.Fatal(err)
}
fmt.Println(item_id, value)
}
}