99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀R 語言分析《釜山行》人物關(guān)系
R 語言分析《釜山行》人物關(guān)系
2017-04-06
收藏
《釜山行》是一部喪尸災(zāi)難片,其人物少、關(guān)系簡單,非常適合我們學(xué)習(xí)文本處理。這個項目將介紹共現(xiàn)在關(guān)系中的提取,使用 R 編寫代碼實現(xiàn)對《釜山行》文本的人物關(guān)系進行分析

使用到的R包

library(rvest)
library(jiebaR)
library(stringr)
library(readr)
library(tidyverse)
library(network)
library(sna)
library(ggnetwork)
library(igraph)

抓取《釜山行》劇本

url='http://labfile.oss.aliyuncs.com/courses/677/busan.txt'
text=read_html(url)%>%html_text()
#對劇本進行分段
spltext=str_split(text,'\r\n\r\n \r\n\r\n')
names(spltext) = 'spltextname'
subtext=spltext$spltextname

人物關(guān)系挖掘

思路:若兩個人物出現(xiàn)在同一段則對其權(quán)重加1,每段中對應(yīng)關(guān)系出現(xiàn)多少次加多少次

n=length(spltext$spltextname)
data=vector('list',n)
cutter = worker('mix')
for(i in 1:n) data[[i]] = cutter[subtext[i]][which(cutter[subtext[i]] %in% iden)]
#which(cutter[subtext1] %in% iden)
weidata=data.frame(t(combn(iden,2)))
names(weidata)=c('name1','name2')
weiname=rep(0,136)
for(i in 1:136) weiname[i]=paste(weidata$name1[i],weidata$name2[i],sep = '--')
weidata$weiname=weiname
weidata$weight = rep(0,136)
for(i in 1:72){
    if(length(data[[i]]) != 0){
      test=as.data.frame(table(paste(expand.grid(data[[i]],data[[i]])$Var1,
                              expand.grid(data[[i]],data[[i]])$Var2,sep = '--')))
      test$Var1=as.vector(test$Var1)
      test$Freq=test$Freq/max(test$Freq)
      id1=which(test$Var1 %in% weidata$weiname)
      id2=which( weidata$weiname %in% test$Var1)
      weidata$weight[id2]=weidata$weight[id2]+test$Freq[id1]
    }
}


結(jié)果:

weight=0說明兩人沒有過交際,weight越大關(guān)系越密切

繪制人物關(guān)系網(wǎng)絡(luò)圖

taltext=as.data.frame(table(cutter[subtext][which(cutter[subtext] %in% iden)]))
taltext$Var1=as.vector(taltext$Var1)
ind3=rep(0,17)
for(i in 1:17) ind3[i] = taltext$Freq[which(taltext$Var1[i] == iden)]
weidata1 = weidata[which(weidata$weight !=0),]

g1=graph.data.frame(weidata1[,-3], directed = F)
op=par(mar=c(0,0,0,0))
plot(g1, edge.width = E(g1)$weight,
     vertex.size=rank(ind3),
     layout=layout.fruchterman.reingold,
     vertex.color=  ind3,
     vertex.label.cex=0.75,
     vertex.label.color='blue'
     )
par(op)

使用ggplot2風(fēng)格

n=fortify(g1)
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_edges(linetype = 2, color = "grey50",curvature = 0.1) +
  geom_nodes(aes(color =  vertex.names, size =  weight)) +
  geom_nodelabel_repel(aes(color = vertex.names, label = vertex.names),
                       fontface = "bold", box.padding = unit(1, "lines")) +
  theme(legend.position='none',
        axis.text = element_blank(),
        axis.title = element_blank(),
  panel.background = element_rect(fill = "grey25"),
  panel.grid = element_blank()
  )

云詞圖(文本挖掘必備)

require(RColorBrewer)
library(wordcloud)

#這里的停詞是把底層的stop_words.utf-8改為txt格式,改之前先備份
wk<-worker('mix',
  stop_word="D:/Program Files/R/R-3.3.2/library/jiebaRD/dict/stop_words.txt")
textda=wk[subtext]
datext=as.data.frame(table(textda))
datext$textda=as.vector(datext$textda)

textdata=arrange(datext,desc(Freq))
textdata1=textdata[which(textdata$Freq>5),]

pal2 <- brewer.pal(8,"Dark2") 
wordcloud(textdata1$textda,textdata1$Freq,colors=pal2,random.order=FALSE,ot.per=.45)

library(wordcloud2)

wordcloud2(textdata,color="random-light",backgroundColor = 'black')



作者 周世榮
本文轉(zhuǎn)自EasyCharts,轉(zhuǎn)載需授權(quán)


數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }