2014-08-03 23:46:35 +02:00
|
|
|
package analysis
|
2014-07-31 01:29:52 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-08-03 23:46:35 +02:00
|
|
|
type WordMap map[string]bool
|
2014-07-31 01:29:52 +02:00
|
|
|
|
2014-08-03 23:46:35 +02:00
|
|
|
func NewWordMap() WordMap {
|
|
|
|
return make(WordMap, 0)
|
2014-07-31 01:29:52 +02:00
|
|
|
}
|
|
|
|
|
2014-08-03 23:46:35 +02:00
|
|
|
func (s WordMap) LoadFile(filename string) error {
|
2014-07-31 01:29:52 +02:00
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.LoadBytes(data)
|
|
|
|
}
|
|
|
|
|
2014-08-03 23:46:35 +02:00
|
|
|
func (s WordMap) LoadBytes(data []byte) error {
|
2014-07-31 01:29:52 +02:00
|
|
|
bytesReader := bytes.NewReader(data)
|
|
|
|
bufioReader := bufio.NewReader(bytesReader)
|
|
|
|
line, err := bufioReader.ReadString('\n')
|
|
|
|
for err == nil {
|
|
|
|
s.LoadLine(line)
|
|
|
|
line, err = bufioReader.ReadString('\n')
|
|
|
|
}
|
|
|
|
// if the err was EOF still need to process last value
|
|
|
|
if err == io.EOF {
|
|
|
|
s.LoadLine(line)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-03 23:46:35 +02:00
|
|
|
func (s WordMap) LoadLine(line string) error {
|
2014-07-31 01:29:52 +02:00
|
|
|
// find the start of comment, if any
|
|
|
|
startComment := strings.IndexAny(line, "#|")
|
|
|
|
if startComment >= 0 {
|
|
|
|
line = line[:startComment]
|
|
|
|
}
|
|
|
|
|
2014-08-03 23:46:35 +02:00
|
|
|
words := strings.Fields(line)
|
|
|
|
for _, word := range words {
|
|
|
|
s.AddWord(word)
|
2014-07-31 01:29:52 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-03 23:46:35 +02:00
|
|
|
func (s WordMap) AddWord(word string) {
|
2014-07-31 01:29:52 +02:00
|
|
|
s[word] = true
|
|
|
|
}
|