多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > php教程 > Golang實(shí)現(xiàn)詞頻統(tǒng)計(jì)

Golang實(shí)現(xiàn)詞頻統(tǒng)計(jì)

來源:程序員人生   發(fā)布時(shí)間:2015-05-14 09:10:01 閱讀次數(shù):3334次

本例使用golang實(shí)現(xiàn)詞頻統(tǒng)計(jì)。步驟:

(1)從文件中讀取1篇文章。

(2)統(tǒng)計(jì)詞頻,按單詞出現(xiàn)的頻率從大到小進(jìn)行排序。

(3)寫入到文件中。

注:任何非英文字母的符號均認(rèn)為是單詞分隔符(即同等于空格)。

效力:使用本程序統(tǒng)計(jì)1篇150W單詞的文章,大約需要70ms.

1.核心代碼:

package wordtest import ( "bytes" "fmt" "io/ioutil" "os" "runtime" "sort" "strings" "time" ) //簡單的詞頻統(tǒng)計(jì)任務(wù) func CountTestBase(inputFilePath string, outputFilePath string) { //時(shí)間開始點(diǎn) start := time.Now().UnixNano() / 1e6 //讀取文件 fileData, err := ioutil.ReadFile(inputFilePath) CheckError(err, "read file") var fileText string = string(fileData) //根據(jù)CPU核數(shù)新開協(xié)程 newRountineCount := runtime.NumCPU()*2 - 1 runtime.GOMAXPROCS(newRountineCount + 1) //切分文件 parts := splitFileText(fileText, newRountineCount) var ch chan map[string]int = make(chan map[string]int, newRountineCount) for i := 0; i < newRountineCount; i++ { go countTest(parts[i], ch) } //主線程接收數(shù)據(jù) var totalWordsMap map[string]int = make(map[string]int, 0) completeCount := 0 for { receiveData := <-ch for k, v := range receiveData { totalWordsMap[strings.ToLower(k)] += v } completeCount++ if newRountineCount == completeCount { break } } //添加進(jìn)slice,并排序 list := make(WordCountBeanList, 0) for k, v := range totalWordsMap { list = append(list, NewWordCountBean(k, v)) } sort.Sort(list) //時(shí)間結(jié)束點(diǎn) end := time.Now().UnixNano() / 1e6 fmt.Printf("time consume:%dms ", end-start) //輸出 wordsCount := list.totalCount() var data bytes.Buffer data.WriteString(fmt.Sprintf("程序履行:%dms ", end-start)) data.WriteString(fmt.Sprintf("文章總單詞數(shù):%d ", wordsCount)) for _, v := range list { var percent float64 = 100.0 * float64(v.count) / float64(wordsCount) _, err := data.WriteString(fmt.Sprintf("%s: %d, %3.2f%% ", v.word, v.count, percent)) CheckError(err, "bytes.Buffer, WriteString") } err = ioutil.WriteFile(outputFilePath, []byte(data.String()), os.ModePerm) CheckError(err, "ioutil.WriteFile") } func countTest(text string, ch chan map[string]int) { var wordMap map[string]int = make(map[string]int, 0) //按字母讀取,除26個(gè)字母(大小寫)以外的所有字符均認(rèn)為是分隔符 startIndex := 0 letterStart := false for i, v := range text { if (v >= 65 && v <= 90) || (v >= 97 && v <= 122) { if !letterStart { letterStart = true startIndex = i } } else { if letterStart { wordMap[text[startIndex:i]]++ letterStart = false } } } //最后1個(gè)單詞 if letterStart { wordMap[text[startIndex:]]++ } ch <- wordMap } //將全文分成n段 func splitFileText(fileText string, n int) []string { length := len(fileText) parts := make([]string, n) lastPostion := 0 for i := 0; i < n⑴; i++ { position := length / n * (i + 1) for string(fileText[position]) != " " { position++ } parts[i] = fileText[lastPostion:position] lastPostion = position } //最后1段 parts[n⑴] = fileText[lastPostion:] return parts } func CheckError(err error, msg string) { if err != nil { panic(msg + "," + err.Error()) } }
2.1個(gè)struct

package wordtest type WordCountBean struct { word string count int } func NewWordCountBean(word string, count int) *WordCountBean { return &WordCountBean{word, count} } type WordCountBeanList []*WordCountBean func (list WordCountBeanList) Len() int { return len(list) } func (list WordCountBeanList) Less(i, j int) bool { if list[i].count > list[j].count { return true } else if list[i].count < list[j].count { return false } else { return list[i].word < list[j].word } } func (list WordCountBeanList) Swap(i, j int) { var temp *WordCountBean = list[i] list[i] = list[j] list[j] = temp } func (list WordCountBeanList) totalCount() int { totalCount := 0 for _, v := range list { totalCount += v.count } return totalCount }
3.主函數(shù):

package main import ( "WordsTest/wordtest" ) func main() { inputFilePath := "files/article.txt" outputFilePath := "files/hanjun-result.txt" wordtest.CountTestBase(inputFilePath, outputFilePath) }




生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 亚洲第一网址 | 亚洲天堂第一页 | 国产在线高清视频 | 亚洲欧美日韩不卡 | 国产一级特黄aaaa大片野外 | 操综合网 | 日本护士高清xxxxx | 91精品国产91热久久p | 女人毛片a毛片久久人人 | 偷柏自拍亚洲欧美综合在线图 | 在线免费观看一级毛片 | 视频在线h | 久久精品国产欧美成人 | 黑人性xxx | 日本一级毛片免费播放 | 国产一区二三区 | 国产一区日韩二区欧美三区 | 在线五月婷婷 | 老司机免费福利在线观看 | 最新国产福利在线观看 | 2022偷拍午夜视频在线播放 | 久草午夜视频 | 亚洲免费成人在线 | 在线观看免费黄色网址 | 亚洲成a人不卡在线观看 | 久久96国产精品久久久 | 欧美二区在线观看 | 性生活免费视频网站 | 国产三区视频在线观看 | 国产一区二区三区久久 | 在线欧美一级毛片免费观看 | 性欧美videos另类视频 | 一区二区三区影视 | 欧美另类视频一区二区三区 | free asian xxxxx黑人 | 日本一区二区三区欧美在线观看 | 精品国产一区二区 | 意大利极品xxxxhd | 国产一区二区三区在线免费观看 | 尤物视频在线观看网站 | 福利视频一二区 |