/
excerpt.go
171 lines (140 loc) · 4 KB
/
excerpt.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package stringfy
import (
"errors"
"fmt"
"regexp"
"strings"
)
const (
defaultExcerptRadius = 100
)
// NewExcerpt creates a new instace of the Excerpter struct with its defaults.
func NewExcerpt() *Excerpter {
return &Excerpter{
radius: defaultExcerptRadius,
omission: "...",
}
}
// Excerpter struct
type Excerpter struct {
radius int
separator string
omission string
}
// Options sets RadiusOption, OmissionOption and SeparatorOption
func (ex *Excerpter) Options(options ...interface{}) {
for _, opt := range options {
switch opt.(type) {
case RadiusOption:
opt.(RadiusOption)(ex)
case OmissionOption:
opt.(OmissionOption)(ex)
case SeparatorOption:
opt.(SeparatorOption)(ex)
}
}
}
// Perform excerpt of a given text. Receices a text and a phrase.
func (ex *Excerpter) Perform(text, phrase string) (string, error) {
if strings.ContainsAny(phrase, " ") {
return "", errors.New("when composing the excerpt, your phrase should not contain more than one word")
}
// Checks if the separator is an empty space, which means that it will compose
// the excerpt based on words.
if ex.separator == " " {
radText, err := ex.emptySpaceSeparator(text, phrase)
if err != nil {
return "", err
}
return radText, nil
}
// The separator is different than empty space.
radText, err := ex.notEmptySpaceSeparator(text, phrase)
if err != nil {
return "", err
}
return radText, nil
}
// Set fields
func (ex *Excerpter) setRadius(r int) {
ex.radius = r
}
func (ex *Excerpter) setOmission(om string) {
ex.omission = om
}
func (ex *Excerpter) setSeparator(sep string) {
ex.separator = sep
}
func (ex *Excerpter) emptySpaceSeparator(text, phrase string) (string, error) {
// Removes all the returns from the text, splits it by by the separator and
// retrieves the number of words in the text.
rawText := strings.Replace(text, "\n", "", -1)
sText := strings.Split(rawText, ex.separator)
sTextLen := len(sText)
// Checks if the text contains the phrase and setup the range of words based
// on the radius.
rangeSlice := make([]int, 0)
for i, word := range sText {
if word != phrase {
continue
}
leftRange := i - ex.radius
if leftRange < 0 {
leftRange = 0
}
rightRange := (i + ex.radius) + 1
if rightRange >= sTextLen {
rightRange = sTextLen
}
rangeSlice = append(rangeSlice, leftRange, rightRange)
break
}
// Tests if phrase was found in the text.
if len(rangeSlice) > 0 {
radText := strings.Join(sText[rangeSlice[0]:rangeSlice[1]], " ")
return ex.addOmissionToText(
radText,
sTextLen,
[]int{rangeSlice[0], rangeSlice[1]},
), nil
}
return "", fmt.Errorf("phrase (%s) not found", phrase)
}
func (ex *Excerpter) notEmptySpaceSeparator(text, phrase string) (string, error) {
tl := len(text)
ol := len(ex.omission)
// Looks for the first occurrence of 'phrase' in the text and return its
// index boundaries
reg := regexp.MustCompile(phrase)
pIndex := reg.FindAllStringIndex(text, 1)
if len(pIndex) == 0 {
return "", fmt.Errorf("phrase (%s) not found", phrase)
}
// Calculates the left radios index. If its less of equal to the length of
// the omission, the left radios index will equal to 0.
leftRad := pIndex[0][0] - ex.radius
if leftRad <= ol {
leftRad = 0
}
// Calculates the right radios index. If its greater than the subtraction of
// the text length by the omission length, the radius index will equal to 0.
rightRad := pIndex[0][1] + ex.radius
if rightRad > (tl - ol) {
rightRad = tl
}
// Extracts the excerpt based on the left and the right radius and then, adds
// the omission symbol.
radText := text[leftRad:rightRad]
return ex.addOmissionToText(radText, tl, []int{leftRad, rightRad}), nil
}
// Test text boundaries in order to add the omission
func (ex *Excerpter) addOmissionToText(text string, max int, textBnd []int) string {
subText := text
if textBnd[0] > 0 {
subText = fmt.Sprintf("%s%s", ex.omission, subText)
}
if textBnd[1] != max {
subText = fmt.Sprintf("%s%s", subText, ex.omission)
}
return subText
}