/
main.go
166 lines (146 loc) · 3.47 KB
/
main.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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"github.com/neurosnap/sentences/english"
)
type Comment struct {
Id int
Body string
ParentId int
Source string
Created string
Updated string
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func formatText(text string) string {
re := regexp.MustCompile(`([^\.]$)`)
text = re.ReplaceAllString(text, "$1. ")
re = regexp.MustCompile(`https?\S+`)
text = re.ReplaceAllString(text, "URL")
re = regexp.MustCompile(`(\w{2,}[^\s\w-']+)(\w{2,})`)
text = re.ReplaceAllString(text, "$1 $2")
re = regexp.MustCompile(`\n`)
text = re.ReplaceAllString(text, " ")
re = regexp.MustCompile(`^-\s*`)
text = re.ReplaceAllString(text, "")
re = regexp.MustCompile(`\s+`)
text = re.ReplaceAllString(text, " ")
return text
}
func main() {
// get some comments
commentId := "1"
response, err := http.Get("http://local.docker:3000/comments/" + commentId + ".json?flat=true")
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
var comments = []Comment{}
json.Unmarshal([]byte(body), &comments)
// collect all the text
allText := ""
for _, v := range comments {
allText += v.Body + "\n\n"
}
allText = formatText(allText)
// get the topics for the text
payload := struct {
Text string `json:"text"`
TopicCount int `json:"topic_count"`
TopWordCount int `json:"top_word_count"`
}{
allText, 5, 5,
}
b, _ := json.Marshal(payload)
reqBody := bytes.NewBuffer(b)
response, err = http.Post("http://local.docker:4567", "text/json", reqBody)
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err = ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
var result struct {
Topics []string
Groups [][]string
}
json.Unmarshal([]byte(body), &result)
fmt.Println(result)
// extract the sentences from the text
tokenizer, err := english.NewSentenceTokenizer(nil)
if err != nil {
panic(err)
}
sentences := tokenizer.Tokenize(allText)
// scan for matching sentences
patterns := []string{
"{}=verb >nsubj {}=subj >dobj {}=obj",
"{}=obj >nsubj {}=subj >cop {}=verb",
"{}=verb >nsubj {}=subj >ccomp {}=verb2",
"{}=subj >advcl {}=verb",
"{}=verb >/nmod.*/ {}=subj >dobj {}=obj",
"{}=verb >nsubj {}=subj >/nmod.*/ {}=obj",
}
var topics []string
for _, s := range sentences {
s.Text = strings.TrimSpace(s.Text)
s.Text = strings.ToLower(s.Text)
for _, topic := range result.Topics {
if strings.Contains(s.Text, topic) {
topics = append(topics, topic)
}
}
if contains(topics, "evolution") {
fmt.Println(topics)
fmt.Println(s.Text)
for _, v := range patterns {
fmt.Println(v)
payload := struct {
Text string `json:"text"`
Pattern string `json:"pattern"`
}{
s.Text, v,
}
b, _ := json.Marshal(payload)
reqBody := bytes.NewBuffer(b)
response, err = http.Post("http://local.docker:4568", "text/json", reqBody)
if err != nil {
panic(err)
}
body, err = ioutil.ReadAll(response.Body)
if len(string(body)) < 10 {
continue
}
var out bytes.Buffer
json.Indent(&out, body, "", " ")
fmt.Println(string(out.Bytes()))
}
reader := bufio.NewReader(os.Stdin)
fmt.Print(">")
_, _ = reader.ReadString('\n')
fmt.Println("-------\n\n")
}
topics = topics[:0]
}
}