Skip to content

Commit

Permalink
Merge pull request #66 from ikawaha/fix/lattice_graph_20160226
Browse files Browse the repository at this point in the history
Remove unused UNK nodes from a lattice graph
  • Loading branch information
ikawaha committed Feb 27, 2016
2 parents 2dc401f + e274807 commit 7f10368
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
9 changes: 6 additions & 3 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/kagome/server/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (h *TokenizeDemoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
case "Search":
m = tokenizer.Search
case "Extended":
m = tokenizer.Extended
// use Normal mode
}
if _, e := exec.LookPath(graphvizCmd); e != nil {
cmdErr = "Error: circo/graphviz is not installed in your $PATH"
Expand Down
25 changes: 20 additions & 5 deletions internal/lattice/lattice.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ func (la *Lattice) Backward(m TokenizeMode) {

// Dot outputs the lattice in the graphviz dot format.
func (la *Lattice) Dot(w io.Writer) {
bests := make(map[*node]struct{})
for _, n := range la.Output {
bests[n] = struct{}{}
}
type edge struct {
from *node
to *node
Expand All @@ -293,26 +297,37 @@ func (la *Lattice) Dot(w io.Writer) {
for i, size := 1, len(la.list); i < size; i++ {
currents := la.list[i]
for _, to := range currents {
if to.Class == UNKNOWN {
if _, ok := bests[to]; !ok {
continue
}
}
prevs := la.list[to.Start]
if len(prevs) == 0 {
continue
}
for _, from := range prevs {
if from.Class == UNKNOWN {
if _, ok := bests[from]; !ok {
continue
}
}
edges = append(edges, edge{from, to})
}
}
}
bests := make(map[*node]struct{})
for _, n := range la.Output {
bests[n] = struct{}{}
}
fmt.Fprintln(w, "graph lattice {")
fmt.Fprintln(w, "dpi=48;")
fmt.Fprintln(w, "graph [style=filled, splines=true, overlap=false, fontsize=30, rankdir=LR]")
fmt.Fprintln(w, "edge [fontname=Helvetica, fontcolor=red, color=\"#606060\"]")
fmt.Fprintln(w, "node [shape=box, style=filled, fillcolor=\"#e8e8f0\", fontname=Helvetica]")
for i, list := range la.list {
for _, n := range list {
if n.Class == UNKNOWN {
if _, ok := bests[n]; !ok {
continue
}
}
surf := n.Surface
if n.ID == BosEosID {
if i == 0 {
Expand All @@ -323,7 +338,7 @@ func (la *Lattice) Dot(w io.Writer) {
}
if _, ok := bests[n]; ok {
fmt.Fprintf(w, "\t\"%p\" [label=\"%s\\n%d\",shape=ellipse, peripheries=2];\n", n, surf, n.Weight)
} else {
} else if n.Class != UNKNOWN {
fmt.Fprintf(w, "\t\"%p\" [label=\"%s\\n%d\"];\n", n, surf, n.Weight)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lattice/lattice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ node [shape=box, style=filled, fillcolor="#e8e8f0", fontname=Helvetica]
t.Errorf("got %v, expected: %v", b.String(), expected)
}
b.Reset()
la.Build("わたしまけましたわ")
la.Build("わたしまけましたわポポピ")
la.Forward(Normal)
la.Backward(Normal)
la.Dot(&b)
Expand Down

0 comments on commit 7f10368

Please sign in to comment.