-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlemmatizer.go
More file actions
44 lines (37 loc) · 766 Bytes
/
lemmatizer.go
File metadata and controls
44 lines (37 loc) · 766 Bytes
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
package ling
import (
"fmt"
"github.com/liuzl/ling/lemmatize"
)
// Lemma processor name
const Lemma = "lemma"
func init() {
Processors[Lemma] = &Lemmatizer{}
}
// Lemmatizer is the processor for lemmatization
type Lemmatizer struct {
}
// Process is the function to annotate documents
func (l *Lemmatizer) Process(d *Document) error {
if d == nil || len(d.Text) == 0 {
return nil
}
if len(d.Tokens) == 0 {
return fmt.Errorf("tokenization required")
}
for _, lang := range d.Langs {
if f, has := lemmatize.Funcs[lang]; has {
ret, err := f(d.XTokens(Norm))
if err != nil {
return err
}
if len(ret) != len(d.Tokens) {
continue
}
for i, str := range ret {
d.Tokens[i].Annotations[Lemma] = str
}
}
}
return nil
}