-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_tree.go
113 lines (98 loc) · 2.17 KB
/
compute_tree.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
package gocyk
import (
"log"
"sync"
grm "github.com/ghigt/gocyk/grammar"
"github.com/ghigt/gocyk/ptree"
)
func (g *GoCYK) checkRight(col int, row int, tok grm.Token) bool {
itm := g.Table.GetItem(col, row)
for _, t := range itm.GetTokens() {
if t == tok {
return true
}
}
return false
}
func (g *GoCYK) buildTree(tok grm.Token, row int, col int) *ptree.PTree {
pt := ptree.New(tok)
if col == row {
return pt
} else {
for _, nt := range g.Grammar.GetListOfNT(tok) {
left, _ := nt.GetLeft()
right, _ := nt.GetRight()
for c := col - 1; c >= row; c-- {
if itm := g.Table.GetItem(c, row); itm.IsEmpty() != true {
for _, t := range itm.GetTokens() {
if t == left && g.checkRight(col, c+1, right) {
pt.Left = g.buildTree(left, row, c)
pt.Right = g.buildTree(right, c+1, col)
return pt
}
}
}
}
}
}
return nil
}
func (g *GoCYK) intermediate(c chan<- *ptree.PTree, wg *sync.WaitGroup, tok grm.Token, row int, col int) {
}
func (g *GoCYK) BuildTrees() []*ptree.PTree {
size := g.Table.Size()
pts := []*ptree.PTree{}
c := make(chan *ptree.PTree)
var wg sync.WaitGroup
go func() {
for row := 0; row < size; {
col := size - 1
for ; col >= row; col-- {
itm := g.Table.GetItem(col, row)
if itm.IsEmpty() == false {
for _, tok := range itm.GetTokens() {
wg.Add(1)
go func(tok grm.Token, row int, col int) {
defer wg.Done()
if tree := g.buildTree(tok, row, col); tree != nil {
c <- tree
} else {
log.Fatal("nil Tree")
}
}(tok, row, col)
}
break
}
}
row = col + 1
}
wg.Wait()
close(c)
}()
for p := range c {
pts = append(pts, p)
}
return pts
}
func (g *GoCYK) BuildTreesNotC() []*ptree.PTree {
size := g.Table.Size()
pts := []*ptree.PTree{}
for row := 0; row < size; {
col := size - 1
for ; col >= row; col-- {
itm := g.Table.GetItem(col, row)
if itm.IsEmpty() == false {
for _, tok := range itm.GetTokens() {
if tree := g.buildTree(tok, row, col); tree != nil {
pts = append(pts, tree)
} else {
log.Fatal("nil Tree")
}
}
break
}
}
row = col + 1
}
return pts
}