Skip to content
Open
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: 12 additions & 4 deletions tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,7 @@ type InputSequence struct {
func NewInputSequence(input interface{}) (retVal InputSequence) {
switch reflect.TypeOf(input).Kind().String() {
case "string":
return InputSequence{
input: []string{input.(string)},
inputType: RawInput,
}
return NewRawInputSequence(input.(string))
case "slice":
if reflect.TypeOf(input).Elem().Name() != "string" {
log.Fatalf("Invalid input type: Expected type of 'string' or '[]string', got %v\n", reflect.TypeOf(input).Kind().String())
Expand All @@ -218,6 +215,17 @@ func NewInputSequence(input interface{}) (retVal InputSequence) {
return
}

// NewRawInputSequence creates a new InputSequence from a raw string input.
// Using this method instead of NewInputSequence avoids calling the reflect library
// to inspect the type of the input when the caller knows exactly what the input is.
// It is a performance optimization because inspecting the type is not needed in most cases.
func NewRawInputSequence(input string) InputSequence {
return InputSequence{
input: []string{input},
inputType: RawInput,
}
}

type Single struct {
Sentence InputSequence
}
Expand Down