– 型
– merge関数
getwd()
[1] "/cloud/project"
txt<-readLines("sample_texts/sample_en.txt")
wordLst<-strsplit(txt,"[[:space:]]|[[:punct:]]")
wordLst<-unlist(wordLst)
wordLst<-tolower(wordLst)
wordLst<- wordLst[wordLst != ""]
[.,!?:...]
の”...”は単なるellipsisの可能性
source("calc.R")
calcTTR1(wordLst)
[1] 77.465
tokens <- length(wordLst)
types <- length(unique(wordLst))
calcTTR2(tokens, types)
[1] 77.465
source("calc.R")
calcRTTR("sample_texts/sample_en.txt")
[1] 6.527299
calcRTTR("sample_texts/sample_ja_1.txt")
[1] 5.277388
calcRTTR("sample_texts/sample_ch.txt")
Warning: incomplete final line found on 'sample_texts/sample_ch.txt'
[1] 6.82191
txt<-readLines("sample_texts/sample_ja_1.txt")
wordLst<-strsplit(txt,"[[:space:]]|[[:punct:]]")
wordLst<-unlist(wordLst)
#wordLst<-tolower(wordLst)
wordLst<- wordLst[wordLst != ""]
freq <- table(wordLst)
freq_data<-sort(freq, decreasing=TRUE)
freqData <- data.frame(freq_data)
head(freqData)
sum(freq_data)
[1] 181
sum(freqData$Freq)
[1] 181
(relative<-sort(freq/sum(freqData$Freq), decreasing=TRUE))
wordLst
に を と が の は ください
0.066298343 0.044198895 0.038674033 0.033149171 0.033149171 0.033149171 0.027624309
て で とき ない ます 1 9
0.027624309 0.027624309 0.027624309 0.027624309 0.027624309 0.022099448 0.022099448
COVID ウイルス コロナ 話 か し や
0.022099448 0.022099448 0.022099448 0.022099448 0.016574586 0.016574586 0.016574586
中 人 体 2 CoV SARS つけ
0.016574586 0.016574586 0.016574586 0.011049724 0.011049724 0.011049724 0.011049724
など なり マスク まわり よう 出 出る
0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724
咳 外 感染 熱 誰 近く distancing
0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.005524862
Social あと あり ある いい うつさ うつし
0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
から こと しかし しまう しれ そして た
0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
だ つける なら なる ませ も ん
0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
入っ 入る 接触 気 空け 話す 間
0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
飛沫
0.005524862
(relativeData <- data.frame(relative))
freqMtx <- merge(freqData, relativeData, all=T, by="wordLst")
head(freqMtx)
freqMtx[,1]
[1] 1 2 9 CoV COVID distancing SARS
[8] Social あと あり ある いい ウイルス うつさ
[15] うつし か が から ください こと コロナ
[22] し しかし しまう しれ そして た だ
[29] つけ つける て で と とき ない
[36] など なら なり なる に の は
[43] ます マスク ませ まわり も や よう
[50] を ん 中 人 体 入っ 入る
[57] 出 出る 咳 外 感染 接触 気
[64] 熱 空け 話 話す 誰 近く 間
[71] 飛沫
71 Levels: に を と が の は ください て で とき ない ます 1 9 COVID ... 飛沫
rownames(freqMtx)<-as.character(freqMtx[,1])
freqMtx<-freqMtx[-1]
colnames(freqMtx) <- c("raw", "relative")
freqMtx<-freqMtx[order(freqMtx$raw, decreasing = TRUE),]
head(freqMtx)
View(freqMtx)
dim(freqMtx)
[1] 71 2
freqMtx[freqMtx$raw>=5,]
freqMtx[1,]
freqMtx[3:5,]
freqMtx[,2]
[1] 0.066298343 0.044198895 0.038674033 0.033149171 0.033149171 0.033149171
[7] 0.027624309 0.027624309 0.027624309 0.027624309 0.027624309 0.027624309
[13] 0.022099448 0.022099448 0.022099448 0.022099448 0.022099448 0.022099448
[19] 0.016574586 0.016574586 0.016574586 0.016574586 0.016574586 0.016574586
[25] 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724
[31] 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724
[37] 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.005524862
[43] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
[49] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
[55] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
[61] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
[67] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
freqMtx$relative
[1] 0.066298343 0.044198895 0.038674033 0.033149171 0.033149171 0.033149171
[7] 0.027624309 0.027624309 0.027624309 0.027624309 0.027624309 0.027624309
[13] 0.022099448 0.022099448 0.022099448 0.022099448 0.022099448 0.022099448
[19] 0.016574586 0.016574586 0.016574586 0.016574586 0.016574586 0.016574586
[25] 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724
[31] 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724
[37] 0.011049724 0.011049724 0.011049724 0.011049724 0.011049724 0.005524862
[43] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
[49] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
[55] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
[61] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
[67] 0.005524862 0.005524862 0.005524862 0.005524862 0.005524862
freqMtx[6,2]
[1] 0.03314917
freqMtx[rownames(freqMtx)=="COVID",]
freqMtx[rownames(freqMtx) %in% c("COVID","コロナ","CoV"),]
txt2<-readLines("sample_texts/sample_ja_2.txt")
wordLst<-strsplit(txt2,"[[:space:]]|[[:punct:]]")
wordLst<-unlist(wordLst)
wordLst<- wordLst[wordLst != ""]
freq2 <- table(wordLst)
freq_data2<-sort(freq2, decreasing=TRUE)
(freqData2 <- data.frame(freq_data2))
freq_sample_ja <-merge(freqData, freqData2, all=T, by="wordLst")
head(freq_sample_ja)
View(freq_sample_ja)
merge(freqData, freqData2, by="wordLst")
merge(freqData, freqData2, all.x=TRUE, by="wordLst")
merge(freqData, freqData2, all.y=TRUE, by="wordLst")
freq_sample_ja[is.na(freq_sample_ja)] <- 0
View(freq_sample_ja)
install.packages("dplyr", dependencies = TRUE)
library(dplyr)
full_join(freqData, freqData2, all = T, by = "wordLst")
inner_join(freqData, freqData2, all = T, by = "wordLst")