Lecture 10: network描画 |
test1<-"Together, We will make America strong again."
substr(test1,1,4)
## [1] "Toge"
size=4
len<-nchar(test1)-size+1
ngramLst <- c()
for(i in 1:nchar(test1)){
ngramLst<-rbind(ngramLst,(substr(test1,i,i+size-1)))
}
head(ngramLst)
## [,1]
## [1,] "Toge"
## [2,] "oget"
## [3,] "geth"
## [4,] "ethe"
## [5,] "ther"
## [6,] "her,"
test2<-"ともに力を合わせ、アメリカを再び偉大な国にします。"
substr(test2,1,4)
## [1] "ともに力"
nchar(test2)
## [1] 25
size=4
len<-nchar(test2)-size+1
ngramLst <- c()
for(i in 1:len){
ngramLst<-rbind(ngramLst,(substr(test2,i,i+size-1)))
}
head(ngramLst)
## [,1]
## [1,] "ともに力"
## [2,] "もに力を"
## [3,] "に力を合"
## [4,] "力を合わ"
## [5,] "を合わせ"
## [6,] "合わせ、"
test1<-"Together, We will make America strong again."
wordLst <- strsplit(test1, "[[:space:]]|[[:punct:]]")
wordLst <- unlist(wordLst)
wordLst <- tolower(wordLst)
wordLst <- wordLst[wordLst != ""]
wordLst
## [1] "together" "we" "will" "make" "america" "strong"
## [7] "again"
size=3
start=1
wordLst[start:(start+size-1)]
## [1] "together" "we" "will"
strLst<-c()
len<-length(wordLst)-size+1
for(i in seq(1,len,size)) {
strLst<-rbind(strLst,wordLst[i:(i+size-1)])
}
strLst
## [,1] [,2] [,3]
## [1,] "together" "we" "will"
## [2,] "make" "america" "strong"
install.packages("RMeCab", repos = "http://rmecab.jp/R")
install.packages("RMeCab", repos = "http://rmecab.jp/R")
##
## The downloaded binary packages are in
## /var/folders/35/pjj96qbn0757lwfd7_bf5cc00000gp/T//Rtmp91iD6B/downloaded_packages
library("RMeCab")
wordLst<-unlist(RMeCabC(test2))
wordLst
## 副詞 名詞 助詞 動詞 記号 名詞
## "ともに" "力" "を" "合わせ" "、" "アメリカ"
## 助詞 副詞 名詞 助動詞 名詞 助詞
## "を" "再び" "偉大" "な" "国" "に"
## 動詞 助動詞 記号
## "し" "ます" "。"
wordLst <- wordLst[names(wordLst)!="記号"]
wordLst
## 副詞 名詞 助詞 動詞 名詞 助詞
## "ともに" "力" "を" "合わせ" "アメリカ" "を"
## 副詞 名詞 助動詞 名詞 助詞 動詞
## "再び" "偉大" "な" "国" "に" "し"
## 助動詞
## "ます"
size=3
len<-length(wordLst)-size+1
strLst<-c()
for(i in seq(1,len,size)) {
strLst<-rbind(strLst,wordLst[i:(i+size-1)])
}
strLst
## 副詞 名詞 助詞
## [1,] "ともに" "力" "を"
## [2,] "合わせ" "アメリカ" "を"
## [3,] "再び" "偉大" "な"
## [4,] "国" "に" "し"
test1<-"Together, We will make America strong again. We will make wealthy again. We will make America proud again. We will make America safe again."
wordLst <- strsplit(test1, "[[:space:]]|[[:punct:]]")
wordLst <- unlist(wordLst)
wordLst <- tolower(wordLst)
wordLst <- wordLst[wordLst != ""]
wordLst
## [1] "together" "we" "will" "make" "america" "strong"
## [7] "again" "we" "will" "make" "wealthy" "again"
## [13] "we" "will" "make" "america" "proud" "again"
## [19] "we" "will" "make" "america" "safe" "again"
size=3
step=2
strLst<-c()
len<-length(wordLst)-size+1
for(i in seq(1,len,step)) {
strLst<-rbind(strLst,wordLst[i:(i+size-1)])
}
strLst
## [,1] [,2] [,3]
## [1,] "together" "we" "will"
## [2,] "will" "make" "america"
## [3,] "america" "strong" "again"
## [4,] "again" "we" "will"
## [5,] "will" "make" "wealthy"
## [6,] "wealthy" "again" "we"
## [7,] "we" "will" "make"
## [8,] "make" "america" "proud"
## [9,] "proud" "again" "we"
## [10,] "we" "will" "make"
## [11,] "make" "america" "safe"
str<-strLst[1,]
pLst <- c()
for(i in 1:(length(str)-1)){
for(j in (i+1):length(str)){
tmp<-cbind(str[i],str[j])
pLst<-rbind(pLst,tmp)
}
}
pLst
## [,1] [,2]
## [1,] "together" "we"
## [2,] "together" "will"
## [3,] "we" "will"
source("netwkPairs.R")
getPairs(strLst[1,])
## [,1] [,2]
## [1,] "together" "we"
## [2,] "together" "will"
## [3,] "we" "will"
getPairs(strLst[2,])
## [,1] [,2]
## [1,] "will" "make"
## [2,] "will" "america"
## [3,] "make" "america"
getPairsLst(strLst)
## [,1] [,2]
## [1,] "together" "we"
## [2,] "together" "will"
## [3,] "we" "will"
## [4,] "will" "make"
## [5,] "will" "america"
## [6,] "make" "america"
## [7,] "america" "strong"
## [8,] "america" "again"
## [9,] "strong" "again"
## [10,] "again" "we"
## [11,] "again" "will"
## [12,] "we" "will"
## [13,] "will" "make"
## [14,] "will" "wealthy"
## [15,] "make" "wealthy"
## [16,] "wealthy" "again"
## [17,] "wealthy" "we"
## [18,] "again" "we"
## [19,] "we" "will"
## [20,] "we" "make"
## [21,] "will" "make"
## [22,] "make" "america"
## [23,] "make" "proud"
## [24,] "america" "proud"
## [25,] "proud" "again"
## [26,] "proud" "we"
## [27,] "again" "we"
## [28,] "we" "will"
## [29,] "we" "make"
## [30,] "will" "make"
## [31,] "make" "america"
## [32,] "make" "safe"
## [33,] "america" "safe"
pFreq<-getPairsFreq(strLst)
head(pFreq)
## Term1 Term2 Freq
## 1 together we 1
## 2 together will 1
## 3 we will 4
## 4 will make 4
## 5 will america 1
## 6 make america 3
install.packages("igraph")
library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
wng<-graph.data.frame(pFreq)
plot(wng)
wng<-as.undirected(graph.data.frame(pFreq))
plot(wng)
wng<-as.undirected(graph.data.frame(pFreq))
E(wng)$weight<-pFreq$Freq
deg<-degree(wng)
plot(wng,edge.width=E(wng)$weight,vertex.size=30*(deg/max(deg)))
install.packages("d3Network")
library(shiny)
runApp("shiny_apps/netwk")
#library(RMeCab)
res<-NgramDF("shiny_apps/netwk/data/Trump_ja.txt", type = 1, N = 2)
head(res)
res
Trumpja<-readLines("shiny_apps/netwk/data/Trump_ja.txt", encoding = "utf8")
Trumpja <- Trumpja[Trumpja != ""]
wordLst<-splitWdsJa(Trumpja)
head(wordLst)
nsize=2
nstep=1
strLst<-getNstr(wordLst,nsize,step=nstep)
res<-getPairsFreq(strLst)
head(res)
Omaba <- readLines("shiny_apps/netwk/data/Omaba_en.txt", encoding = "utf8")
OmabawrdLst<-splitWdsEn(Omaba)
wordLst <- OmabawrdLst
nsize=2
nstep=1
strLst<- data.frame(getPairsLst(strLst))
res<-getPairsFreq(strLst)
head(res)
実装画面例