I went to school yesterday.
I went
went to
to school
school yesterday
setwd("NICER_NNS")
yomikomi <- readLines("JPN501.txt", warn=F) # 一行ずつ読み込む
tmp <- grep("\\*(JPN|NS)", yomikomi, value=T) # 発話部分の検索
tmp1 <- gsub("\\*(JPN|NS)...:\t", "", tmp) # 話者記号の削除
tmp2 <- gsub("[[:punct:]]", "", tmp1) # 句読点の削除
tmp3 <- tolower(tmp2) # すべて小文字に
tmp4 <- strsplit(tmp3, " ") # 半角スペースで、文字列を「切る」
tmp5 <- unlist(tmp4) # リスト形式解除
tmp5 <- tmp5[tmp5 != ""] # 空の要素を除く
head(tmp5, 20)
## [1] "what" "kind" "of" "sports" "do" "you"
## [7] "like" "do" "you" "like" "soccer" "base"
## [13] "ball" "or" "swimming" "there" "are" "many"
## [19] "and" "variety"
str(tmp5)
## chr [1:319] "what" "kind" "of" "sports" "do" "you" "like" "do" "you" ...
setwd("NICER_NNS")
ngram <- NULL # 最後に連語を入れる入れ物用意
yomikomi <- readLines("JPN501.txt", warn=F)
tmp <- grep("\\*(JPN|NS)", yomikomi, value=T)
tmp1 <- gsub("\\*(JPN|NS)...:\t", "", tmp)
tmp2 <- gsub("[[:punct:]]", "", tmp1)
tmp3 <- tolower(tmp2)
tmp4 <- strsplit(tmp3, " ")
tmp5 <- unlist(tmp4)
tmp5 <- tmp5[tmp5 != ""]
for (n in 1:(length(tmp5)-1)){ # すべての単語数マイナス1回繰り返す
bigram <- paste(tmp5[n], tmp5[n+1]) # n番目とn+1番目をくっつけてbigramに
ngram <- c(ngram, bigram) # bigramを足して連語のリストをつくる
}
head(ngram, 20)
## [1] "what kind" "kind of" "of sports" "sports do"
## [5] "do you" "you like" "like do" "do you"
## [9] "you like" "like soccer" "soccer base" "base ball"
## [13] "ball or" "or swimming" "swimming there" "there are"
## [17] "are many" "many and" "and variety" "variety sports"
str(ngram)
## chr [1:318] "what kind" "kind of" "of sports" "sports do" "do you" ...
my2gram <- function(){ # 独自プログラムとして
files <- list.files() # フォルダー内のすべてのファイルの一覧
# ngram <- NULL # ★間違い:ファイル内のすべての連語を入れる
ngram.all <- NULL # すべてのファイルのngramを入れる
for (f in files){ # すべてのファイルに対して
ngram <- NULL # ★訂正後:ファイル内のすべての連語を入れる
yomikomi <- readLines(f, warn=F)
tmp <- grep("\\*(JPN|NS)", yomikomi, value=T)
tmp1 <- gsub("\\*(JPN|NS)...:\t", "", tmp)
tmp2 <- gsub("[[:punct:]]", "", tmp1)
tmp3 <- tolower(tmp2)
tmp4 <- strsplit(tmp3, " ")
tmp5 <- unlist(tmp4)
tmp5 <- tmp5[tmp5 != ""]
for (n in 1:(length(tmp5)-1)){ # 各ファイルごとに
bigram <- paste(tmp5[n], tmp5[n+1])
ngram <- c(ngram, bigram) # 各ファイルごとの連語リスト
}
ngram.all <- c(ngram.all, ngram) # 各ファイルごとの連語リストを合わせていく
}
return(ngram.all)
}
setwd("NICER_NNS")
nns.bigram <- my2gram() # 実行した結果を nns.bigram に入れる
head(nns.bigram)
## [1] "what kind" "kind of" "of sports" "sports do" "do you" "you like"
length(nns.bigram)
## [1] 104551
my2gram.df <- function(){ # 独自プログラムとして
files <- list.files() # フォルダー内のすべてのファイルの一覧
# ngram <- NULL # ★間違い:ファイル内のすべての連語を入れる
ngram.all <- NULL # すべてのファイルのngramを入れる
for (f in files){ # すべてのファイルに対して
ngram <- NULL # ★訂正後:ファイル内のすべての連語を入れる
yomikomi <- readLines(f, warn=F)
tmp <- grep("\\*(JPN|NS)", yomikomi, value=T)
tmp1 <- gsub("\\*(JPN|NS)...:\t", "", tmp)
tmp2 <- gsub("[[:punct:]]", "", tmp1)
tmp3 <- tolower(tmp2)
tmp4 <- strsplit(tmp3, " ")
tmp5 <- unlist(tmp4)
tmp5 <- tmp5[tmp5 != ""]
for (n in 1:(length(tmp5)-1)){ # 各ファイルごとに
bigram <- paste(tmp5[n], tmp5[n+1])
ngram <- c(ngram, bigram) # 各ファイルごとの連語リスト
}
ngram.all <- c(ngram.all, ngram) # 各ファイルごとの連語リストを合わせていく
}
#return(ngram.all)
data.frame(ngram.all)
}
setwd("NICER_NNS")
nns.bigram.df <- my2gram.df() # 実行した結果を nns.bigram.df に入れる
str(nns.bigram.df)
## 'data.frame': 104551 obs. of 1 variable:
## $ ngram.all: chr "what kind" "kind of" "of sports" "sports do" ...
my2gram.df2 <- function(){ # 独自プログラムとして
files <- list.files() # フォルダー内のすべてのファイルの一覧
# ngram <- NULL # ★間違い:ファイル内のすべての連語を入れる
ngram.all <- NULL # すべてのファイルのngramを入れる
for (f in files){ # すべてのファイルに対して
# ngram <- NULL # これをやめる★
yomikomi <- readLines(f, warn=F)
tmp <- grep("\\*(JPN|NS)", yomikomi, value=T)
tmp1 <- gsub("\\*(JPN|NS)...:\t", "", tmp)
tmp2 <- gsub("[[:punct:]]", "", tmp1)
tmp3 <- tolower(tmp2)
tmp4 <- strsplit(tmp3, " ")
tmp5 <- unlist(tmp4)
tmp5 <- tmp5[tmp5 != ""]
for (n in 1:(length(tmp5)-1)){ # 各ファイルごとに
bigram <- paste(tmp5[n], tmp5[n+1])
# ngram <- c(ngram, bigram) # これをやめて、
ngram.all <- c(ngram.all, bigram) # こちらに変更★
}
# ngram.all <- c(ngram.all, ngram) # これもやめる
}
#return(ngram.all)
data.frame(ngram.all)
}
setwd("NICER_NNS")
nns.bigram.df2 <- my2gram.df2() # 実行した結果を nns.bigram.df に入れる
str(nns.bigram.df2)
## 'data.frame': 104551 obs. of 1 variable:
## $ ngram.all: chr "what kind" "kind of" "of sports" "sports do" ...
head(nns.bigram.df)
## ngram.all
## 1 what kind
## 2 kind of
## 3 of sports
## 4 sports do
## 5 do you
## 6 you like
library(dplyr)
##
## 次のパッケージを付け加えます: 'dplyr'
## 以下のオブジェクトは 'package:stats' からマスクされています:
##
## filter, lag
## 以下のオブジェクトは 'package:base' からマスクされています:
##
## intersect, setdiff, setequal, union
table(nns.bigram.df) %>% head()
## ngram.all
## 0 years 05 euro 1 2019 1 euro 1 grandfather
## 1 1 1 2 1
## 1 hour
## 1
table(nns.bigram.df) %>% as.data.frame() %>% head()
## ngram.all Freq
## 1 0 years 1
## 2 05 euro 1
## 3 1 2019 1
## 4 1 euro 2
## 5 1 grandfather 1
## 6 1 hour 1
nns.bigram.df.table <- table(nns.bigram.df) %>% as.data.frame()
library(dplyr)
arrange(nns.bigram.df.table, Freq) %>% head()
## ngram.all Freq
## 1 0 years 1
## 2 05 euro 1
## 3 1 2019 1
## 4 1 grandfather 1
## 5 1 hour 1
## 6 1 i 1
arrange(nns.bigram.df.table, desc(Freq)) %>% head(20)
## ngram.all Freq
## 1 it is 531
## 2 i think 467
## 3 in the 353
## 4 high school 287
## 5 a lot 280
## 6 want to 273
## 7 i was 271
## 8 lot of 263
## 9 there are 260
## 10 have to 242
## 11 in japan 228
## 12 when i 223
## 13 for example 216
## 14 of the 210
## 15 think that 206
## 16 the world 194
## 17 we can 186
## 18 i have 184
## 19 so i 183
## 20 you can 181
nns.bi.freq <- arrange(nns.bigram.df.table, desc(Freq))
head(nns.bi.freq)
## ngram.all Freq
## 1 it is 531
## 2 i think 467
## 3 in the 353
## 4 high school 287
## 5 a lot 280
## 6 want to 273
nns.bi.freq %>% mutate(Rank = row_number()) %>% head()
## ngram.all Freq Rank
## 1 it is 531 1
## 2 i think 467 2
## 3 in the 353 3
## 4 high school 287 4
## 5 a lot 280 5
## 6 want to 273 6
nns.bi.freq.rank <- nns.bi.freq %>% mutate(Rank = row_number())
library(ggplot2)
nns.bi.freq.rank.30 <- head(nns.bi.freq.rank, 30)
g <- ggplot(nns.bi.freq.rank.30)
g <- g + aes(x = Rank, y = Freq)
g <- g + geom_point()
plot(g)
koumoku <- as.vector(nns.bi.freq.rank.30$ngram.all)
koumoku
## [1] "it is" "i think" "in the" "high school"
## [5] "a lot" "want to" "i was" "lot of"
## [9] "there are" "have to" "in japan" "when i"
## [13] "for example" "of the" "think that" "the world"
## [17] "we can" "i have" "so i" "you can"
## [21] "if you" "to study" "to be" "and i"
## [25] "is not" "to play" "to do" "go to"
## [29] "playing sports" "is very"
nns.bi.freq.rank.30 <- head(nns.bi.freq.rank, 30)
g <- ggplot(nns.bi.freq.rank.30)
g <- g + aes(x = Rank, y = Freq)
g <- g + geom_point()
g <- g + geom_line()
g <- g + scale_x_continuous(breaks=seq(1:30), labels=koumoku)
plot(g)
nns.bi.freq.rank.30 <- head(nns.bi.freq.rank, 30)
g <- ggplot(nns.bi.freq.rank.30)
g <- g + aes(x = Rank, y = Freq)
g <- g + geom_point()
g <- g + geom_line()
#g <- g + scale_x_continuous(breaks=seq(1:30), labels=koumoku)
g <- g + scale_x_reverse(breaks=seq(1:30), labels=koumoku)
g <- g + coord_flip()
plot(g)
https://sugiura-ken.org/wiki/wiki.cgi/exp?page=MI
\[ MI = log2((共起頻度×コーパス総語数)/(片方の頻度×もう片方の頻度)) \]
my1gram.df <- function(){ # 独自プログラムとして
files <- list.files() # フォルダー内のすべてのファイルの一覧
word.all <- NULL # すべてのファイルのwordを入れる
for (f in files){ # すべてのファイルに対して
yomikomi <- readLines(f, warn=F)
tmp <- grep("\\*(JPN|NS)", yomikomi, value=T)
tmp1 <- gsub("\\*(JPN|NS)...:\t", "", tmp)
tmp2 <- gsub("[[:punct:]]", "", tmp1)
tmp3 <- tolower(tmp2)
tmp4 <- strsplit(tmp3, " ")
tmp5 <- unlist(tmp4)
tmp5 <- tmp5[tmp5 != ""]
word.all <- c(word.all, tmp5) # 単語リストを合わせていく
}
data.frame(word.all)
}
setwd("NICER_NNS")
nns.1gram.df <- my1gram.df()
nns.1gram.df.table <- table(nns.1gram.df) %>% as.data.frame()
arrange(nns.1gram.df.table, desc(Freq)) %>% head(30)
## word.all Freq
## 1 to 3634
## 2 i 3096
## 3 the 2994
## 4 and 2462
## 5 is 2427
## 6 in 2197
## 7 of 2011
## 8 a 1473
## 9 sports 1274
## 10 we 1266
## 11 it 1254
## 12 for 1215
## 13 that 1211
## 14 you 1194
## 15 have 1068
## 16 they 1026
## 17 are 1016
## 18 people 959
## 19 money 935
## 20 so 900
## 21 not 853
## 22 can 848
## 23 think 773
## 24 school 736
## 25 but 726
## 26 do 646
## 27 my 640
## 28 english 631
## 29 many 625
## 30 students 579
nns.bigram.df.table %>% filter(ngram.all == "playing sports")
## ngram.all Freq
## 1 playing sports 155
sum(nns.1gram.df.table$Freq)
## [1] 104932
nns.1gram.df.table %>% filter(word.all == "playing")
## word.all Freq
## 1 playing 330
nns.1gram.df.table %>% filter(word.all == "sports")
## word.all Freq
## 1 sports 1274
MI <- function(xy, x, y, N){
log2(xy*N/(x*y))
}
MI(155, 330, 1274, 104932)
## [1] 5.273748
nns.bigram.df.table %>% filter(ngram.all == "doing sports")
## ngram.all Freq
## 1 doing sports 55
nns.1gram.df.table %>% filter(word.all == "doing")
## word.all Freq
## 1 doing 108
MI(55, 108, 1274, 104932)
## [1] 5.390418
playing sportsは頻度は155で、MIスコアは5.3、doing sportsは頻度は55で、MIスコアは5.4であった。 学習者は、playing sportsを3倍ほど多く使うが、表現の結びつきの強さはそれほど違いはなかった。