/
nlputil.go
181 lines (149 loc) · 3.78 KB
/
nlputil.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
package nlp
import (
"bufio"
"context"
"fmt"
"github.com/sirupsen/logrus"
"github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils"
"github.com/ztrue/tracerr"
"golang.org/x/sync/errgroup"
"io"
"regexp"
"strings"
"unicode/utf8"
)
var chineseSplitReg = regexp.MustCompile(`(?U)(.*[\s\x{3002}\x{ff1b}\x{ff0c}\x{ff1a}\x{201c}\x{201d}\x{ff08}\x{ff09}\x{3001}\x{ff1f}\x{300a}\x{300b}])`)
type CorpusCleaner interface {
DoClean(text string) string
GetSeparator() string
DoSplit(text string) []string
}
func RemoveUrl(text string) string {
re := regexp.MustCompile(`(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]`)
return re.ReplaceAllString(text, "")
}
func RemoveNonChinese(text string) string {
re := regexp.MustCompile(`[^\p{Han}\d\s\x{3002}\x{ff1b}\x{ff0c}\x{ff1a}\x{201c}\x{201d}\x{ff08}\x{ff09}\x{3001}\x{ff1f}\x{300a}\x{300b}]`)
return re.ReplaceAllString(text, "")
}
func CleanChineseText(text string) string {
return strings.TrimSpace(RemoveNonChinese(RemoveUrl(text)))
}
type ChineseCorpusCleaner struct{}
func (self *ChineseCorpusCleaner) DoClean(text string) string {
return CleanChineseText(text)
}
func (self *ChineseCorpusCleaner) GetSeparator() string {
return "。"
}
func (self *ChineseCorpusCleaner) DoSplit(text string) []string {
splits := chineseSplitReg.FindAllStringSubmatch(text, -1)
var ret []string
for _, split := range splits {
if stringutils.IsNotEmpty(split[1]) && utf8.RuneCountInString(split[1]) > 1 {
ret = append(ret, split[1])
}
}
return ret
}
type EnglishCorpusCleaner struct{}
//TODO
func (self *EnglishCorpusCleaner) DoClean(text string) string {
panic("implement me!")
}
func (self *EnglishCorpusCleaner) GetSeparator() string {
return "."
}
func (self *EnglishCorpusCleaner) DoSplit(text string) []string {
panic("implement me!")
}
type MixCorpusCleaner struct{}
//TODO
func (self *MixCorpusCleaner) DoClean(text string) string {
panic("implement me")
}
//TODO
func (self *MixCorpusCleaner) GetSeparator() string {
panic("implement me")
}
//TODO
func (self *MixCorpusCleaner) DoSplit(text string) []string {
panic("implement me!")
}
func ProcessLine(r io.Reader, max int, threshold int, cleaner CorpusCleaner, callback func(s string) error) error {
reader := bufio.NewReader(r)
var err error
hits := make(chan string)
g, ctx := errgroup.WithContext(context.Background())
g.Go(func() error {
defer close(hits)
for {
var line []byte
line, err = reader.ReadBytes('\n')
if err != nil && err != io.EOF {
err = tracerr.Wrap(err)
return err
}
sline := cleaner.DoClean(string(line))
lineLength := utf8.RuneCount([]byte(sline))
if max < 0 || lineLength <= max {
select {
case hits <- sline:
case <-ctx.Done():
return ctx.Err()
}
} else {
splits := cleaner.DoSplit(sline)
var index int
for {
if index >= len(splits) {
break
}
var newline string
for {
if utf8.RuneCount([]byte(newline)) >= max || index >= len(splits) {
break
}
newline += splits[index]
index++
}
if utf8.RuneCount([]byte(newline)) > threshold {
logrus.Println(lineLength)
logrus.Println(newline)
return tracerr.New(fmt.Sprintf("单行字符数超过%d,请调整后重试", threshold))
}
select {
case hits <- newline:
case <-ctx.Done():
return ctx.Err()
}
}
}
if err == io.EOF {
break
}
}
return nil
})
for i := 0; i < 1; i++ {
g.Go(func() error {
for hit := range hits {
select {
case <-ctx.Done():
return ctx.Err()
default:
if err = callback(hit); err != nil {
err = tracerr.Wrap(err)
return err
}
}
}
return nil
})
}
if err := g.Wait(); err != nil {
err = tracerr.Wrap(err)
return err
}
return nil
}