-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.go
136 lines (111 loc) · 3.27 KB
/
tokenizer.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
// This file is part of GoIRS.
//
// GoIRS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GoIRS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GoIRS. If not, see <http://www.gnu.org/licenses/>.
package goirs
import (
"bufio"
"io"
"os"
"regexp"
"strings"
"unicode"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
var (
notallowed = regexp.MustCompile("[^\\p{L}[:digit:]_-]+")
)
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
func isNull(r rune) bool {
return r == 0 || r == '-'
}
//CleanToken elimina caracteres extraños de un token y normaliza los acentos
func CleanToken(oldToken string) string {
oldToken = strings.Replace(oldToken, "ñ", "*", -1)
//------------------------------------------------------
// Aquí comienza un bloque de código copiado de StackOverflow...
b := make([]byte, len(oldToken))
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
_, _, e := t.Transform(b, []byte(oldToken), true)
if e != nil {
return ""
}
//Fin del código de StackOverflow
//-----------------------------------------------------
return strings.TrimFunc(strings.Replace(strings.ToLower(string(b)), "*", "ñ", -1), isNull)
}
func cleanToken(in <-chan string, out chan string) {
defer close(out)
for currstr := range in {
if token := CleanToken(currstr); len(token) > 0 {
out <- token
}
}
}
func tokenizeWords(in <-chan string, out chan string) {
defer close(out)
for currstr := range in {
currstr = notallowed.ReplaceAllString(currstr, " ")
for _, x := range strings.Split(currstr, " ") {
if len(x) > 0 {
out <- x
}
}
}
}
func tokenizeSpaces(in *bufio.Scanner, out chan string) {
defer close(out)
for in.Scan() {
currstr := in.Text()
out <- currstr
}
}
//TokenizerIterator devuelve un canal que suelta tokens...
func TokenizerIterator(input io.Reader) StringIterator {
scanner := bufio.NewScanner(input)
scanner.Split(bufio.ScanWords)
uno := make(chan string, BUFFERSIZE)
dos := make(chan string, BUFFERSIZE)
tres := make(chan string, BUFFERSIZE)
go tokenizeSpaces(scanner, uno)
go tokenizeWords(uno, dos)
go cleanToken(dos, tres)
return tres
}
func tokenWrite(file *os.File, in <-chan string, out chan string) {
defer close(out)
defer file.Close()
for token := range in {
out <- token
file.WriteString(token)
file.WriteString("\n")
}
}
//TokenizerWriterIterator Igual que TokenizerIterator, pero que escribe los
//tokens en el fichero especificado si write es true
func TokenizerWriterIterator(input io.Reader, file string, write bool) StringIterator {
if write {
out := make(chan string, BUFFERSIZE)
in := TokenizerIterator(input)
dest, err := os.Create(file)
if err != nil {
panic(err)
}
go tokenWrite(dest, in, out)
return out
}
return TokenizerIterator(input)
}