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

Optimize script detection #1

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
157 changes: 157 additions & 0 deletions unidetect_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package unidetect

import (
"fmt"
"reflect"
"sort"
"strings"
"testing"
"unicode"
)

var w = strings.Fields
Expand Down Expand Up @@ -43,3 +46,157 @@ func TestDetect(t *testing.T) {
}

}

func TestUnicodeScripts(t *testing.T) {
scriptNames := make([]string, 0, len(unicode.Scripts))
for name := range unicode.Scripts {
scriptNames = append(scriptNames, name)
}
sort.Strings(scriptNames)

sortedNames := scriptNames[:]
sort.Sort(ByRanges(sortedNames))

printScripts(t, sortedNames)
}

func printScripts(t *testing.T, scriptNames []string) {
for _, name := range scriptNames {
script := unicode.Scripts[name]
nranges := len(script.R16) + len(script.R32)
n := rangeTableCount(script)
lo := rangeTableLo(script)
hi := rangeTableHi(script)

var o []string
for _, name2 := range scriptNames {
if name2 == name {
continue
}
script2 := unicode.Scripts[name2]
if intersects(script, script2) {
o = append(o, name2)
}
}
fmt.Printf("%d\t%d\t%d\t%d\t%s\t%s\n",
lo, hi, nranges, n, name, strings.Join(o, ","))

}
}

func rangeTableLo(t *unicode.RangeTable) int {
if len(t.R16) != 0 {
return int(t.R16[0].Lo)
} else if len(t.R32) != 0 {
return int(t.R32[0].Lo)
}
return 0
}

func rangeTableHi(t *unicode.RangeTable) int {
if len(t.R32) != 0 {
return int(t.R32[len(t.R32)-1].Hi)
} else if len(t.R16) != 0 {
return int(t.R16[len(t.R16)-1].Hi)
}
return 0
}

func rangeTableCount(t *unicode.RangeTable) int {
var n int
for _, r := range t.R16 {
n += int(r.Hi-r.Lo)/int(r.Stride) + 1
}
for _, r := range t.R32 {
n += int(r.Hi-r.Lo)/int(r.Stride) + 1
}
return n
}

func overlaps(a, b *unicode.RangeTable) bool {
return rangeTableHi(a) >= rangeTableLo(b) && rangeTableHi(b) >= rangeTableLo(a)
}

var _ = unicode.Tamil

func intersects(a, b *unicode.RangeTable) bool {
if !overlaps(a, b) {
return false
}
for _, ar := range a.R16 {
if ar.Stride != 1 && ar.Stride != (ar.Hi-ar.Lo) {
fmt.Printf("%d:%d ", (ar.Hi - ar.Lo), ar.Stride)
}
for _, br := range b.R16 {
if ar.Hi >= br.Lo && br.Hi >= ar.Lo {
return true
}
}
for _, br := range b.R32 {
if uint32(ar.Hi) >= br.Lo && br.Hi >= uint32(ar.Lo) {
return true
}
}
}
for _, ar := range a.R32 {
if ar.Stride != 1 && ar.Stride != (ar.Hi-ar.Lo) {
fmt.Printf("%d:%d ", (ar.Hi - ar.Lo), ar.Stride)
}
for _, br := range b.R16 {
if ar.Hi >= uint32(br.Lo) && uint32(br.Hi) >= ar.Lo {
return true
}
}
for _, br := range b.R32 {
if ar.Hi >= br.Lo && br.Hi >= ar.Lo {
return true
}
}
}
return false
}

// ByRanges implments sort.Interface.
type ByRanges []string

func (s ByRanges) Len() int {
return len(s)
}

func (s ByRanges) Less(i, j int) bool {
a := unicode.Scripts[s[i]]
b := unicode.Scripts[s[j]]

alo := rangeTableLo(a)
blo := rangeTableLo(b)
if alo < blo {
return true
}
if alo > blo {
return false
}

ac := rangeTableCount(a)
bc := rangeTableCount(b)
if ac < bc {
return true
}
if ac > bc {
return false
}

an := len(a.R16) + len(a.R32)
bn := len(b.R16) + len(b.R32)
if an < bn {
return true
}
if an > bn {
return false
}

return rangeTableHi(a) < rangeTableHi(b)
}

func (s ByRanges) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}