Lecture4: 頻度行列の作成
作業ディレクトリの確認
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 != ""]
freq <- table(wordLst)
freq_data<-sort(freq, decreasing=TRUE)
先頭のデータを抽出
head(freq_data)
## wordLst
## it and is a as general
## 5 3 3 2 2 2
頻度テーブルをデータ型に変換
(freqData <- data.frame(freq_data))
tokensの算出
sum(freq_data)
## [1] 71
sum(freqData$Freq)
## [1] 71
相対頻度テーブル
(relative<-sort(freq/sum(freqData$Freq), decreasing=TRUE))
## wordLst
## it and is a as general
## 0.07042254 0.04225352 0.04225352 0.02816901 0.02816901 0.02816901
## in infectious spread such symptoms 19
## 0.02816901 0.02816901 0.02816901 0.02816901 0.02816901 0.01408451
## 2 an appear been before by
## 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451
## called caused causes contact coronavirus cough
## 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451
## cov covid disease diseases distancing droplet
## 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451
## fever follow for habitually has important
## 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451
## mainly mask may or out pointed
## 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451
## preventing public sars social strategies that
## 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451
## the therefore through to transmission wearing
## 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451 0.01408451
## when
## 0.01408451
#sum(freq/tokens)
相対頻度テーブルをデータフレームに変換
(relativeData <- data.frame(relative))
2つのデータ型変数を連結(merge)
raw_relative <- merge(freqData, relativeData, all=T, by="wordLst")
head(raw_relative)
出現単語の情報を行ラベルにコピー
rownames(raw_relative)<-as.character(raw_relative[,1])
出現単語の情報(1列目)を削除
raw_relative<-raw_relative[-1]
colnames(raw_relative) <- c("raw", "relative")
head(raw_relative)
頻度順にソート
raw_relative<-raw_relative[order(raw_relative$raw, decreasing = TRUE),]
head(raw_relative)
データサイズ
dim(raw_relative)
## [1] 55 2
データの抽出(頻度数2以上)
raw_relative[raw_relative$raw>=2,]
View関数
View(raw_relative)
配列の条件抽出
配列の条件抽出:行・列
raw_relative[1,]
raw_relative[1:2,]
raw_relative[,2]
raw_relative[2,2]
配列の条件抽出:粗頻度(2以上4より小さい)の条件で、行を抽出
raw_relative[raw_relative$raw >=2 & raw_relative$raw < 4, ]
配列の条件抽出:単語の条件で、行を抽出
raw_relative[rownames(raw_relative)=="covid",]
raw_relative[rownames(raw_relative) %in% c("covid","coronavirus","spread"),]
条件文if
calcSQRT
calcSQRT<- function(value) {
return(sqrt(value))
}
calc2ndPower
calc2ndPower<- function(value) {
return (value^2)
}
実行
tmp <- 100
calcSQRT(tmp)
## [1] 10
calc2ndPower(tmp)
## [1] 10000
条件文1
分岐条件: 分類値
calcTest1 <- function(value, type=1){
if(type==1){
ans <- calcSQRT(value)
}else if(type==2){
ans <- calc2ndPower(value)
}
return(ans)
}
実行
calcTest1(100)
## [1] 10
calcTest1(100,2)
## [1] 10000
条件文2
分岐条件: 真偽値
calcTest2 <- function(value, sqrtFlag=FALSE){
if(sqrtFlag==TRUE){
ans <- calcSQRT(value)
}else{
ans <- calc2ndPower(value)
}
return(ans)
}
実行
calcTest2(100)
## [1] 10000
calcTest2(100, sqrtFlag=TRUE)
## [1] 10
練習:引数に頻度タイプの選択を持ち、単語リスト情報から、粗頻度もしくは相対頻度のデータフレームが戻り値となる関数をファイル”utils.R”内に記述
第1引数: 単語リスト
第2引数: 頻度のタイプ(分類or真偽値)
utils.Rファイルの読み込み
source("utils.R")
getFreq関数の実行例
getFreq(wordLst)
getFreq関数の実行例
getFreq(wordLst, relative=TRUE)
本日の課題(締切11月10日): RTTR(Root Type-Token Ratio) Guiraudの値を計算する関数calcRTTRの作成
引数: 英文テキストファイル
戻り値: Guiraud計算値
calcRTTR関数の実行1
calcRTTR("sample_texts/sample_en.txt")
## [1] 6.527299
calcRTTR関数の実行2
calcRTTR("shiny.txt")
## [1] 5.714286