Initial commit
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ecfe0bc
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+sandbox
\ No newline at end of file
diff --git a/datokenizer.go b/datokenizer.go
new file mode 100644
index 0000000..8a68753
--- /dev/null
+++ b/datokenizer.go
@@ -0,0 +1,521 @@
+package datokenizer
+
+/**
+ * The file reader is basically a port of foma2js,
+ * licensed under the Apache License, version 2,
+ * and written by Mans Hulden.
+ */
+
+import (
+ "bufio"
+ "compress/gzip"
+ "fmt"
+ "io"
+ "os"
+ "strconv"
+ "strings"
+ "unicode/utf8"
+)
+
+const (
+ PROPS = 1
+ SIGMA = 2
+ STATES = 3
+ NONE = 4
+)
+
+// Special symbols in sigma
+var EPSILON, UNKNOWN, IDENTITY, FINAL int
+
+type mapping struct {
+ source int
+ target int
+}
+
+type edge struct {
+ in int
+ out int
+ target int
+}
+
+type Tokenizer struct {
+ sigma map[rune]int
+ sigma_rev map[int]rune
+ arccount int
+ statecount int
+ sigmacount int
+ maxsize int
+ array []int
+ transitions []map[int]*edge
+}
+
+func parse_file(file string) *Tokenizer {
+ f, err := os.Open(file)
+ if err != nil {
+ panic(err)
+ }
+ defer f.Close()
+
+ gz, err := gzip.NewReader(f)
+ if err != nil {
+ panic(err)
+ }
+ defer gz.Close()
+
+ return parse(gz)
+}
+
+func parse(ior io.Reader) *Tokenizer {
+ r := bufio.NewReader(ior)
+
+ tok := &Tokenizer{
+ sigma: make(map[rune]int),
+ sigma_rev: make(map[int]rune),
+ }
+
+ final := false
+
+ var arrstate, arrin, arrout, arrtarget, arrfinal int
+
+ mode := 0
+ var elem []string
+ var elemint [5]int
+
+ for {
+ line, err := r.ReadString('\n')
+ if err != nil {
+ if err == io.EOF {
+ break
+ }
+ panic(err)
+ }
+ if strings.HasPrefix(line, "##foma-net") {
+ continue
+ }
+ if strings.HasPrefix(line, "##props##") {
+ mode = PROPS
+ continue
+ }
+ if strings.HasPrefix(line, "##states##") {
+ mode = STATES
+
+ // Adds a final transition symbol to sigma
+ // written as '#' in Mizobuchi et al (2000)
+ tok.sigmacount++
+ FINAL = tok.sigmacount
+ continue
+ }
+ if strings.HasPrefix(line, "##sigma##") {
+ mode = SIGMA
+ continue
+ }
+ if strings.HasPrefix(line, "##end##") {
+ mode = NONE
+ continue
+ }
+
+ switch mode {
+ case PROPS:
+ {
+ elem = strings.Split(line, " ")
+ /*
+ fmt.Println("arity: " + elem[0])
+ fmt.Println("arccount: " + elem[1])
+ fmt.Println("statecount: " + elem[2])
+ fmt.Println("linecount: " + elem[3])
+ fmt.Println("finalcount: " + elem[4])
+ fmt.Println("pathcount: " + elem[5])
+ fmt.Println("is_deterministic: " + elem[6])
+ fmt.Println("is_pruned: " + elem[7])
+ fmt.Println("is_minimized: " + elem[8])
+ fmt.Println("is_epsilon_free: " + elem[9])
+ fmt.Println("is_loop_free: " + elem[10])
+ fmt.Println("extras: " + elem[11])
+ fmt.Println("name: " + elem[12])
+ */
+ if elem[6] != "1" {
+ panic("The FST needs to be deterministic")
+ }
+ if elem[9] != "1" {
+ panic("The FST needs to be epsilon free")
+ }
+
+ elemint[0], err = strconv.Atoi(elem[1])
+ if err != nil {
+ panic("Can't read arccount")
+ }
+ tok.arccount = elemint[0]
+
+ // States start at 1 in Mizobuchi et al (2000),
+ // as the state 0 is associated with a fail.
+ // Initialize states and transitions
+ elemint[0], err = strconv.Atoi(elem[2])
+ if err != nil {
+ panic("Can't read statecount")
+ }
+ tok.statecount = elemint[0]
+ tok.transitions = make([]map[int]*edge, elemint[0]+1)
+ continue
+ }
+ case STATES:
+ {
+ elem = strings.Split(line[0:len(line)-1], " ")
+ if elem[0] == "-1" {
+ continue
+ }
+ elemint[0], err = strconv.Atoi(elem[0])
+
+ if len(elem) > 1 {
+ elemint[1], err = strconv.Atoi(elem[1])
+ if err != nil {
+ break
+ }
+ if len(elem) > 2 {
+ elemint[2], err = strconv.Atoi(elem[2])
+ if err != nil {
+ break
+ }
+ if len(elem) > 3 {
+ elemint[3], err = strconv.Atoi(elem[3])
+ if err != nil {
+ break
+ }
+ if len(elem) > 4 {
+ elemint[4], err = strconv.Atoi(elem[4])
+ if err != nil {
+ break
+ }
+ }
+ }
+ }
+ }
+
+ switch len(elem) {
+ case 5:
+ {
+ arrstate = elemint[0]
+ arrin = elemint[1]
+ arrout = elemint[2]
+ arrtarget = elemint[3]
+ arrfinal = elemint[4]
+ }
+ case 4:
+ {
+ if elemint[1] == -1 {
+ arrstate = elemint[0]
+ arrfinal = elemint[3]
+ } else {
+ arrstate = elemint[0]
+ arrin = elemint[1]
+ arrtarget = elemint[2]
+ arrfinal = elemint[3]
+ arrout = arrin
+ }
+ }
+ case 3:
+ {
+ arrin = elemint[0]
+ arrout = elemint[1]
+ arrtarget = elemint[2]
+ }
+ case 2:
+ {
+ arrin = elemint[0]
+ arrtarget = elemint[2]
+ arrout = arrin
+ }
+ }
+
+ // This collects all edges until arrstate changes
+ if arrfinal == 1 {
+ final = true
+ } else {
+ final = false
+ }
+
+ // While the states in foma start with 0, the states in the
+ // Mizobuchi FSA start with one - so we increase every state by 1.
+
+ if arrin != arrout && arrin != EPSILON && tok.sigma_rev[arrin] != '\n' {
+ panic("Problem: " + strconv.Itoa(arrstate) + " -> " + strconv.Itoa(arrtarget) + " (" + strconv.Itoa(arrin) + ":" + strconv.Itoa(arrout) + ") ")
+ }
+
+ // TODO:
+ // if arrin == EPSILON && arrout == TOKENEND, mark state as newline
+ // if the next transition is the same, remove TOKENEND and add SENTENCEEND
+ // This requires to remove the transition alltogether and marks the state instead.
+
+ // TODO:
+ // if arrout == EPSILON, mark the transition as NOTOKEN
+
+ targetObj := &edge{
+ in: arrin,
+ out: arrout,
+ target: arrtarget + 1,
+ }
+
+ // Initialize outgoing state
+ if tok.transitions[arrstate+1] == nil {
+ tok.transitions[arrstate+1] = make(map[int]*edge)
+ }
+
+ tok.transitions[arrstate+1][arrin] = targetObj
+
+ if final {
+ tok.transitions[arrstate+1][FINAL] = &edge{}
+ }
+
+ continue
+ }
+ case SIGMA:
+ {
+ elem = strings.SplitN(line[0:len(line)-1], " ", 2)
+
+ // Turn string into sigma id
+ number, err := strconv.Atoi(elem[0])
+
+ if err != nil {
+ panic(err)
+ }
+
+ tok.sigmacount = number
+
+ var symbol rune
+
+ // Read rune
+ if utf8.RuneCountInString(elem[1]) == 1 {
+ symbol = []rune(elem[1])[0]
+
+ // Probably a MCS
+ } else if utf8.RuneCountInString(elem[1]) > 1 {
+ switch elem[1] {
+ case "@_EPSILON_SYMBOL_@":
+ {
+ EPSILON = number
+ continue
+ }
+ case "@_UNKNOWN_SYMBOL_@":
+ {
+ UNKNOWN = number
+ continue
+ }
+
+ case "@_IDENTITY_SYMBOL_@":
+ {
+ IDENTITY = number
+ continue
+ }
+ default:
+ panic("MCS not supported: " + line)
+ }
+
+ // Probably a new line symbol
+ } else {
+ line, err = r.ReadString('\n')
+ if err != nil {
+ panic(err)
+ }
+ if len(line) != 1 {
+ panic("MCS not supported:" + line)
+ }
+ symbol = rune('\n')
+ }
+
+ tok.sigma[symbol] = number
+ tok.sigma_rev[number] = symbol
+ }
+ }
+ }
+
+ return tok
+}
+
+// Implementation of Mizobuchi et al (2000), p.128
+func (tok *Tokenizer) buildDA() *Tokenizer {
+
+ mark := 0
+ size := 0
+
+ // Create a mapping from s to t
+ table := make([]*mapping, tok.arccount+1)
+
+ table[size] = &mapping{source: 1, target: 1}
+ size++
+
+ A := make([]int, 0, 256)
+
+ for mark < size {
+ s := table[mark].source // This is a state in Ms
+ t := table[mark].target // This is a state in Mt
+ mark++
+ // fmt.Println("Increase mark", mark)
+ // St := append(St, t)
+ A = A[:0]
+ tok.get_set(s, &A)
+
+ // fmt.Println("Outgoing arcs from t", t, A)
+
+ // tok.array[t].base = tok.x_check(A)
+ tok.set_base(t, tok.x_check(A))
+
+ for _, a := range A {
+
+ if a != FINAL {
+ s1 := tok.transitions[s][a].target // g(s, a)
+
+ // fmt.Println("Found", s, "to", s1, "via", a)
+
+ t1 := tok.get_base(t) + a
+ tok.set_check(t1, t)
+
+ r := in_table(s1, table, size)
+ if r == 0 {
+ // fmt.Println("Increase size", t1)
+ table[size] = &mapping{source: s1, target: t1}
+ size++
+ } else {
+ //fmt.Println("Rep is there", t1, r)
+ tok.set_base(t1, -1*r)
+ // tok.array[t1].base = -1 * r
+ }
+ } else {
+ fmt.Println("I set a final")
+ // t1 := tok.array[t].base + FINAL
+ t1 := tok.get_base(t) + FINAL
+ // tok.array[t1].check = t
+ tok.set_check(t1, t)
+ }
+ }
+ }
+
+ // Following Mizobuchi et al (2000) the size of the
+ // FSA should be stored in check(1).
+ tok.set_check(1, tok.maxsize+1)
+ tok.array = tok.array[:tok.maxsize+1]
+ return tok
+}
+
+func (tok *Tokenizer) resize(l int) {
+ if len(tok.array) < l {
+ tok.array = append(tok.array, make([]int, l)...)
+ }
+}
+
+func (tok *Tokenizer) set_base(p int, v int) {
+ l := p*2 + 1
+ tok.resize(l)
+ if tok.maxsize < l {
+ tok.maxsize = l
+ }
+ tok.array[p*2] = v
+}
+
+func (tok *Tokenizer) get_base(p int) int {
+ if p*2 >= len(tok.array) {
+ return 0
+ }
+ return tok.array[p*2]
+}
+
+func (tok *Tokenizer) set_check(p int, v int) {
+ l := p*2 + 1
+ tok.resize(l)
+ if tok.maxsize < l {
+ tok.maxsize = l
+ }
+ tok.array[(p*2)+1] = v
+}
+
+func (tok *Tokenizer) get_check(p int) int {
+ if (p*2)+1 >= len(tok.array) {
+ return 0
+ }
+ return tok.array[(p*2)+1]
+}
+
+// Check the table if a mapping of s
+// exists and return this as a representative
+func in_table(s int, table []*mapping, size int) int {
+ for x := 0; x < size; x++ {
+ if table[x].source == s {
+ return table[x].target
+ }
+ }
+ return 0
+}
+
+// Set alphabet A to the list of all symbols
+// outgoing from s
+func (tok *Tokenizer) get_set(s int, A *[]int) {
+ for a, _ := range tok.transitions[s] {
+ *A = append(*A, a)
+ }
+}
+
+// Based on Mizobuchi et al (2000), p. 124
+// This iterates for every state through the complete double array
+// structure until it finds a gap that fits all outgoing transitions
+// of the state. This is extremely slow, but is only necessary in the
+// construction phase of the tokenizer.
+func (tok *Tokenizer) x_check(symbols []int) int {
+ // see https://github.com/bramstein/datrie/blob/master/lib/trie.js
+ base := 1
+
+ // fmt.Println("Resize", len(tok.linarray), "<", ((base + FINAL + 1) * 2))
+
+OVERLAP:
+ tok.resize((base + FINAL) * 2)
+ for _, a := range symbols {
+ // if tok.array[base+a].check != 0 {
+ if tok.get_check(base+a) != 0 {
+ base++
+ goto OVERLAP
+ }
+ }
+ fmt.Println("Found a nice place at", base, "for", len(symbols))
+ return base
+}
+
+// Based on Mizobuchi et al (2000), p. 129
+func (tok *Tokenizer) match(input string) bool {
+ t := 1 // Start position
+ chars := []rune(input)
+ i := 0
+ for ; i < len(chars); i++ {
+ a := tok.sigma[chars[i]]
+ tu := t
+ t = tok.get_base(tu) + a
+ fmt.Println("Check", a, t, tok.get_check(1))
+ if t > tok.get_check(1) {
+ break
+ } else if tok.get_check(t) != tu {
+ break
+ } else if tok.get_base(t) < 0 {
+ t = -1 * tok.get_base(t)
+ // fmt.Println("Match is representative!")
+ } else {
+ // fmt.Println("Match is fine!")
+ }
+ }
+
+ if i == len(chars) {
+ fmt.Println("At the end")
+ } else {
+ return false
+ }
+
+ // fmt.Println("Hmm...", tok.get_check(tok.get_base(t)+FINAL), "-", t)
+
+ if tok.get_check(tok.get_base(t)+FINAL) == t {
+ fmt.Println("FINE")
+ return true
+ }
+ return false
+}
+
+// In the final realization, the states can only have 30 bits:
+// base[1] -> is final
+// base[2] -> is_separate
+// check[1] -> translates to epsilon
+// check[2] -> appends newine (Maybe)
+// If check[1] && check[2] is set, this translates to a sentence split (Maybe)
diff --git a/datokenizer_test.go b/datokenizer_test.go
new file mode 100644
index 0000000..8945f91
--- /dev/null
+++ b/datokenizer_test.go
@@ -0,0 +1,40 @@
+package datokenizer
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestSimpleString(t *testing.T) {
+ assert := assert.New(t)
+
+ // bau | bauamt
+ r := strings.NewReader(`##foma-net 1.0##
+##props##
+1 6 7 8 2 2 1 1 1 1 1 2 5B57D486
+##sigma##
+0 @_EPSILON_SYMBOL_@
+3 a
+4 b
+5 m
+6 t
+7 u
+##states##
+0 4 1 0
+1 3 2 0
+2 7 3 0
+3 3 4 1
+4 5 5 0
+5 6 6 0
+6 -1 -1 1
+-1 -1 -1 -1 -1
+##end##`)
+
+ tok := parse(r) // ("tokenizer.fst")
+ tok.buildDA()
+ assert.True(tok.match("bau"))
+ assert.True(tok.match("bauamt"))
+ assert.False(tok.match("baum"))
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..53dc7d0
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module github.com/KorAP/datokenizer
+
+go 1.16
+
+require github.com/stretchr/testify v1.7.0
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..acb88a4
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,11 @@
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=