forked from veer66/mapkha
/
pat_edge_builder.go
49 lines (43 loc) · 987 Bytes
/
pat_edge_builder.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
package mapkha
type runePatPredicate func(rune) bool
type PatEdgeBuilder struct {
s int
foundS bool
foundE bool
isPat runePatPredicate
edgeType Etype
}
func (builder *PatEdgeBuilder) Build(context *EdgeBuildingContext) *Edge {
if !builder.foundS {
if builder.isPat(context.Ch) {
builder.s = context.I
builder.foundS = true
}
}
if builder.foundS {
if builder.isPat(context.Ch) {
if len(context.runes) == context.I+1 ||
!builder.isPat(context.runes[context.I+1]) {
builder.foundE = true
}
} else {
builder.foundS = false
builder.foundE = false
}
}
if builder.foundS && builder.foundE {
source := context.Path[builder.s]
builder.foundS = false
builder.foundE = false
return &Edge{S: builder.s,
EdgeType: builder.edgeType,
WordCount: source.WordCount + 1,
UnkCount: source.UnkCount}
}
return nil
}
func (builder *PatEdgeBuilder) Reset() {
builder.foundS = false
builder.foundE = false
builder.s = 0
}