-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (196 loc) · 5.83 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
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
package main
import (
"github.com/jdkato/prose/v2"
"fmt"
"log"
"encoding/json"
"flag"
"bufio"
"os"
"io"
"io/ioutil"
"path/filepath"
"crypto/md5"
)
var pln = fmt.Println
func demo_tokens_entities_sentences() {
// Create a new document with the default configuration:
doc, err := prose.NewDocument("Go is an open-source programming language created at Google.")
if err != nil {
log.Fatal(err)
}
// Iterate over the doc's tokens:
for _, tok := range doc.Tokens() {
fmt.Println(tok.Text, tok.Tag, tok.Label)
// Go NNP B-GPE
// is VBZ O
// an DT O
// ...
}
fmt.Println("\n==========\n")
// Iterate over the doc's named-entities:
for _, ent := range doc.Entities() {
fmt.Println(ent.Text, ent.Label)
// Go GPE
// Google GPE
}
fmt.Println("\n==========\n")
// Iterate over the doc's sentences:
for _, sent := range doc.Sentences() {
fmt.Println(sent.Text)
// Go is an open-source programming language created at Google.
}
fmt.Println("\n==========\n")
}
func do_tokens(idoc *[]byte) {
pln("************************************************************")
pln("******************* TOKENIZED/TAGGED TEXT ******************")
pln("************************************************************")
doc, err := prose.NewDocument(string(*idoc))
if err != nil {
log.Fatal(err)
}
// Iterate over the doc's tokens:
fmt.Println ("--- Format is: Text; Tag; Label ---")
for _, tok := range doc.Tokens() {
fmt.Println(tok.Text,"; ", tok.Tag, "; ", tok.Label)
}
/*
pln("************************************************************")
pln("*********** JSON VERSION OF TOKENIZED/TAGGED TEXT *********")
pln("************************************************************")
u, err := json.Marshal(doc.Tokens())
if err != nil {
log.Fatal(err)
}
fmt.Println(string(u))
*/
}
func do_entities(idoc *[]byte) {
pln("************************************************************")
pln("******* PERSONS, GEOGRAPHICAL, and POLITICAL ENTITIES ******")
pln("************************************************************")
doc, err := prose.NewDocument(string(*idoc))
if err != nil {
log.Fatal(err)
}
// Iterate over the doc's named-entities:
fmt.Println ("--- Format is: Text; Label ---")
for _, ent := range doc.Entities() {
fmt.Println(ent.Text, "; ", ent.Label)
// Go GPE
// Google GPE
}
/*
pln("************************************************************")
pln("*** JSON VERSION PERSONS, GEOGRAPHICAL/POLITICAL ENTITIES **")
pln("************************************************************")
u, err := json.Marshal(doc.Entities())
if err != nil {
log.Fatal(err)
}
fmt.Println(string(u))
*/
}
type GoalpostJSON struct {
FullFile string
MD5Digest string
FileName string
Text string
GPE []TokenizedBlock
Tokens []TokenizedBlock
}
type TokenizedBlock struct{
Text string
Tag string
Label string
}
var JSONOutput GoalpostJSON
func md5file (file *string) string {
var digest string
f, err := os.Open(*file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
hash := md5.New()
_, err = io.Copy(hash,f)
if err !=nil {
log.Fatal(err)
}
digest = fmt.Sprintf("%x", hash.Sum(nil))
return digest
}
func post_file(file *string){
var fname string
var tagcount uint64 //I dunno why I am making this so big, whatever.
tagcount = 0
fname = filepath.Clean(*file)
pln("\t\t [+] Running PoST on:", fname)
var text []byte
fileContent, err :=os.ReadFile(fname)
if err != nil {
log.Fatal(err)
}
text=fileContent
doc, err := prose.NewDocument(string(text))
if err != nil {
log.Fatal(err)
}
tempFile, err := ioutil.TempFile("./", fname+".goalpost_json-")
if err != nil {
fmt.Println(err)
}
defer tempFile.Close()
fmt.Println("\t [+] Writing PoST to: ", tempFile.Name())
// ********* BUILDING OUR JSON OUTPUT BELOW ***********
JSONOutput.Text=string(doc.Text)
JSONOutput.FullFile=*file
JSONOutput.FileName=fname
JSONOutput.MD5Digest=md5file(file)
var tempBlock TokenizedBlock
for _, ent := range doc.Entities() {
tempBlock.Text = ent.Text
tempBlock.Tag = ""
tempBlock.Label = ent.Label
JSONOutput.GPE = append(JSONOutput.GPE, tempBlock)
tagcount+=1
}
for _, tok := range doc.Tokens() {
tempBlock.Text = tok.Text
tempBlock.Tag = tok.Tag
tempBlock.Label = tok.Label
JSONOutput.Tokens = append(JSONOutput.Tokens,tempBlock)
tagcount+=1
}
// ************* DONE BUILDING OUTPUT ******************
fmt.Println("\t [+] Tagged this many items: ", tagcount)
json.NewEncoder(tempFile).Encode(JSONOutput)
}
func main() {
//demo_tokens_entities_sentences()
var argsvar string
flag.StringVar(&argsvar,"f", "./file.txt", "File to perform Part-Of-Speech-Tagging (PoST) on.")
flag.Parse()
// check if there is somethinig to read on STDIN
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
var stdin []byte
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
stdin = append(stdin, scanner.Bytes()...)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
pln("\t [+] STDIN input found, using it instead of file argument.")
//fmt.Printf("STDIN = %s\n", stdin)
do_tokens(&stdin)
do_entities(&stdin)
pln("\t [+] Remember, to get this output in JSON format use '-f' option instead.")
} else {
pln("\t [+] No input found on STDIN, using filename argument instead")
pln("\t\t [+] filename:", argsvar)
post_file(&argsvar)
}
}