Go language Statistics The number of Chinese words, participle.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The main purpose is to count the number of words appearing in an article, at present it is only a general framework, the results of statistics only two kinds,

One is two words of one is three words, and the accuracy is not high, need to optimize the place too much,

The first note down, back to idle to continue to optimize, the accuracy and processing speed to bring up.

Rookie writing is not good, look at you a lot of advice.



</pre><pre name= "code" class= "plain" >package mainimport ("FMT" "Io/ioutil" "strings") var Tri map[string] int = make (Map[string]int) var Dou map[string]int = Make (Map[string]int) var Num int = 2var Path string = "Test.txt" func mai N () {Buf, _: = Ioutil. ReadFile (Path) N: = Len (Buf) Str: = String (Buf) Get_slice (n, Buf, &str) List: = Paixu (Dou) List_tri: = Paixu (Tri) Getvalue (Rm_duplicate (&list), Dou) Getvalue (Rm_duplicate (&list_tri), Tri)}func get_slice (n int, Buf []byte, Str *string) {var i, x, y int = 0, 0, 0var Th Ree Stringfor I < n-2 {if buf[i] > x = i + 3for x < n-2 {if BUF[X] > $ {s: = string (buf[i:i+3]) + Strin G (buf[x:x+3]) if I: = strings. Count (*str, s); I > 1 {dou[s] = I}y = x + 3for y < n-2 {if buf[y] > {three = s + string (Buf[y:y+3]) break} else {Y++}}break} E LSE {X++}}IF I: = strings. Count (*str, three); I > 1 {tri[three] = I}i = i + 3} else {I++}}}func Paixu (Map map[string]int) []int {var list []intfor _, K: = Range Map {list = AppenD (list, k)}num: = Len (list) for I: = 0; i < num; i++ {for J: = i + 1, j < Num, J + + {if list[i] < List[j] {List[i], list[j] = list[j], List[i]}}}return list}func Getv Alue (list []int, Map Map[string]int) {For _, I: = Range List[:num] {for k, V: = range Map {if i = = v {fmt.  Println (k, "occurrences:", i)}}}}func rm_duplicate (list *[]int) []int {var x []int = []int{}for _, I: = Range *list {if len (x) = = 0 {x = append (x, i)} else {for k, V: = range x {if i = = v {break}if k = = Len (x)-1 {x = append (x, i)}}}}return x}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.