LoginSignup
1
0

More than 5 years have passed since last update.

係り受け解析

Posted at

library(dplyr)

library(RMeCab)


sentence=c("日々の出来事を紙などに記録したものである。単なる記録として扱われるものから、文学として扱われるものまで、その内容は様々である。ある人物の生涯にわたって記されるような長期にわたるものから、ある旅、ある職務、ある事件などの間だけ記された短期のものまで、期間・分量も様々であり、西洋・東洋を問わず、世界的に存在する。")

write.table(sentence,"~/txt.txt")

p=NgramDF("txt.txt",type=1,N=2)

freq=RMeCabFreq("txt.txt")

library(stringr)

sentence_num=length(unlist(strsplit(sentence,"。")))

sentences=unlist(strsplit(sentence,"。"))

#一文をやってみる!

j=3

a_sentence=sentences[j]

write.table(sentences[j],"~/txt_sub.txt")

p_sub=NgramDF2("txt_sub.txt",type=1,N=2)

freq_sub=RMeCabFreq("txt_sub.txt")

#Lの計算

w_sub=unique(freq_sub$Term)

#C(1,length(w_sub))を計算

n=length(w_sub)

L=c();und_approx=c();upp_approx=c()

L_fun=function(j){

z<-(1/j)*prod(c(1:(2*(j-1)))/rep(c(1:(j-1)),2))

z[is.nan(z)>0]=1

return(z)

}

for(j in 1:(n-1)){

if(j==1){

approx=c(approx,(2^j)/(4*sqrt(pi)))  

und_approx=c(und_approx,(2^j)/(4*sqrt(pi)))

upp_approx=c(upp_approx,(2^(2*j))/(4*sqrt(pi)))

L=c(L,1)  

}else{  

L=c(L,L_fun(j))

approx=c(approx,(2^j)/(4*sqrt(pi)))  

und_approx=c(und_approx,(2^j)/(4*sqrt(pi)))

upp_approx=c(upp_approx,(2^(2*j))/(4*sqrt(pi)))


}

}



#Lの畳み込みを計算する

L_real=c()

for(j in 1:(n-1)){

L_real=c(L_real,L_fun(j)*L_fun(n-j))  

}

und_approx<L

L<upp_approx

#出発ワードを決める

start_word=freq_sub$Term[30]

word_vectors=c()

times=100

for(k in 1:times){

if(k==1){  

p_sub_sub = p_sub %>% filter(Ngram1==start_word)

word_vectors=c(word_vectors,p_sub_sub$Ngram2[p_sub_sub$Freq==max(p_sub_sub$Freq)])

word=p_sub_sub$Ngram2[p_sub_sub$Freq==max(p_sub_sub$Freq)]

}else{

p_sub_sub = p_sub %>% filter(Ngram1==word)

if(length(p_sub_sub)>0){

word_vectors=c(word_vectors,p_sub_sub$Ngram2[p_sub_sub$Freq==max(p_sub_sub$Freq)])

word=p_sub_sub$Ngram2[p_sub_sub$Freq==max(p_sub_sub$Freq)]

}

}

}

word_vectors=word_vectors[word_vectors!="\""]

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0