Lecture2: データの整形(その2)

前回の復習:単語出現頻度表の作成

テキストファイルの読み込み

一行ずつ読み込んで、リストに格納

txt<-readLines("test1.txt")
## [1] "The culture culture Culture and the and and culture     and culture "
## [2] "language culture culture culture the the culture culture language"   
## [3] "the the culture culture culture language Language culture the"       
## [4] ""

一行目の内容

txt[1]
## [1] "The culture culture Culture and the and and culture     and culture "

読み込んだ行数

length(txt)
## [1] 4

スペース&記号による分割

Punctuation characters:
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~.
wordLst<-strsplit(txt,"[[:space:]]|[[:punct:]]")
## [[1]]
##  [1] "The"     "culture" "culture" "Culture" "and"     "the"     "and"    
##  [8] "and"     "culture" ""        ""        ""        ""        "and"    
## [15] "culture"
## 
## [[2]]
## [1] "language" "culture"  "culture"  "culture"  "the"      "the"     
## [7] "culture"  "culture"  "language"
## 
## [[3]]
## [1] "the"      "the"      "culture"  "culture"  "culture"  "language"
## [7] "Language" "culture"  "the"     
## 
## [[4]]
## character(0)

各行のデータを一括化

wordLst<-unlist(wordLst)
##  [1] "The"      "culture"  "culture"  "Culture"  "and"      "the"     
##  [7] "and"      "and"      "culture"  ""         ""         ""        
## [13] ""         "and"      "culture"  "language" "culture"  "culture" 
## [19] "culture"  "the"      "the"      "culture"  "culture"  "language"
## [25] "the"      "the"      "culture"  "culture"  "culture"  "language"
## [31] "Language" "culture"  "the"

小文字に変換

wordLst<-tolower(wordLst)
##  [1] "the"      "culture"  "culture"  "culture"  "and"      "the"     
##  [7] "and"      "and"      "culture"  ""         ""         ""        
## [13] ""         "and"      "culture"  "language" "culture"  "culture" 
## [19] "culture"  "the"      "the"      "culture"  "culture"  "language"
## [25] "the"      "the"      "culture"  "culture"  "culture"  "language"
## [31] "language" "culture"  "the"

空白“”の削除(どちらか好きなほう)

wordLst<-wordLst[nchar(wordLst)>0]
wordLst<- wordLst[wordLst != ""]

単語のToken数

tokens <- length(wordLst)
## [1] 29

単語のTypes数

  • unique()関数は,リストの重複しない要素を返す
types <- length(unique(wordLst))
## [1] 4

TTR: Type-Token Ratioの計算

\[TTR=\frac{types}{tokens} \times 100 \]

types/tokens*100
## [1] 13.7931
小数点2桁で結果を出力
TTR <- round(types/tokens*100,2)
## [1] 13.79

頻度数の集計(頻度順でソート)

freq<-sort(table(wordLst), decreasing=TRUE)
## wordLst
##  culture      the      and language 
##       14        7        4        4

このへんから今日の内容

相対頻度数

全体を1としたときの出現率

relative <- freq / sum(freq)
## wordLst
##   culture       the       and  language 
## 0.4827586 0.2413793 0.1379310 0.1379310

相対頻度の合計

sum(relative)
## [1] 1

小数点

round(relative,2)
## wordLst
##  culture      the      and language 
##     0.48     0.24     0.14     0.14

データ型に変換

freqData <- data.frame(word=rownames(freq),freq=freq)
##              word freq
## culture   culture   14
## the           the    7
## and           and    4
## language language    4
relativeData <- data.frame(word=rownames(relative),freq=relative)
##              word      freq
## culture   culture 0.4827586
## the           the 0.2413793
## and           and 0.1379310
## language language 0.1379310

csvファイルに出力

write.csv(freqData,"freq-test1.csv")

2つのデータ型変数を連結(merge)

freqMtx <- merge(freqData, relativeData, all=T, by="word")
##       word freq.x    freq.y
## 1      and      4 0.1379310
## 2  culture     14 0.4827586
## 3 language      4 0.1379310
## 4      the      7 0.2413793

列に名前をつける

names(freqMtx) <- c("term","raw", "relative")
##       term raw  relative
## 1      and   4 0.1379310
## 2  culture  14 0.4827586
## 3 language   4 0.1379310
## 4      the   7 0.2413793

粗頻度でソート

freqOrder<-order(freqMtx$raw, decreasing=TRUE)
freqMtx <- freqMtx[freqOrder,]
##       term raw  relative
## 2  culture  14 0.4827586
## 4      the   7 0.2413793
## 1      and   4 0.1379310
## 3 language   4 0.1379310

ソート関数(sort, order)

freqMtx$raw
## wordLst
##  culture      the      and language 
##       14        7        4        4
sort(freqMtx$raw)
## wordLst
##      and language      the  culture 
##        4        4        7       14
order(freqMtx$raw)
## [1] 3 4 2 1

単語でソート

freqOrder2<-order(freqMtx$term)
freqMtx2 <- freqMtx[freqOrder2,]
##       term raw  relative
## 1      and   4 0.1379310
## 2  culture  14 0.4827586
## 3 language   4 0.1379310
## 4      the   7 0.2413793

自作関数function():ソートの基準を選択(粗頻度または単語)

mySort <- function(freqData, sortBy="term"){
  if(sortBy=="term"){
    freqOrder<-order(freqData$term)
  }else if(sortBy=="raw"){
    freqOrder<-order(freqData$raw, decreasing=TRUE)
  }
  freqData <- freqData[freqOrder,]
  return(freqData)
}
mySort(freqMtx)
##       term raw  relative
## 1      and   4 0.1379310
## 2  culture  14 0.4827586
## 3 language   4 0.1379310
## 4      the   7 0.2413793
mySort(freqMtx, sortBy="term")
##       term raw  relative
## 1      and   4 0.1379310
## 2  culture  14 0.4827586
## 3 language   4 0.1379310
## 4      the   7 0.2413793
mySort(freqMtx, sortBy="raw")
##       term raw  relative
## 2  culture  14 0.4827586
## 4      the   7 0.2413793
## 1      and   4 0.1379310
## 3 language   4 0.1379310

関数ファイルの作成

alt text

自作関数:getRawFreqMtx

getRawFreqMtx.Rを作成

getRawFreqMtx <- function(filename){
  
  txt<-readLines(filename)
  
  wordLst<-strsplit(txt,"[[:space:]]|[[:punct:]]")
  wordLst<-unlist(wordLst)
  wordLst<-tolower(wordLst)
  wordLst<- wordLst[wordLst != ""]
  
  freq<-table(wordLst)
  data.frame(freq) ->freqData
  freqOrder<-order(freqData$Freq, decreasing=TRUE)
  freqData<-freqData[freqOrder,]
  
  return(freqData)
}

getRawFreqMtx.Rを読み込む

source("getRawFreqMtx.R")

getRawFreqMtx関数の実行例

getRawFreqMtx("test1.txt")
##    wordLst Freq
## 2  culture   14
## 4      the    7
## 1      and    4
## 3 language    4

今日の課題(締め切り10月25日)

課題1

テキストファイル名を引数にして、TTRの計算結果をを出力する関数を作成しなさい。
関数名はgetTTRとし、関数ファイル(getTTR.R)をメールで提出すること。
提出前に、テキストファイル"osaka-u.txt"を使用して、正しく実行できるかを必ず確認すること。

出力イメージ

getTTR("osaka-u.txt")
## [1] 48.24

課題2

テキストファイル名を引数にして、単語の頻度数と相対頻度をマージした行列データを出力する関数を作成しなさい。
関数名はgetRelativeFreqMtxとし、関数ファイル(getRelativeFreqMtx.R)をメールで提出すること。
提出前に、テキストファイル"osaka-u.txt"を使用して、正しく実行できるかを必ず確認すること。

出力イメージ

getFreqMtx("osaka-u.txt")
##           term raw relative
## 211        the  33    0.065
## 21         and  31    0.061
## 146         of  31    0.061
## 228 university  16    0.031
## 112         in  15    0.029