-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
68 lines (55 loc) · 1.64 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package jargon_test
import (
"fmt"
"log"
"strings"
"github.com/clipperhouse/jargon"
"github.com/clipperhouse/jargon/filters/stackoverflow"
)
func ExampleTokenize() {
// Tokenize takes an io.Reader
text := `Let’s talk about Ruby on Rails and ASPNET MVC.`
r := strings.NewReader(text)
tokens := jargon.Tokenize(r)
// Tokenize returns a Tokens iterator. Iterate by calling Next() until nil, which
// indicates that the iterator is exhausted.
for {
token, err := tokens.Next()
if err != nil {
// Because the source is I/O, errors are possible
log.Fatal(err)
}
if token == nil {
break
}
// Do stuff with token
}
// Tokens is lazily evaluated; it does the tokenization work as you call Next.
// This is done to ensure predictble memory usage and performance. It is
// 'forward-only', which means that once you consume a token, you can't go back.
// Usually, Tokenize serves as input to Lemmatize
}
func ExampleTokenStream_Filter() {
// Lemmatize take tokens and attempts to find their canonical version
// Lemmatize takes a Tokens iterator, and one or more token filters
text := `Let’s talk about Ruby on Rails and ASPNET MVC.`
r := strings.NewReader(text)
tokens := jargon.Tokenize(r)
filtered := tokens.Filter(stackoverflow.Tags)
// Lemmatize returns a Tokens iterator. Iterate by calling Next() until nil, which
// indicates that the iterator is exhausted.
for {
token, err := filtered.Next()
if err != nil {
// Because the source is I/O, errors are possible
log.Fatal(err)
}
if token == nil {
break
}
// Do stuff with token
if token.IsLemma() {
fmt.Printf("found lemma: %s", token)
}
}
}