-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognizer.go
More file actions
64 lines (50 loc) · 1.29 KB
/
Copy pathrecognizer.go
File metadata and controls
64 lines (50 loc) · 1.29 KB
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
package main
import (
"path/filepath"
prose "gopkg.in/jdkato/prose.v2"
)
type recognizeResult struct {
Entities []entityCount
}
type entityCount struct {
Entity prose.Entity `json:entity`
Count int `json:count`
}
func recognize(text string, modelName string) (recognizeResult, error) {
var result recognizeResult
doc, err := getDocument(text, modelName)
if err != nil {
return result, err
}
if len(doc.Entities()) == 0 {
return result, nil
}
result = recognizeResult{Entities: distinctEntities(doc.Entities())}
return result, nil
}
func getDocument(text string, modelName string) (*prose.Document, error) {
modelPath := filepath.Join(".", "models", modelName)
model := prose.ModelFromDisk(modelPath)
doc, err := prose.NewDocument(text, prose.UsingModel(model))
if err != nil {
return nil, err
}
return doc, nil
}
func distinctEntities(entities []prose.Entity) []entityCount {
counter := map[string]entityCount{}
for _, entity := range entities {
value, found := counter[entity.Text]
if found {
value.Count = value.Count + 1
counter[entity.Text] = value
} else {
counter[entity.Text] = entityCount{Entity: entity, Count: 1}
}
}
distinct := []entityCount{}
for _, value := range counter {
distinct = append(distinct, value)
}
return distinct
}