From 90910964bc8beafcb66349cc6776c424e3c00d9c Mon Sep 17 00:00:00 2001 From: ikawaha Date: Thu, 17 Mar 2016 18:28:43 +0900 Subject: [PATCH 1/2] Add a function to get a part-of-speech tag --- tokenizer/token.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tokenizer/token.go b/tokenizer/token.go index 4e282420..e6efbc93 100644 --- a/tokenizer/token.go +++ b/tokenizer/token.go @@ -80,6 +80,15 @@ func (t Token) Features() (features []string) { return } +// Pos returns the first element of features. +func (t Token) Pos() string { + f := t.Features() + if len(f) < 1 { + return "" + } + return f[0] +} + // String returns a string representation of a token. func (t Token) String() string { return fmt.Sprintf("%v(%v, %v)%v[%v]", t.Surface, t.Start, t.End, t.Class, t.ID) From fed9387d5ce22e6d3f074a2c91c2deb0e26e136b Mon Sep 17 00:00:00 2001 From: ikawaha Date: Thu, 17 Mar 2016 18:31:23 +0900 Subject: [PATCH 2/2] Add tests --- tokenizer/token_test.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tokenizer/token_test.go b/tokenizer/token_test.go index 9d987994..f379a361 100644 --- a/tokenizer/token_test.go +++ b/tokenizer/token_test.go @@ -41,7 +41,7 @@ func TestTokenClassString(t *testing.T) { } } -func TestFeatures01(t *testing.T) { +func TestFeaturesAndPos01(t *testing.T) { tok := Token{ ID: 0, Class: TokenClass(lattice.KNOWN), @@ -56,9 +56,12 @@ func TestFeatures01(t *testing.T) { if !reflect.DeepEqual(f, expected) { t.Errorf("got %v, expected %v", f, expected) } + if p := tok.Pos(); p != f[0] { + t.Errorf("got %v, expected %v", p, f[0]) + } } -func TestFeatures02(t *testing.T) { +func TestFeaturesAndPos02(t *testing.T) { tok := Token{ ID: 0, Class: TokenClass(lattice.UNKNOWN), @@ -73,9 +76,12 @@ func TestFeatures02(t *testing.T) { if !reflect.DeepEqual(f, expected) { t.Errorf("got %v, expected %v", f, expected) } + if p := tok.Pos(); p != f[0] { + t.Errorf("got %v, expected %v", p, f[0]) + } } -func TestFeatures03(t *testing.T) { +func TestFeaturesAndPos03(t *testing.T) { tok := Token{ ID: 0, Class: TokenClass(lattice.USER), @@ -95,9 +101,12 @@ func TestFeatures03(t *testing.T) { if !reflect.DeepEqual(f, expected) { t.Errorf("got %v, expected %v", f, expected) } + if p := tok.Pos(); p != f[0] { + t.Errorf("got %v, expected %v", p, f[0]) + } } -func TestFeatures04(t *testing.T) { +func TestFeaturesAndPos04(t *testing.T) { tok := Token{ ID: 0, Class: DUMMY, @@ -116,6 +125,9 @@ func TestFeatures04(t *testing.T) { if len(f) != 0 { t.Errorf("got %v, expected empty", f) } + if p := tok.Pos(); p != "" { + t.Errorf("got %v, expected empty", p) + } } func TestTokenString01(t *testing.T) {