Skip to content

Instantly share code, notes, and snippets.

@lixin9311
Last active January 19, 2019 15:05
Show Gist options
  • Save lixin9311/98eb6ecb9e6c0dd1dc91de86fa65ee9c to your computer and use it in GitHub Desktop.
Save lixin9311/98eb6ecb9e6c0dd1dc91de86fa65ee9c to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"io/ioutil"
"strings"
"sync"
)
var (
dict = []string{}
initials = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
wg = sync.WaitGroup{}
wordFile = flag.String("dict", "pinyin.txt", "the word list you want to load.")
n = flag.Int("n", 0, "n/N id of the program")
N = flag.Int("N", 1, "n/N")
d = flag.Int("d", 1, "d/D number of words")
D = flag.Int("D", 4, "d/D")
l = flag.Int("l", 5, "l/L length of the phrase")
L = flag.Int("L", 10, "l/L")
)
func loadDict(filename string) error {
dat, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
wordlist := string(dat)
lines := strings.Split(wordlist, "\n")
for _, line := range lines {
words := strings.Split(line, " ")
dict = append(dict, words...)
}
return nil
}
func main() {
flag.Parse()
if err := loadDict(*wordFile); err != nil {
fmt.Println("Failed to load "+*wordFile, err)
return
}
list := make([]string, 0)
// 加首字母
list = append(list, "")
for _, initial := range initials {
list = append(list, initial+".")
}
// 先组装词 // 总长度8-13 必定包含数字 所以基础长度应该在10以内,5以上
for k, base := range list {
if k%*N == *n {
wg.Add(1)
go wordassemble(base, 1)
}
}
wg.Wait()
}
func wordassemble(base string, level int) {
if level == 1 {
defer func() {
wg.Done()
}()
}
if level == *N || len(base) > *L {
return
}
if len(base) >= *l {
if level >= *n {
fmt.Println(base)
}
}
for _, app := range dict {
wordassemble(base+app, level+1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment