Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ToLower option style same with the cut mode #133

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (seg *Segmenter) Analyze(text []string, t1 string, by ...bool) (az []Analyz
}

isEx := make(map[string]int, 0)
if ToLower {
t1 = strings.ToLower(t1)
}
all := findAllOccs([]byte(t1), text)
for k, v := range text {
if k > 0 && t1 == "" {
Expand All @@ -98,7 +101,9 @@ func (seg *Segmenter) Analyze(text []string, t1 string, by ...bool) (az []Analyz
isEx[v] = 0
}

start = all[v][isEx[v]]
if len(all[v]) > 0 {
start = all[v][isEx[v]]
}
end = start + len([]byte(v))
}

Expand Down Expand Up @@ -212,6 +217,9 @@ func (seg *Segmenter) cutDAG(str string, reg ...*regexp.Regexp) []string {
mLen := int(float32(len(str))/RatioWord) + 1
result := make([]string, 0, mLen)

if ToLower {
str = strings.ToLower(str)
}
runes := []rune(str)
routes := seg.calc(runes)

Expand Down Expand Up @@ -260,6 +268,9 @@ func (seg *Segmenter) cutDAGNoHMM(str string) []string {
mLen := int(float32(len(str))/RatioWord) + 1
result := make([]string, 0, mLen)

if ToLower {
str = strings.ToLower(str)
}
runes := []rune(str)
routes := seg.calc(runes)
length := len(runes)
Expand Down Expand Up @@ -298,6 +309,9 @@ func (seg *Segmenter) cutAll(str string) []string {
mLen := int(float32(len(str))/RatioWord) + 1
result := make([]string, 0, mLen)

if ToLower {
str = strings.ToLower(str)
}
runes := []rune(str)
dag := seg.getDag(runes)
start := -1
Expand Down
8 changes: 4 additions & 4 deletions gse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func TestAnalyze(t *testing.T) {

s := prodSeg.Cut(txt, true)
tt.Equal(t, 23, len(s))
tt.Equal(t, "[城市地标 建筑 : 纽约 帝国大厦 , 旧金山湾 金门大桥 , Seattle Space Needle , Toronto CN Tower , 伦敦 大笨钟]", s)
tt.Equal(t, "[城市地标 建筑 : 纽约 帝国大厦 , 旧金山湾 金门大桥 , seattle space needle , toronto cn tower , 伦敦 大笨钟]", s)

a := prodSeg.Analyze(s, "")
tt.Equal(t, 23, len(a))
tt.Equal(t, "[{0 4 0 0 城市地标 3 j} {4 6 1 0 建筑 14397 n} {6 8 2 0 : 0 } {8 10 3 0 纽约 1758 ns} {10 14 4 0 帝国大厦 3 nr} {14 16 5 0 , 0 } {16 20 6 0 旧金山湾 3 ns} {20 24 7 0 金门大桥 38 nz} {24 26 8 0 , 0 } {26 33 9 0 Seattle 0 } {33 34 10 0 0 } {34 39 11 0 Space 0 } {39 40 12 0 0 } {40 46 13 0 Needle 0 } {46 48 14 0 , 0 } {48 55 15 0 Toronto 0 } {55 56 16 0 0 } {56 58 17 0 CN 0 } {58 59 18 0 0 } {59 64 19 0 Tower 0 } {64 66 20 0 , 0 } {66 68 21 0 伦敦 2255 ns} {68 71 22 0 大笨钟 0 }]", a)
tt.Equal(t, "[{0 4 0 0 城市地标 3 j} {4 6 1 0 建筑 14397 n} {6 8 2 0 : 0 } {8 10 3 0 纽约 1758 ns} {10 14 4 0 帝国大厦 3 nr} {14 16 5 0 , 0 } {16 20 6 0 旧金山湾 3 ns} {20 24 7 0 金门大桥 38 nz} {24 26 8 0 , 0 } {26 33 9 0 seattle 0 } {33 34 10 0 0 } {34 39 11 0 space 0 } {39 40 12 0 0 } {40 46 13 0 needle 0 } {46 48 14 0 , 0 } {48 55 15 0 toronto 0 } {55 56 16 0 0 } {56 58 17 0 cn 0 } {58 59 18 0 0 } {59 64 19 0 tower 0 } {64 66 20 0 , 0 } {66 68 21 0 伦敦 2255 ns} {68 71 22 0 大笨钟 0 }]", a)

tt.Equal(t, 0, a[0].Start)
tt.Equal(t, 4, a[0].End)
Expand All @@ -59,11 +59,11 @@ func TestAnalyze(t *testing.T) {

s = prodSeg.CutSearch(txt, true)
tt.Equal(t, 34, len(s))
tt.Equal(t, "[城市 市地 地标 城市地标 建筑 : 纽约 帝国 国大 大厦 帝国大厦 , 金山 山湾 旧金山 旧金山湾 金门 大桥 金门大桥 , Seattle Space Needle , Toronto CN Tower , 伦敦 大笨钟]", s)
tt.Equal(t, "[城市 市地 地标 城市地标 建筑 : 纽约 帝国 国大 大厦 帝国大厦 , 金山 山湾 旧金山 旧金山湾 金门 大桥 金门大桥 , seattle space needle , toronto cn tower , 伦敦 大笨钟]", s)

a = prodSeg.Analyze(s, txt)
tt.Equal(t, 34, len(a))
tt.Equal(t, "[{0 6 0 0 城市 25084 ns} {3 9 1 0 市地 11 n} {6 12 2 0 地标 32 n} {0 12 3 0 城市地标 3 j} {12 18 4 0 建筑 14397 n} {18 20 5 0 : 0 } {20 26 6 0 纽约 1758 ns} {26 32 7 0 帝国 3655 n} {29 35 8 0 国大 114 j} {32 38 9 0 大厦 777 n} {26 38 10 0 帝国大厦 3 nr} {104 106 11 0 , 0 } {43 49 12 0 金山 291 nr} {46 52 13 0 山湾 7 ns} {40 49 14 0 旧金山 238 ns} {40 52 15 0 旧金山湾 3 ns} {52 58 16 0 金门 149 n} {58 64 17 0 大桥 3288 ns} {52 64 18 0 金门大桥 38 nz} {86 88 19 0 , 0 } {66 73 20 0 Seattle 0 } {105 106 21 0 0 } {74 79 22 0 Space 0 } {98 99 23 0 0 } {80 86 24 0 Needle 0 } {64 66 25 0 , 0 } {88 95 26 0 Toronto 0 } {95 96 27 0 0 } {96 98 28 0 CN 0 } {87 88 29 0 0 } {99 104 30 0 Tower 0 } {38 40 31 0 , 0 } {106 112 32 0 伦敦 2255 ns} {112 121 33 0 大笨钟 0 }]", a)
tt.Equal(t, "[{0 6 0 0 城市 25084 ns} {3 9 1 0 市地 11 n} {6 12 2 0 地标 32 n} {0 12 3 0 城市地标 3 j} {12 18 4 0 建筑 14397 n} {18 20 5 0 : 0 } {20 26 6 0 纽约 1758 ns} {26 32 7 0 帝国 3655 n} {29 35 8 0 国大 114 j} {32 38 9 0 大厦 777 n} {26 38 10 0 帝国大厦 3 nr} {104 106 11 0 , 0 } {43 49 12 0 金山 291 nr} {46 52 13 0 山湾 7 ns} {40 49 14 0 旧金山 238 ns} {40 52 15 0 旧金山湾 3 ns} {52 58 16 0 金门 149 n} {58 64 17 0 大桥 3288 ns} {52 64 18 0 金门大桥 38 nz} {86 88 19 0 , 0 } {66 73 20 0 seattle 0 } {105 106 21 0 0 } {74 79 22 0 space 0 } {98 99 23 0 0 } {80 86 24 0 needle 0 } {64 66 25 0 , 0 } {88 95 26 0 toronto 0 } {95 96 27 0 0 } {96 98 28 0 cn 0 } {87 88 29 0 0 } {99 104 30 0 tower 0 } {38 40 31 0 , 0 } {106 112 32 0 伦敦 2255 ns} {112 121 33 0 大笨钟 0 }]", a)
}

func TestHMM(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions segmenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ func TestInAlphaNum(t *testing.T) {

tx = seg.Cut(text, false)
tt.Equal(t, 11, len(tx))
tt.Equal(t, "[hello world ! 你好 世界 , Hello world .]", tx)
tt.Equal(t, "[hello world ! 你好 世界 , hello world .]", tx)

tx = seg.Cut(text, true)
tt.Equal(t, 9, len(tx))
tt.Equal(t, "[hello world ! 你好 世界 , Hello world .]", tx)
tt.Equal(t, "[hello world ! 你好 世界 , hello world .]", tx)
}