Skip to content

Commit

Permalink
use map of literal to kanji details
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnps committed Mar 3, 2014
1 parent 98694b0 commit 49366e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
11 changes: 7 additions & 4 deletions kanjidic2.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ type Kanji struct {
Nanori []string `xml:"nanori"`
}

func ParseKanjiDic2(filename string) (kanjiList []Kanji, err error) {
type Kanjidic2Parser map[string]Kanji

func ParseKanjiDic2(filename string) (Kanjidic2Parser, error) {
k := Kanjidic2Parser{}
xmlFile, err := os.Open(filename)
if err != nil {
return nil, err
return k, err
}
defer xmlFile.Close()
decoder := xml.NewDecoder(xmlFile)
Expand All @@ -87,9 +90,9 @@ func ParseKanjiDic2(filename string) (kanjiList []Kanji, err error) {
if startElement.Name.Local == "character" {
var kanji Kanji
decoder.DecodeElement(&kanji, &startElement)
kanjiList = append(kanjiList, kanji)
k[kanji.Literal] = kanji
}
}
}
return
return k, nil
}
20 changes: 7 additions & 13 deletions kanjidic2_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package kanjidic2

import (
"testing"
)
import "testing"

var numKanji = 13108
var kanjiList []Kanji
var kanjidic2Parser Kanjidic2Parser

func TestParser(t *testing.T) {
got, err := ParseKanjiDic2("kanjidic2.xml")
Expand All @@ -16,14 +14,14 @@ func TestParser(t *testing.T) {
t.Fatalf("ParseKanjiDic2 length incorrect: got %d, want %d", got, numKanji)
}
// set kanjiList for later tests
kanjiList = got
kanjidic2Parser = got
}

var strokeCountTests = []struct {
Literal string
Count int
}{
{"", 1},
{"", 1},
{"二", 2},
{"三", 3},
{"中", 4},
Expand All @@ -33,13 +31,9 @@ var strokeCountTests = []struct {
}

func TestStrokeCount(t *testing.T) {
for _, k := range kanjiList {
for _, tt := range strokeCountTests {
if k.Literal == tt.Literal {
if got := k.StrokeCount; got != tt.Count {
t.Errorf("TestStrokeCount (%s): got %d, want %d", tt.Literal, got, tt.Count)
}
}
for _, tt := range strokeCountTests {
if got := kanjidic2Parser[tt.Literal].StrokeCount; got != tt.Count {
t.Errorf("TestStrokeCount (%s): got %d, want %d", tt.Literal, got, tt.Count)
}
}
}

0 comments on commit 49366e3

Please sign in to comment.