-
Notifications
You must be signed in to change notification settings - Fork 7
/
node.go
118 lines (97 loc) · 2.44 KB
/
node.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
package mecab
// #include <mecab.h>
// #include <stdlib.h>
import "C"
import (
"errors"
"fmt"
"strconv"
"unicode/utf8"
)
type Node struct {
// pointer to the first node
head *C.mecab_node_t
// pointer to he next node
current *C.struct_mecab_node_t
}
var StopIteration = errors.New("StopIteration")
// proceed to the next node.
// if current node is last, this method returns StopIteration error.
func (node *Node) Next() error {
node.current = node.current.next
if node.current == nil {
return StopIteration
}
return nil
}
// surface string
func (node *Node) Surface() string {
current := node.current
return C.GoStringN(current.surface, C.int(current.length))
}
// feature string
func (node *Node) Feature() string {
return C.GoString(node.current.feature)
}
// unique node id
func (node *Node) Id() int {
return int(node.current.id)
}
// length of the surface form.
func (node *Node) Length() int {
return int(node.current.length)
}
// length of the surface form including white space before the morph.
func (node *Node) Rlength() int {
return int(node.current.rlength)
}
// right attribute id
func (node *Node) RcAttr() int {
return int(node.current.rcAttr)
}
// left attribute id
func (node *Node) LcAttr() int {
return int(node.current.lcAttr)
}
// unique part of speech id.
func (node *Node) Posid() int {
return int(node.current.posid)
}
// character type
func (node *Node) Char_type() int {
return int(node.current.char_type)
}
// status of this model.
func (node *Node) Stat() int {
return int(node.current.stat)
}
// set 1 if this node is best node.
func (node *Node) Isbest() int {
return int(node.current.isbest)
}
// forward accumulative log summation.
func (node *Node) Alpha() float32 {
return float32(node.current.alpha)
}
// backward accumulative log summation.
func (node *Node) Beta() float32 {
return float32(node.current.beta)
}
// marginal probability.
func (node *Node) Prob() float32 {
return float32(node.current.prob)
}
// word cost.
func (node *Node) Wcost() int {
return int(node.current.wcost)
}
// best accumulative cost from bos node to this node.
func (node *Node) Cost() int {
return int(node.current.cost)
}
// start pos in the text.
func (node *Node) StartPos() int {
pFirstNode, _ := strconv.Atoi(fmt.Sprintf("%d", node.head.surface))
pCurrentNode, _ := strconv.Atoi(fmt.Sprintf("%d", node.current.surface))
return utf8.RuneCountInString(C.GoStringN(node.head.surface, C.int(pCurrentNode-pFirstNode)))
}