Lecture 10: network描画 |
test1<-"The meaning of Osaka University's motto"
substr(test1,1,4)
## [1] "The "
size=4
len<-nchar(test1)-size+1
ngramLst <- c()
for(i in 1:nchar(test1)){
ngramLst<-rbind(ngramLst,(substr(test1,i,i+size-1)))
}
test2<-"大阪大学は、「大阪にも帝国大学を」という地元大阪府民の熱意と、関係者の努力により"
substr(test2,1,4)
## [1] "大阪大学"
nchar(test2)
## [1] 40
size=4
len<-nchar(test2)-size+1
ngramLst <- c()
for(i in 1:len){
ngramLst<-rbind(ngramLst,(substr(test2,i,i+size-1)))
}
test1<-"The meaning of Osaka University's motto"
wordLst <- strsplit(test1, "[[:space:]]|[[:punct:]]")
wordLst <- unlist(wordLst)
wordLst <- tolower(wordLst)
wordLst <- wordLst[wordLst != ""]
wordLst
## [1] "the" "meaning" "of" "osaka" "university"
## [6] "s" "motto"
size=3
start=1
wordLst[start:(start+size-1)]
## [1] "the" "meaning" "of"
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,] "the" "meaning" "of"
## [2,] "osaka" "university" "s"
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,] "地元" "大阪" "府民"
## [5,] "の" "熱意" "と"
## [6,] "関係" "者" "の"
test1<-"Osaka University: The meaning of Osaka University's motto"
wordLst <- strsplit(test1, "[[:space:]]|[[:punct:]]")
wordLst <- unlist(wordLst)
wordLst <- tolower(wordLst)
wordLst <- wordLst[wordLst != ""]
wordLst
## [1] "osaka" "university" "the" "meaning" "of"
## [6] "osaka" "university" "s" "motto"
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,] "osaka" "university" "the"
## [2,] "the" "meaning" "of"
## [3,] "of" "osaka" "university"
## [4,] "university" "s" "motto"
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,] "osaka" "university"
## [2,] "osaka" "the"
## [3,] "university" "the"
source("netwkPairs.R")
getPairs(strLst[1,])
## [,1] [,2]
## [1,] "osaka" "university"
## [2,] "osaka" "the"
## [3,] "university" "the"
getPairs(strLst[2,])
## [,1] [,2]
## [1,] "the" "meaning"
## [2,] "the" "of"
## [3,] "meaning" "of"
getPairsLst(strLst)
## [,1] [,2]
## [1,] "osaka" "university"
## [2,] "osaka" "the"
## [3,] "university" "the"
## [4,] "the" "meaning"
## [5,] "the" "of"
## [6,] "meaning" "of"
## [7,] "of" "osaka"
## [8,] "of" "university"
## [9,] "osaka" "university"
## [10,] "university" "s"
## [11,] "university" "motto"
## [12,] "s" "motto"
pFreq<-getPairsFreq(strLst)
head(pFreq)
## Term1 Term2 Freq
## 1 osaka university 2
## 2 osaka the 1
## 3 university the 1
## 4 the meaning 1
## 5 the of 1
## 6 meaning of 1
library(igraph)
##
## Attaching package: 'igraph'
## 以下のオブジェクトは 'package:stats' からマスクされています:
##
## decompose, spectrum
## 以下のオブジェクトは '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)))
library(shiny)
runApp("netwk")
#library(RMeCab)
res<-NgramDF("netwk/data/osaka-u-2015_ja.txt", type = 1, N = 2)
head(res)
OUja<-readLines("netwk2/data/osaka-u-2015_ja.txt", encoding = "utf8")
OUja <- OUja[OUja != ""]
wordLst<-splitWdsJa(OUja)
nsize=2
nstep=1
strLst<-getNstr(wordLst,nsize,step=nstep)
res<-getPairsFreq(strLst)
head(res)
実装画面例