-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtext.go
261 lines (221 loc) · 6.59 KB
/
text.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package gcse
import (
"log"
"regexp"
"strings"
"github.com/golangplus/bytes"
)
/*
Text analyzes
*/
var patMultiReturns = regexp.MustCompile(`\n\n+`)
var patMultiSpaces = regexp.MustCompile(`\s+`)
var patHeaderBottom = regexp.MustCompile(`((----+)|(====+))\n`)
var patMess = regexp.MustCompile(`((--+==+)|(==+--+))`)
func SplitSentences(text string) []string {
text = strings.TrimSpace(text)
text = patHeaderBottom.ReplaceAllString(text, "\n")
text = patMess.ReplaceAllString(text, "")
var sents []string
for _, line := range patMultiReturns.Split(text, -1) {
line = strings.TrimSpace(line)
line = strings.Replace(line, "\n", " ", -1)
rawSents := strings.Split(line, ". ")
for i, sent := range rawSents {
if i != len(rawSents)-1 {
sent = sent + "."
}
sent = patMultiSpaces.ReplaceAllString(sent, " ")
sents = append(sents, sent)
}
}
return sents
}
const (
reJust = `just`
reA = `(an|a|the)`
reThe = `(this|the)`
reThis = `(this|it|these|they)`
reBasic = `(basic|helper|naive|simple|small|unofficial)`
reSome = `((a few)|some|(a set of))`
reGo = `(pure(-| ))?` + `go` + `(lang|( programming)|(( programming)? language))?`
reImplementation = `(binding|code|implementation|interface|lib|library|package|plugin|port|program|project|repository|repositorie|tool|utilitie|utility|version|wrapper)`
reWill = `(will|would|shall|can|could|(is going to))`
reImplement = `(allow|analyze|contain|create|find|implement|perform|provide|wrap)`
reImplementing = `(allow|analyz|contain|creat|find|implement|perform|provid|wrapp)ing`
reIs = `(is|are|be)`
reOf = `(of|for|to)`
reVerbSuffix = `(s|ed|d)?`
)
// optional prefix
func op(re string) string {
return `(` + re + ` )?`
}
// a nount
func n(noun string) string {
return noun + `s?`
}
// a verb
func v(verb string) string {
return op(reWill) + verb + reVerbSuffix
}
var (
isPrefixes = []string{
`这是一个`,
`这个项目是`,
op(reJust) + op(reA) + op(reBasic) + op(reGo) + n(reImplementation) + ` ` + reOf + ` `,
op(op(reThe)+op(reGo)+n(reImplementation)) + v(reImplement) + ` `,
op(reGo) + op(reBasic) + n(reImplementation) + ` that `,
op(reA) + op(reBasic) + reGo + ` `,
`extended version of ` + reGo + `'s template ` + n(reImplementation) + ` ` + reOf + ` `,
reA + ` ` + reImplementation + ` ` + reImplementing + ` `,
reThe + ` ` + reImplementation + ` ` + v(`offer`) + ` ` + reA + ` `,
reThe + ` ` + reImplementation + ` is designed ` + reOf + ` `,
reThe + ` ` + reImplementation + ` ` + v(`allow`) + ` ` + reGo + ` `,
reThe + ` goal of ` + reThe + ` ` + reImplementation + ` is `,
reThe + ` ` + reImplementation + ` ((can be used)|(allows us)) to `,
reThe + ` ` + reImplementation + ` ` + reWill + ` `,
reThis + ` ` + n(reImplement) + ` `,
op(reThis) + op(reGo) + op(n(reImplementation)) + reIs + ` ` + reA + ` `,
op(reJust) + reSome + ` ` + n(`function`) + ` ` + reOf + ` `,
reGo + ` ?的`,
`api ` + reOf + ` `,
`to ` + reImplement + ` `,
`yet another `,
`solution ` + reOf + ` `,
}
isVerbs = []string{
` ` + op(reJust) + v(reImplement) + ` `,
` ` + reIs + ` ` + op(`more than`) + reA + ` `,
` ?是一个`,
` -+ `,
`[,:] `,
` module helps to `,
` ` + n(`make`) + ` interface ` + reOf + ` `,
` ` + reImplementation + ` ` + v(reImplement) + ` `,
` is ` + reGo + ` ` + reImplementation + ` ` + reOf + ` `,
` ` + reIs + ` supposed to `,
` ` + reIs + ` intended to `,
}
isSuffixes = []string{
` (for|in) ` + op(reThe) + reGo,
` using ` + reGo,
}
isWrap = [][2]string{
{reA + ` `, ` ` + reImplementation},
}
patISPrefixes = regexp.MustCompile(`^((` + strings.Join(isPrefixes, `)|(`) + `))`)
patISSuffixes = regexp.MustCompile(`((` + strings.Join(isSuffixes, `)|(`) + `))[.]?\s*(` + reLinkAnchor + `)?$`)
patISWrap *regexp.Regexp
reVerbs = `((` + strings.Join(isVerbs, `)|(`) + `))`
// ![Build Status](https://secure.travis-ci.org/prometheus/client_golang.png?branch=master)
reLinkAnchor = `!\[.*?\][(].+?[)]`
patLinkAnchor *regexp.Regexp = regexp.MustCompile(reLinkAnchor)
)
func init() {
reWrap := ""
for _, pair := range isWrap {
if reWrap != "" {
reWrap += `|`
}
reWrap += `(` + pair[0] + `.+` + pair[1] + `)`
}
patISWrap = regexp.MustCompile(`^(` + reWrap + `)[.]?$`)
}
func reEscapeRune(r rune) string {
switch r {
case '+', '?', '*', '\\':
return "[" + string(r) + "]"
}
return string(r)
}
func reEscapeString(s string) string {
if strings.IndexAny(s, "+?*\\") < 0 {
return s
}
var buf bytesp.Slice
for _, r := range s {
buf.WriteString(reEscapeRune(r))
}
return string(buf)
}
func reName(name string) string {
var res bytesp.Slice
p := len(name)
if strings.HasSuffix(name, "d") || strings.HasSuffix(name, "s") {
p = len(name) - 1
} else if strings.HasSuffix(name, "-go") {
p = len(name) - 3
}
for i, r := range name {
if i > 0 {
res.WriteString(" ?")
}
res.WriteString(reEscapeRune(r))
if i >= p {
res.WriteRune('?')
}
}
return string(res)
}
func removeBrackets(sent string) string {
for {
l := strings.IndexAny(sent, "(")
if l < 0 {
break
}
r := strings.IndexAny(sent[l+1:], ")")
if r < 0 {
break
}
r += l + 1
if l > 0 && sent[l-1] == byte(' ') && r < len(sent)-1 && sent[r+1] == byte(' ') {
// if both ends are spaces, increase r to reduce one
r++
}
sent = sent[:l] + sent[r+1:]
}
return strings.TrimSpace(sent)
}
func ChooseImportantSentenses(text string, name, pkg string) []string {
sents := SplitSentences(text)
if len(sents) < 1 {
return nil
}
name = strings.ToLower(name)
var namePrefix bytesp.Slice
namePrefix.WriteString(op(reThe) + `(`)
parts := strings.Split(strings.ToLower(pkg), "/")
for i := range parts {
word := strings.Join(parts[i:], "/")
if i == len(parts)-1 {
word = `(go )?` + reName(word) + `([.]go)?`
} else {
word = reEscapeString(word)
}
re := `(` + word + `)|`
namePrefix.WriteString(re)
}
if name != "" && name != parts[len(parts)-1] {
namePrefix.WriteString(`((go )?` + reName(name) + `([.]go)?)`)
}
namePrefix.WriteString(`)( package)?`)
re := `^(package )?(` + string(namePrefix) + reVerbs + `)`
pat, err := regexp.Compile(re)
if err != nil {
log.Printf("regexp.Compile %s failed: %v", re, err)
}
//log.Printf("pat: %v", pat)
var importants []string
for _, sent := range sents {
lowerSent := removeBrackets(strings.ToLower(sent))
if patISPrefixes.MatchString(lowerSent) ||
patISSuffixes.MatchString(lowerSent) ||
patISWrap.MatchString(lowerSent) ||
pat != nil && pat.MatchString(lowerSent) {
sent = patLinkAnchor.ReplaceAllString(sent, "")
importants = append(importants, sent)
}
}
return importants
}