-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
183 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package assocentity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package assocentity | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/ndabAP/assocentity/v15/tokenize" | ||
"github.com/ndabAP/assocentity/v15/tokenize/delimiter" | ||
) | ||
|
||
func TestNewSource(t *testing.T) { | ||
type test struct { | ||
name string | ||
entities []any | ||
texts []any | ||
want source[any] | ||
} | ||
|
||
tests := []test{ | ||
{ | ||
name: "empty", | ||
entities: []any{}, | ||
texts: []any{}, | ||
want: source[any]{Entities: []any{}, Texts: []any{}}, | ||
}, | ||
{ | ||
name: "only text", | ||
entities: []any{}, | ||
texts: []any{"No Payne, No Gain.", "You can't win this one, Max."}, | ||
want: source[any]{Entities: []any{}, Texts: []any{"No Payne, No Gain.", "You can't win this one, Max."}}, | ||
}, | ||
{ | ||
name: "only entities", | ||
entities: []any{"Max Payne", "Max", "Payne"}, | ||
texts: []any{}, | ||
want: source[any]{Entities: []any{"Max Payne", "Max", "Payne"}, Texts: []any{}}, | ||
}, | ||
{ | ||
name: "duplicate entities", | ||
entities: []any{"Max Payne", "Max", "Max", "Payne"}, | ||
texts: []any{}, | ||
want: source[any]{Entities: []any{"Max Payne", "Max", "Payne"}, Texts: []any{}}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := NewSource(tt.entities, tt.texts) | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewSource() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSource_Tokenize(t *testing.T) { | ||
type test struct { | ||
name string | ||
entities []string | ||
texts []string | ||
tokenizer tokenize.Tokenizer[string] | ||
mut []WithMut[string] | ||
want Tokens[string] | ||
wantErr bool | ||
} | ||
|
||
tests := []test{ | ||
{ | ||
name: "empty", | ||
entities: []string{}, | ||
texts: []string{}, | ||
tokenizer: delimiter.New(nil), | ||
want: Tokens[string]{entities: [][]tokenize.Token[string]{}, texts: [][]tokenize.Token[string]{}}, | ||
}, | ||
{ | ||
name: "only entities", | ||
entities: []string{"Max Payne", "Max", "Payne"}, | ||
texts: []string{}, | ||
tokenizer: delimiter.New(func(r rune) bool { | ||
return r == ' ' | ||
}), | ||
want: Tokens[string]{entities: [][]tokenize.Token[string]{ | ||
{ | ||
{Text: "Max"}, | ||
{Text: "Payne"}, | ||
}, | ||
{ | ||
{Text: "Max"}, | ||
}, | ||
{ | ||
{Text: "Payne"}, | ||
}, | ||
}, texts: [][]tokenize.Token[string]{}}, | ||
}, | ||
{ | ||
name: "only text", | ||
entities: []string{}, | ||
texts: []string{"No Payne, No Gain."}, | ||
tokenizer: delimiter.New(func(r rune) bool { | ||
switch r { | ||
case ' ', ',', '.': | ||
return true | ||
default: | ||
return false | ||
} | ||
}), | ||
want: Tokens[string]{entities: [][]tokenize.Token[string]{}, texts: [][]tokenize.Token[string]{ | ||
{ | ||
{Text: "No"}, | ||
{Text: "Payne"}, | ||
{Text: "No"}, | ||
{Text: "Gain"}, | ||
}, | ||
}}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := NewSource(tt.entities, tt.texts) | ||
got, err := s.Tokenize(context.Background(), tt.tokenizer, tt.mut...) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Tokenize() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Tokenize() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package delimiter | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/ndabAP/assocentity/v15/tokenize" | ||
) | ||
|
||
type delim struct { | ||
delim func(rune) bool | ||
} | ||
|
||
func New(f func(rune) bool) delim { | ||
return delim{f} | ||
} | ||
|
||
func (d delim) Tokenize(ctx context.Context, text string) ([]tokenize.Token[string], error) { | ||
tokens := make([]tokenize.Token[string], 0) | ||
|
||
spl := strings.FieldsFunc(text, d.delim) | ||
for _, s := range spl { | ||
tokens = append(tokens, tokenize.Token[string]{ | ||
Text: s, | ||
}) | ||
} | ||
|
||
return tokens, nil | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters