{"id":2541,"date":"2015-11-21T23:36:37","date_gmt":"2015-11-21T15:36:37","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2541"},"modified":"2015-11-21T23:36:37","modified_gmt":"2015-11-21T15:36:37","slug":"%e7%94%a8go%e7%bb%9f%e8%ae%a1%e6%96%87%e4%bb%b6%e4%b8%ad%e5%8d%95%e8%af%8d%e7%9a%84%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2541.html","title":{"rendered":"\u7528Go\u7edf\u8ba1\u6587\u4ef6\u4e2d\u5355\u8bcd\u7684\u4e2a\u6570"},"content":{"rendered":"<p>=Start=<\/p>\n<h6>\u7f18\u7531\uff1a<\/h6>\n<p>\u6bcf\u5b66\u4e60\u4e00\u95e8\u65b0\u8bed\u8a00\uff0c\u6211\u5c31\u4f1a\u62ff\u521a\u5b66\u4f1a\u7684\u8bed\u6cd5\u53bb\u5b9e\u73b0\u4e00\u4e9b\u5c0f\u529f\u80fd\/\u7a0b\u5e8f\uff0c\u4ee5\u6b64\u52a0\u6df1\u5bf9\u8bed\u6cd5\u7684\u8ba4\u8bc6\u548c\u719f\u6089\u7a0b\u5ea6\u3002\u8fd9\u5176\u4e2d\u6bd4\u8f83\u597d\u73a9\u53c8\u5b9e\u7528\u7684\u5c31\u5305\u62ec\u7edf\u8ba1\u6587\u4ef6\u4e2d\u5355\u8bcd\u7684\u4e2a\u6570\uff0c\u6d89\u53ca\u5230\u6587\u4ef6\u64cd\u4f5c\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001<strong><span style=\"color: #ff0000;\">\u53bb\u91cd\u64cd\u4f5c<\/span><\/strong>\u7b49\uff0c\u662f\u4e00\u4e2a\u6bd4\u8f83\u597d\u7684\u5b66\u4e60\u6848\u4f8b\u3002<\/p>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package main\n\nimport (\n    \"bufio\"\n    \"fmt\"\n    \"io\"\n    \"log\"\n    \"os\"\n    \"path\/filepath\"\n    \"runtime\"\n    \"sort\"\n    \"strings\"\n    \"unicode\"\n    \"unicode\/utf8\"\n)\n\nfunc main() {\n    if len(os.Args) == 1 || os.Args[1] == \"-h\" || os.Args[1] == \"--help\" {\n        fmt.Printf(\"usage: %s &lt;file1&gt; [&lt;file2&gt; [... &lt;fileN&gt;]]\\n\", filepath.Base(os.Args[0]))\n        os.Exit(1)\n    }\n\n    frequencyForWord := map[string]int{} \/\/ Same as: make(map[string]int)\n    for _, filename := range commandLineFiles(os.Args[1:]) {\n        updateFrequencies(filename, frequencyForWord)\n    }\n    \/\/ fmt.Println(frequencyForWord)\n    \/\/ reportByWords(frequencyForWord)\n    wordsForFrequency := invertStringIntMap(frequencyForWord)\n    \/\/ fmt.Println(wordsForFrequency)\n    reportByFrequency(wordsForFrequency)\n}\n\nfunc commandLineFiles(files []string) []string {\n    if runtime.GOOS == \"windows\" {\n        args := make([]string, 0, len(files))\n        for _, name := range files {\n            if matches, err := filepath.Glob(name); err != nil {\n                args = append(args, name) \/\/ Invalid pattern\n            } else if matches != nil { \/\/ At least one match\n                args = append(args, matches...)\n            }\n        }\n        return args\n    }\n    return files\n}\n\nfunc updateFrequencies(filename string, frequencyForWord map[string]int) {\n    var file *os.File\n    var err error\n    if file, err = os.Open(filename); err != nil {\n        log.Println(\"failed to open the file: \", err)\n        return\n    }\n    defer file.Close()\n    reader := bufio.NewReader(file)\n    for {\n        line, err := reader.ReadString('\\n')\n        for _, word := range SplitOnNonLetters(strings.TrimSpace(line)) {\n            if len(word) &gt; utf8.UTFMax || utf8.RuneCountInString(word) &gt; 1 {\n                frequencyForWord[strings.ToLower(word)] += 1\n            }\n        }\n        if err != nil {\n            if err != io.EOF {\n                log.Println(\"failed to finish reading the file: \", err)\n            }\n            break\n        }\n    }\n}\n\n\/\/ SplitOnNonLetters() \u5bf9 s \u7528 \u975e\u5b57\u6bcd\u5b57\u7b26 \u8fdb\u884c\u5207\u5206\uff0c\u8fd4\u56de s \u4e2d\u5305\u542b\u7684\u5355\u8bcd\u5217\u8868\nfunc SplitOnNonLetters(s string) []string {\n    notALetter := func(char rune) bool { return !unicode.IsLetter(char) }\n    return strings.FieldsFunc(s, notALetter)\n}\n\nfunc reportByWords(frequencyForWord map[string]int) {\n    words := make([]string, 0, len(frequencyForWord))\n    wordWidth, frequencyWidth := 0, 0\n    for word, frequency := range frequencyForWord {\n        words = append(words, word)\n        if width := utf8.RuneCountInString(word); width &gt; wordWidth {\n            wordWidth = width\n        }\n        if width := len(fmt.Sprint(frequency)); width &gt; frequencyWidth {\n            frequencyWidth = width\n        }\n    }\n    sort.Strings(words)\n    gap := wordWidth + frequencyWidth - len(\"Word\") - len(\"Frequency\")\n    fmt.Printf(\"Word %*s%s\\n\", gap, \" \", \"Frequency\") \/\/ fmtp.Printf \u7684 %*s \u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u2014\u2014\u6700\u5927\u5bbd\u5ea6 &amp; \u8981\u6253\u5370\u7684\u5b57\u7b26\u4e32\n    for _, word := range words {\n        fmt.Printf(\"%-*s %*d\\n\", wordWidth, word, frequencyWidth, frequencyForWord[word])\n    }\n}\n\n\/\/ \u5bf9 map[string][int] \u8fdb\u884c\u53cd\u8f6c\u65f6\uff0c\u9700\u8981\u6ce8\u610f int \u53ef\u80fd\u4f1a\u6709\u76f8\u540c\u5927\u5c0f\uff0c\u800c string \u4e0d\u5e94\u8be5\u88ab\u8986\u76d6\uff0c\u800c\u5e94\u8be5\u88ab\u6dfb\u52a0\n\/\/ \u6240\u4ee5\u53cd\u8f6c\u4e4b\u540e\u7684\u7c7b\u578b\u4e3a map[int][]string\nfunc invertStringIntMap(stringIntMap map[string]int) map[int][]string {\n    intStrArrayMap := make(map[int][]string, len(stringIntMap))\n    for key, value := range stringIntMap {\n        intStrArrayMap[value] = append(intStrArrayMap[value], key)\n    }\n    return intStrArrayMap\n}\n\nfunc reportByFrequency(wordsForFrequency map[int][]string) {\n    frequencies := make([]int, 0, len(wordsForFrequency)) \/\/ length = 0, capacity = len(wordsForFrequency)\n    for frequency := range wordsForFrequency {\n        frequencies = append(frequencies, frequency)\n    }\n    sort.Ints(frequencies)\n    width := len(fmt.Sprint(frequencies[len(frequencies)-1])) \/\/ \u56e0\u4e3a frequencies \u5df2\u7ecf\u662f\u6392\u8fc7\u5e8f\u4e86\u7684\uff0c\u6240\u4ee5\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u5bbd\u7684\n    fmt.Println(\"Frequency \u2192 Words\")\n    for _, frequency := range frequencies {\n        words := wordsForFrequency[frequency]\n        sort.Strings(words)\n        fmt.Printf(\"%*d %s\\n\", width, frequency, strings.Join(words, \", \"))\n    }\n}<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li>\u300aGo\u8bed\u8a00\u7a0b\u5e8f\u8bbe\u8ba1\u300b<\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6bcf\u5b66\u4e60\u4e00\u95e8\u65b0\u8bed\u8a00\uff0c\u6211\u5c31\u4f1a\u62ff\u521a\u5b66\u4f1a\u7684\u8bed\u6cd5\u53bb\u5b9e\u73b0\u4e00\u4e9b\u5c0f\u529f\u80fd\/\u7a0b\u5e8f\uff0c\u4ee5\u6b64\u52a0\u6df1\u5bf9\u8bed\u6cd5\u7684\u8ba4\u8bc6\u548c\u719f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[563,564,342,19],"class_list":["post-2541","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-go","tag-golang","tag-map","tag-tips"],"views":6694,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2541"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2541\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}