Skip to content

Commit

Permalink
TokenCount function, for counting tokens
Browse files Browse the repository at this point in the history
This is like AllTokens except it returns just the number of tokens found,
rather than the tokens themselves.
  • Loading branch information
apparentlymart committed May 27, 2017
1 parent 33a8a4c commit 40e9043
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions textseg/all_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ func AllTokens(buf []byte, splitFunc bufio.SplitFunc) ([][]byte, error) {
}
return ret, scanner.Err()
}

// TokenCount is a utility that uses a bufio.SplitFunc to count the number of
// recognized tokens in the given buffer.
func TokenCount(buf []byte, splitFunc bufio.SplitFunc) (int, error) {
scanner := bufio.NewScanner(bytes.NewReader(buf))
scanner.Split(splitFunc)
var ret int
for scanner.Scan() {
ret++
}
return ret, scanner.Err()
}

0 comments on commit 40e9043

Please sign in to comment.