1package parse
2
3// Rule interface and its methods make possible the polimorf usage of process
4// how Rule retrieve tokens from text.
5type Rule interface {
6 IsWordSeparator(rune rune) bool
7 IsSentenceSeparator(rune rune) bool
8}
9
10// RuleDefault struct implements the Rule interface. It contains the separator
11// characters and can decide a character is separator or not.
12type RuleDefault struct {
13 wordSeparators [21]string
14 sentenceSeparators [3]string
15}
16
17// NewRule constructor retrieves a RuleDefault pointer.
18func NewRule() *RuleDefault {
19 return &RuleDefault{
20 [21]string{" ", ",", "'", "’", "\"", ")", "(", "[", "]", "{", "}", "\"", ";", "\n", ">", "<", "%", "@", "&", "=", "#"},
21 [3]string{"!", ".", "?"},
22 }
23}
24
25// IsWordSeparator method retrieves true when a character is a kind of special
26// character and possibly it separates to words from each other. It also checks
27// for sentence separator by IsSentenceSeparator method.
28func (r *RuleDefault) IsWordSeparator(rune rune) bool {
29 chr := string(rune)
30
31 for _, val := range r.wordSeparators {
32 if chr == val {
33 return true
34 }
35 }
36
37 return r.IsSentenceSeparator(rune)
38}
39
40// IsSentenceSeparator method retrieves true when a character is a kind of
41// special character and possibly it separates to words from each other.
42func (r *RuleDefault) IsSentenceSeparator(rune rune) bool {
43 chr := string(rune)
44
45 for _, val := range r.sentenceSeparators {
46 if chr == val {
47 return true
48 }
49 }
50
51 return false
52}