diff --git a/phrase/paragraph.go b/phrase/paragraph.go index f829bbf..fe6a57a 100644 --- a/phrase/paragraph.go +++ b/phrase/paragraph.go @@ -33,6 +33,11 @@ var ( } ) +// Pinyin return paragraph []string +func Pinyin(p string, segs ...gse.Segmenter) []string { + return strings.Split(Paragraph(p, segs...), " ") +} + // Paragraph convert a Chinese string paragraph to pinyin, // including letters, numbers, symbols func Paragraph(p string, segs ...gse.Segmenter) (s string) { diff --git a/phrase/paragraph_test.go b/phrase/paragraph_test.go index d27872a..83cb955 100644 --- a/phrase/paragraph_test.go +++ b/phrase/paragraph_test.go @@ -37,3 +37,10 @@ func TestParagraph(t *testing.T) { } } + +func TestPinyin(t *testing.T) { + seg := gse.New() + WithGse(seg) + p := Pinyin("西雅图太空针") + tt.Equal(t, "[xi ya tu tai kong zhen]", p) +} diff --git a/phrase/phrase.go b/phrase/phrase.go index 6f5b468..f6905e3 100644 --- a/phrase/phrase.go +++ b/phrase/phrase.go @@ -18,6 +18,12 @@ func LoadGseDict(files ...string) error { return seg.LoadDict(files...) } +// WithGse register gse segmenter +func WithGse(segs gse.Segmenter) { + seg = segs + loaded = true +} + // CutWord cut the string word func CutWord(s string) []string { return seg.CutAll(s)