Documentation ¶
Overview ¶
Package segment is a library for performing Unicode Text Segmentation as described in Unicode Standard Annex #29 http://www.unicode.org/reports/tr29/
Currently only segmentation at Word Boundaries is supported.
The functionality is exposed in two ways:
1. You can use a bufio.Scanner with the SplitWords implementation of SplitFunc. The SplitWords function will identify the appropriate word boundaries in the input text and the Scanner will return tokens at the appropriate place.
scanner := bufio.NewScanner(...) scanner.Split(segment.SplitWords) for scanner.Scan() { tokenBytes := scanner.Bytes() } if err := scanner.Err(); err != nil { t.Fatal(err) }
2. Sometimes you would also like information returned about the type of token. To do this we have introduce a new type named Segmenter. It works just like Scanner but additionally a token type is returned.
segmenter := segment.NewWordSegmenter(...) for segmenter.Segment() { tokenBytes := segmenter.Bytes()) tokenType := segmenter.Type() } if err := segmenter.Err(); err != nil { t.Fatal(err) }
Index ¶
Constants ¶
const ( None = iota Number Letter Kana Ideo )
Word Types
const ( // Maximum size used to buffer a token. The actual maximum token size // may be smaller as the buffer may need to include, for instance, a newline. MaxScanTokenSize = 64 * 1024 )
Variables ¶
var ( ErrTooLong = errors.New("bufio.Segmenter: token too long") ErrNegativeAdvance = errors.New("bufio.Segmenter: SplitFunc returns negative advance count") ErrAdvanceTooFar = errors.New("bufio.Segmenter: SplitFunc returns advance count beyond input") )
Errors returned by Segmenter.
var ParseError = fmt.Errorf("unicode word segmentation parse error")
var RagelFlags = "-T1"
Functions ¶
func SegmentWordsDirect ¶
Types ¶
type SegmentFunc ¶
type SegmentFunc func(data []byte, atEOF bool) (advance int, token []byte, segmentType int, err error)
SegmentFunc is the signature of the segmenting function used to tokenize the input. The arguments are an initial substring of the remaining unprocessed data and a flag, atEOF, that reports whether the Reader has no more data to give. The return values are the number of bytes to advance the input and the next token to return to the user, plus an error, if any. If the data does not yet hold a complete token, for instance if it has no newline while scanning lines, SegmentFunc can return (0, nil, nil) to signal the Segmenter to read more data into the slice and try again with a longer slice starting at the same point in the input.
If the returned error is non-nil, segmenting stops and the error is returned to the client.
The function is never called with an empty data slice unless atEOF is true. If atEOF is true, however, data may be non-empty and, as always, holds unprocessed text.
type Segmenter ¶
type Segmenter struct {
// contains filtered or unexported fields
}
Segmenter provides a convenient interface for reading data such as a file of newline-delimited lines of text. Successive calls to the Segment method will step through the 'tokens' of a file, skipping the bytes between the tokens. The specification of a token is defined by a split function of type SplitFunc; the default split function breaks the input into lines with line termination stripped. Split functions are defined in this package for scanning a file into lines, bytes, UTF-8-encoded runes, and space-delimited words. The client may instead provide a custom split function.
Segmenting stops unrecoverably at EOF, the first I/O error, or a token too large to fit in the buffer. When a scan stops, the reader may have advanced arbitrarily far past the last token. Programs that need more control over error handling or large tokens, or must run sequential scans on a reader, should use bufio.Reader instead.
func NewSegmenter ¶
NewSegmenter returns a new Segmenter to read from r. Defaults to segment using SegmentWords
func NewSegmenterDirect ¶
NewSegmenterDirect returns a new Segmenter to work directly with buf. Defaults to segment using SegmentWords
func NewWordSegmenter ¶
NewWordSegmenter returns a new Segmenter to read from r.
func NewWordSegmenterDirect ¶
NewWordSegmenterDirect returns a new Segmenter to work directly with buf.
func (*Segmenter) Bytes ¶
Bytes returns the most recent token generated by a call to Segment. The underlying array may point to data that will be overwritten by a subsequent call to Segment. It does no allocation.
func (*Segmenter) Segment ¶
Segment advances the Segmenter to the next token, which will then be available through the Bytes or Text method. It returns false when the scan stops, either by reaching the end of the input or an error. After Segment returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil.
func (*Segmenter) SetSegmenter ¶
func (s *Segmenter) SetSegmenter(segmenter SegmentFunc)
SetSegmenter sets the segment function for the Segmenter. If called, it must be called before Segment.