1. 程式人生 > >分詞及詞雲圖繪製-R語言

分詞及詞雲圖繪製-R語言

分詞的方法模型很多種,最大概率法(mp)、隱式馬爾可夫模型(hmm)、索引模型(query)以及混合模型(mix)等,字典的型別也分為很多種,混合模型的結果一般情況下效果較優,下面利用R語言簡單繪製雲圖,達到視覺化的效果。

wordcloud2函式:
wordcloud2(data, size = 1, minSize = 0, gridSize =  0,
    fontFamily = 'Segoe UI', fontWeight = 'bold',
    color = 'random-dark', backgroundColor = "white",
    minRotation =
-pi/4, maxRotation = pi/4, shuffle = TRUE, rotateRatio = 0.4, shape = 'circle', ellipticity = 0.65, widgetsize = NULL, figPath = NULL, hoverFunction = NULL) 常用引數說明: data:待分詞文字 fontFamily :字型 color:詞顏色設定 backgroundColor:背景顏色 minRotate:字型最小旋轉度 maxRotate:字型最大旋轉度 shuttle:設定為T,每次生成的圖均不同 rotateRatio:詞旋轉的可能性 shape:設定詞雲圖形狀,預設為橢圓 ellipticity:圖形的平整度
worker函式:
worker(type = "mix", dict = DICTPATH, hmm = HMMPATH,
    user = USERPATH,idf = IDFPATH, stop_word = STOPPATH, 
    write = T, qmax = 20, topn = 5,encoding = "UTF-8", 
    detect = T, symbol = F, lines = 1e+05,output = NULL, 
    bylines = F, user_weight = "max")
常用引數說明:
type:分詞模型選擇
dict:主詞典路徑
user:使用者詞典路徑
topn:取關鍵詞個數,僅對simhash and keywords兩種方式起作用
bylines:為T
,則按行讀入 user_weight:使用者詞典權重( "min""max""median"

R語言例項

library(jiebaR)
library(cidian)#用於將細胞詞庫轉化為R可操作的dict或txt格式
library(wordcloud2)

text<-read.table("E://rdata//reply.txt",encoding="UTF-8",header=F,as.is=T)##讀取待分詞檔案
mixseg<-worker(type="mix",user = "e:/wordseg/qihuan.dict",stop_word = "e:/wordseg/stopwords.txt")#設定分詞方法、詞典及停詞,這裡採用混合模型
seg<-mixseg[text[,1]]#獲取分詞結果
#stop_word<-c("一個","哈哈")#自定義新增停詞
#seg<-filter_segment(seg_result,stopword)
seg<-seg[nchar(seg)>1] #去除字元長度小於2的詞語
num<-table(seg)
df<-data.frame(num)
wordcloud2(df)#採用預設引數,可修改

結果範例
這裡寫圖片描述