Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ndabAP committed Jan 24, 2025
1 parent 8b96295 commit 7393adc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 39 deletions.
17 changes: 2 additions & 15 deletions tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ type (
entities []tokenize.Tokens // [["Max", "Payne"], ["Max"], ["Payne"]]
texts []tokenize.Tokens // [["Relax", ",", "Max", "."]]
}
)

func (tokens Tokens) Entities(
iterator func(entities []*tokenize.Token) iter.Seq2[int, *tokenize.Token],
) iter.Seq[*tokenize.Token] {
f := func(yield func(*tokenize.Token) bool) {
for _, entity := range tokens.entities {
for _, token := range iterator(entity) {
if !yield(token) {
return
}
}
}
}
return f
}
iterfunc func(entities []*tokenize.Token) iter.Seq2[int, *tokenize.Token]
)
88 changes: 64 additions & 24 deletions tokens_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,52 @@ import (
"github.com/ndabAP/assocentity/v15/tokenize"
)

func (tokens Tokens) Vecs() {
cmp := func(
text []*tokenize.Token,
iterator func(entities []*tokenize.Token) iter.Seq2[int, *tokenize.Token],
) int {
next, stop := iter.Pull(tokens.Entities(iterator))
func (tokens Tokens) Locs() {
cmp := func(text []*tokenize.Token, iterator iterfunc) int {
var i int = 0
next, stop := iter.Pull(slices.Values(tokens.entities))
defer stop()
var (
i int = 0
found bool = true
)
for {
token, ok := next()
found := true
tok, ok := next()
if !ok {
break
}

if text[i].Text != token.Text {
ok = false
i = 0
continue
}
// Entity iterator
n, s := iter.Pull2(iterator(tok))
// Text iterator
m, t := iter.Pull2(iterator(text))
for {
_, u, ok := m()
if !ok {
s()
t()
break
}
j, v, ok := n()
if !ok {
s()
t()
break
}

i++
}
// If text and entity iterator values don't match, continue
// with next entity
if u.Text != v.Text {
i = 0
found = false
s()
t()
break
}

if found {
return i
i = j
}

if found {
return i
}
}

return -1
Expand All @@ -52,18 +70,32 @@ func (tokens Tokens) Vecs() {

if j := cmp(text[i:], slices.All); j > -1 {
// Skip entity
i += j
switch j {
case -1:
case 0:
i += 1
default:
i += j
}

continue
}

token := text[i]
// For each text token, starting from text token
// Search for entity, starting from text token
for k := range slices.All(text[i:]) {
if l := cmp(text[i:], slices.All); l > -1 {
// Found entity
vecs[token] = append(vecs[token], i+k)

i += l
switch l {
case -1:
case 0:
i += 1
default:
i += l
}

break
}
}
Expand All @@ -80,7 +112,15 @@ func (tokens Tokens) Vecs() {

if j := cmp(text[:l], slices.Backward); j > -1 {
// Found entity, skip
l -= j
// Skip entity
switch j {
case -1:
case 0:
i -= 1
break
default:
i -= j
}
continue
}

Expand Down

0 comments on commit 7393adc

Please sign in to comment.