-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterlines.go
161 lines (143 loc) · 6.02 KB
/
filterlines.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
package main
import (
"bufio"
"flag"
"fmt"
"math"
"os"
"regexp"
"strings"
"github.com/agnivade/levenshtein"
)
func calculateEntropy(s string) float64 {
if len(s) == 0 {
return 0
}
frequency := make(map[rune]float64)
for _, char := range s {
frequency[char]++
}
var entropy float64
for _, count := range frequency {
probability := count / float64(len(s))
entropy -= probability * math.Log2(probability)
}
return entropy
}
func isRandom(s string, entropyThreshold float64) bool {
if len(s) < 5 {
return false
}
randomPattern := regexp.MustCompile(`^[a-z]{5,}$`)
highConsonantRatioPattern := regexp.MustCompile(`^[bcdfghjklmnpqrstvwxyz]{4,}[a-z]*$`)
entropy := calculateEntropy(s)
return entropy >= entropyThreshold || randomPattern.MatchString(s) || highConsonantRatioPattern.MatchString(s)
}
func fuzzyMatch(s, target string, threshold float64) bool {
distance := levenshtein.ComputeDistance(s, target)
similarity := 1 - float64(distance)/float64(max(len(s), len(target)))
return similarity >= threshold
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
// Command-line arguments
listFile := flag.String("l", "", "File containing input lines")
charCount := flag.Int("c", -1, "Exclude lines with char count greater than <int>")
randomThreshold := flag.Float64("r", 3.5, "Exclude lines that are completely random")
matchRegex := flag.String("mr", "", "Exclude lines matching the regex")
matchString := flag.String("ms", "", "Exclude lines matching the string (case sensitive)")
fuzzyString := flag.String("mf", "", "Exclude lines with fuzzy match using Levenshtein similarity")
similarityThreshold := flag.Float64("t", 0.8, "Fuzzy match similarity threshold")
showHelp := flag.Bool("h", false, "Show help")
flag.Parse()
// Show help if requested
if *showHelp {
fmt.Println("Usage:")
fmt.Println("-l <file> Input file (optional, otherwise read from stdin)")
fmt.Println("-c <int> Exclude lines with char count greater than <int>")
fmt.Println("-r <float> Exclude random lines with entropy threshold <float>")
fmt.Println("-mr <regex> Exclude lines matching the regex")
fmt.Println("-ms <string> Exclude lines matching the string (case sensitive)")
fmt.Println("-mf <string> Exclude lines with fuzzy match using Levenshtein similarity")
fmt.Println("-t <float> Fuzzy match similarity threshold (default 0.8)")
fmt.Println("-h Show this help")
return
}
// Check if no arguments are provided
noArgsProvided := *listFile == "" && *charCount == -1 && *matchRegex == "" && *matchString == "" && *fuzzyString == ""
// Read input lines
var lines []string
if *listFile != "" {
file, err := os.Open(*listFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening file: %v\n", err)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading file: %v\n", err)
os.Exit(1)
}
} else {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading stdin: %v\n", err)
os.Exit(1)
}
}
// Process lines
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Apply default rules if no arguments are provided
if noArgsProvided {
if len(line) > 10 || isRandom(line, 3.5) {
continue
}
} else {
// Exclude by char count
if *charCount > 0 && len(line) > *charCount {
continue
}
// Exclude random lines
if isRandom(line, *randomThreshold) {
continue
}
// Exclude by regex match
if *matchRegex != "" {
regex, err := regexp.Compile(*matchRegex)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid regex: %v\n", err)
os.Exit(1)
}
if regex.MatchString(line) {
continue
}
}
// Exclude by string match
if *matchString != "" && line == *matchString {
continue
}
// Exclude by fuzzy match
if *fuzzyString != "" && fuzzyMatch(line, *fuzzyString, *similarityThreshold) {
continue
}
}
// Print the line if it passes all checks
fmt.Println(line)
}
}