-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextParse.go
156 lines (135 loc) · 3.69 KB
/
textParse.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
package main
import (
"os"
"fmt"
"flag"
"bufio"
"strings"
"strconv"
"os/exec"
"syscall"
"encoding/csv"
"github.com/Arcania0311/textParse/wordNet"
)
/*****
* Variables used for command line parsing.
*/
var (
FRAMEWIDTH int
FAMLTHRESHOLD int
fileName string
)
func main() {
// Parse command line flags first.
flag.IntVar(&FAMLTHRESHOLD, "faml", 2, "The polysemy count at which a word is tokenised.")
flag.IntVar(&FRAMEWIDTH, "frame", 5, "The number of tokens per frame.")
flag.StringVar(&fileName, "file", "goTest", "Name of the .txt file to be processed.")
flag.Parse()
// Set the famlthreshold
wordNet.SetFAMLTHRESHOLD(FAMLTHRESHOLD)
// Open the file.
lines, err := importLines("./texts/" + fileName + ".txt")
if err != nil {
fmt.Println("readLines: %s", err)
}
// Create a map in which the words are stored.
words := make(map[string]*wordNet.Token)
text := make([]*wordNet.Token, 0)
// Ugly way to get rid of the most common words.
words["a"] = nil
words["an"] = nil
words["is"] = nil
words["as"] = nil
words["by"] = nil
words["of"] = nil
words["be"] = nil
words["to"] = nil
words["in"] = nil
words["are"] = nil
words["and"] = nil
words["the"] = nil
// Process all the lines.
for _, line := range lines {
line = strings.ToLower(line)
splitLine := strings.Split(line, " ")
// Tokenize the words.
for _, tmp := range splitLine {
// Remove special characters from the string.
stripWord := func(r rune) rune {
switch {
case r < 'a' || r > 'z':
return -1
}
return r
}
tmp = strings.Map(stripWord, tmp)
// Append all non-nil tokens to the text slice.
token, ok := words[tmp]
if (ok && token != nil) {
// This makes sure all words are only tokenised once.
text = append(text, token)
} else if (ok && token == nil) {
continue
} else {
newToken := wordNet.CreateToken(tmp)
words[tmp] = newToken
if (newToken != nil) {
text = append(text, newToken)
}
}
}
}
results := make([]int, 0)
totalScore := 0
// Process the results.
for x := 0; x < len(text) - FRAMEWIDTH; x++ {
score := 0
for frameIndex := 1; frameIndex <= FRAMEWIDTH; frameIndex++ {
score += wordNet.CompareTokens(text[x], text[x + frameIndex])
}
score = score / FRAMEWIDTH
totalScore += score
results = append(results, score)
}
totalScore = totalScore / (len(text) - FRAMEWIDTH)
// Create a csv file to store the results.
resultsFile := fileName + "-" + strconv.Itoa(FRAMEWIDTH) + "-" + strconv.Itoa(FAMLTHRESHOLD) + "-" + strconv.Itoa(totalScore)
csvFile, err := os.Create("results/" + resultsFile + ".csv")
out := csv.NewWriter(csvFile)
for index, value := range results {
csvLine := make([]string, 3)
csvLine[0] = strconv.Itoa(index)
csvLine[1] = strconv.Itoa(value)
csvLine[2] = text[index].Word
out.Write(csvLine)
}
out.Flush()
csvFile.Close()
// Open the file with the JavaScript script.
binary, lookErr := exec.LookPath("node")
if lookErr != nil {
panic(lookErr)
}
args := []string{"node", "results/visual/showResults.js", resultsFile}
env := os.Environ()
execErr := syscall.Exec(binary, args, env)
if execErr != nil {
panic(execErr)
}
}
/*****
* Open the specified file and return a slice of its lines.
*/
func importLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}