/
Shuffler.go
116 lines (99 loc) · 3.03 KB
/
Shuffler.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package wordShuffler
import (
"strings"
"fmt"
)
type Shuffler struct {
// location of the file containing the text (could be empty value "")
Location string
// Shuffle Rule
Rule ShuffleRule
// old / original words
oldGrams []string
// new / shuffled words
newGrams []string
// original text
oldText string
// shuffled text
newText string
}
func NewShuffler(location string, shuffleRule... ShuffleRule) Shuffler {
m := new(Shuffler)
m.Location = location
if shuffleRule != nil && len(shuffleRule) > 0 {
m.Rule = shuffleRule[0]
} else {
// default rule is Cambridge Rule
m.Rule = &CambridgeRule{}
}
return *m
}
// return the shuffled / new text
func (s *Shuffler) GetShuffleText() string {
return s.newText
}
// return the shuffled / new grams
func (s *Shuffler) GetShuffleGrams() []string {
return s.newGrams
}
// shuffle the text. If no text is provided, will try to extract the text
// from the file location WHICH might also be empty ""; if that is the case,
// an error would be thrown
func (s *Shuffler) ShuffleText(oldText... string) (string, error) {
if oldText != nil && len(oldText) > 0 {
s.oldText = oldText[0]
} else {
if strings.Compare(s.Location, "") == 0 {
return "", fmt.Errorf("there is NO text for shuffling! Either provide it to the function or through a file (check the Location attribute)")
} else {
content, err := ReadFileContent(s.Location)
if err != nil {
return "", err
}
s.oldText = content
}
} // end -- if (oldText provided?)
// grams breaking for the oldText
grams, err := gramsBreaking(s.oldText)
if err != nil {
return "", err
}
s.oldGrams = grams
s.newGrams = make([]string, 1)
// run shuffle by the rule object
for _, oldGram := range s.oldGrams {
gram, err := s.Rule.Shuffle(oldGram)
if err != nil {
return "", err
}
s.newGrams = append(s.newGrams, gram)
}
// form the newText
s.newText = combineGramsToText(s.newGrams)
return s.newText, nil
}
func gramsBreaking(text string) ([]string, error) {
// grams := strings.Split(text, " ") // no regular expression supported...
grams := make([]string, 1)
currentCharArray := make([]rune, 1)
charArray := []rune(text)
for _, charInRune := range charArray {
switch charInRune {
// 32 = space, 10 = line break
case 32, 10:
grams = append(grams, string(currentCharArray))
// sort of reset
currentCharArray = currentCharArray[:0]
default:
currentCharArray = append(currentCharArray, charInRune)
}
}
// handling for the last word (if any)
if len(currentCharArray) > 0 {
grams = append(grams, string(currentCharArray))
}
return grams, nil
}
func combineGramsToText(grams []string) string {
return strings.TrimSpace(strings.Join(grams, " "))
}