-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrammar.go
36 lines (33 loc) · 1001 Bytes
/
grammar.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
// Package grammar generates grammar corrections.
package grammar
import (
"victorz.ca/grammar/parser"
"victorz.ca/grammar/sequence"
"victorz.ca/grammar/transform"
"victorz.ca/grammar/wording"
)
// Load runs some text through the transformer, lexer, parser, and formatter.
// If nothing is detected, corrections will be nil.
// Reasons for the corrections should be stored in why, but it may be nil.
func Load(s string) (corrections, why []string) {
s = transform.DoAll(s)
seq := sequence.New(s)
why = parser.DoAll(&seq)
if len(why) != 0 {
c := seq.Corrections()
corrections = make([]string, len(c))
for i, correction := range c {
corrections[i] = "“" + correction + "”"
}
}
return
}
// MakeTweetReply generates a response for a tweet.
// If nothing was detected, it returns an empty string.
func MakeTweetReply(tweet, author string) string {
corrections, why := Load(tweet)
if len(corrections) == 0 {
return ""
}
return wording.MakeTweet(corrections, why, author)
}