forked from martinlindhe/subtitles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_style.go
55 lines (51 loc) · 1.07 KB
/
filter_style.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
package subtitles
import (
"regexp"
"strconv"
"strings"
)
// filterStyle removes all html tags and ssa codes from captions
func (subtitle *Subtitle) filterStyle() *Subtitle {
re := regexp.MustCompile("(?:<[^<>]+>|{[^{}]+})")
for _, cap := range subtitle.Captions {
for i, line := range cap.Text {
if re.MatchString(line) {
line = re.ReplaceAllString(line, "")
}
if isMeaningless(line) {
cap.Text[i] = ""
continue
}
line = unescapeString(line)
line = strings.TrimSpace(line)
cap.Text[i] = line
}
}
return subtitle
}
func isMeaningless(line string) bool {
arr := strings.Split(line, " ")
if len(arr) > 5 {
isnumberorsinglechar := 0
for _, v := range arr {
if len(v) == 1 {
isnumberorsinglechar++
continue
}
if _, err := strconv.Atoi(v); err == nil {
isnumberorsinglechar++
}
}
if isnumberorsinglechar == len(arr) {
return true
}
}
return false
}
func unescapeString(line string) string {
r, err := strconv.Unquote(`"` + line + `"`)
if err != nil {
return strings.Replace(line, "\\n", "\n", -1)
}
return r
}