-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcommon-substr.go
174 lines (158 loc) · 5.33 KB
/
common-substr.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
172
173
174
package main
import (
"fmt"
"os"
"bufio"
"sort"
"flag"
"strings"
)
// Allow the substring map to be sorted
// Source: https://groups.google.com/d/msg/golang-nuts/FT7cjmcL7gw/Gj4_aEsE_IsJ
func rankByWordCount(wordFrequencies map[string]int) PairList{
pl := make(PairList, len(wordFrequencies))
i := 0
for k, v := range wordFrequencies {
pl[i] = Pair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
type Pair struct {
Key string
Value int
}
type PairList []Pair
// Required for Sort to be able to work
func (p PairList) Len() int { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p PairList) Swap(i, j int){ p[i], p[j] = p[j], p[i] }
func shortusage() {
fmt.Println("Common Substring Generator by @singe")
fmt.Println("Usage: $0 [-hinsp] [-t <n>] [-l <n>] [-L <n>] -f <filename>")
os.Exit(1)
}
func longusage() {
fmt.Println("Common Substring Generator by @singe")
fmt.Println("Usage: "+os.Args[0]+" [-hinsp] [-t <n>] [-l <n>] [-L <n>] -f <filename>")
fmt.Println(" -h|--help This help")
fmt.Println(" -i|--insensitive Ignore case of substrings")
fmt.Println(" -L|--maxlength <n> Maximum length substring to look for.")
fmt.Println(" -l|--minlength <n> Minimum length substring to look for.")
fmt.Println(" -n|--nostats Just print the substrings, no stats. Default is to include them.")
fmt.Println(" -t|--threshold <n> Only print substrings more prevalent than <n> percent.")
fmt.Println(" -f|--file <filename> The file to extract substrings from")
fmt.Println(" -s|--suffix Only look at suffix substrings at the end of a string")
fmt.Println(" -p|--prefix Only look at prefix substrings at the beginning of a string")
fmt.Println("Default output (with stats) is tab separated: <percentage> <count> <substring>")
fmt.Println("Sorted from most to least common")
os.Exit(1)
}
func main() {
var nocase bool = false
var nostats bool = false
var strmax int = 32
var strmin int = 2
var threshold int = 0
var focus int = 0
var filenamePtr *string
var suffixPtr *bool
var prefixPtr *bool
if ( len(os.Args) == 1 ) {
shortusage()
}
helpPtr := flag.Bool("help", false, "Longer help.")
flag.BoolVar(&nocase, "insensitive", false, "Ignore case of substrings")
flag.BoolVar(&nocase, "i", false, "Ignore case of substrings")
flag.IntVar(&strmax, "maxlength", 32, "Maximum length substring to look for.")
flag.IntVar(&strmax, "L", 32, "Maximum length substring to look for.")
flag.IntVar(&strmin, "minlength", 2, "Minimum length substring to look for.")
flag.IntVar(&strmin, "l", 2, "Minimum length substring to look for.")
flag.BoolVar(&nostats, "nostats", false, "Just print the substrings, no stats. Default is to include them.")
flag.BoolVar(&nostats, "n", false, "Just print the substrings, no stats. Default is to include them.")
flag.IntVar(&threshold, "threshold", 0, "Only print substrings more prevalent than <int> percent.")
flag.IntVar(&threshold, "t", 0, "Only print substrings more prevalent than <int> percent.")
filenamePtr = flag.String("file", "", "The file to extract substrings from.")
filenamePtr = flag.String("f", "", "The file to extract substrings from.")
suffixPtr = flag.Bool("suffix", false, "Only look at suffix substrings at the end of a string.")
suffixPtr = flag.Bool("s", false, "Only look at suffix substrings at the end of a string.")
prefixPtr = flag.Bool("prefix", false, "Only look at prefix substrings at the beginning of a string.")
prefixPtr = flag.Bool("p", false, "Only look at prefix substrings at the beginning of a string.")
flag.Parse()
if ( *helpPtr ) {
longusage()
}
if _, err := os.Stat(*filenamePtr); err != nil {
fmt.Printf("Unable to open file \"%s\".\n",*filenamePtr)
}
if ( *suffixPtr ) {
focus = 1
}
if ( *prefixPtr ) {
focus = 2
}
var i int = 0
var j int = 0
var count int = 0
var maxlen int = 0
var subs = map[string]int{}
f, _ := os.Open(*filenamePtr)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
var str string
if (nocase) {
str = strings.ToLower(scanner.Text())
} else {
str = scanner.Text()
}
if ( len(str)-1 < strmax ) {
maxlen = len(str)
} else {
maxlen = strmax
}
switch focus {
case 0: //all substrings
for i = 0; i < len(str); i++ {
for j = i; j < maxlen; j++ {
if ( (i-j)-1 <= -strmin ) {
subs[str[i:j+1]]++
}
} //j
} //i
case 1: //suffix
j = maxlen-1
for i = 0; i < len(str); i++ {
if ( (i-j)-1 <= -strmin ) {
subs[str[i:j+1]]++
}
} //i
case 2: //prefix
i = 0
for j = i; j < maxlen; j++ {
if ( (i-j)-1 <= -strmin ) {
subs[str[i:j+1]]++
}
} //j
} //switch
count++ //needed for % calc
}
f.Close()
//sort
var wubs = rankByWordCount(subs)
//print output
for _, p := range wubs {
if (p.Value > 1) {
percentage := ( float32(p.Value) / float32(count) ) * 100
//we might want to ignore some uncommon strings
if (percentage >= float32(threshold)) {
//stats can be noisy
if (nostats) {
fmt.Printf( "%s\n", p.Key)
} else {
fmt.Printf( "%f\t%d\t%s\n", percentage, p.Value, p.Key)
}
}
}
}
}