Abstract : ์ด๋ฒ์ฅ์์๋ Machine clustering์ ๋ํด์ ์๊ฐํ๋ค
์ด๋ฒ์ฅ์์๋ ์์ corpus๋ฅผ ์ฌ์ฉํด ๋ฌธ์๋ฅผ ๋ฌด๋ฆฌ ์ง๋ ๋ฐฉ๋ฒ์ ๋ํด ์ค๋ช ํ๋ค
์์ ๊ณต๊ฐ์ TextAnalysisWithR ํด๋๋ก ์ค์ ํ๋ค
setwd("E:/TextAnalysisWithR")
getwd()
## [1] "E:/TextAnalysisWithR"
R์์ ๋ฒกํฐ๊ฐ์ ๋ํ๋ผ๋ [1]์ด๋ ๊ฐ์ด ์ฝ์์ฐฝ ๊ฐ์ฅ ์์ ๋์ค๋๋ฐ ์ด ๊ฐ์ ๊ทธ ๋ฒกํฐ์์์ ์ฒซ๋ฒ์งธ ๊ฐ์ด๋ ๊ฒ์ ์๋ฏธํ๋ค. R๊ณผ ๋ค๋ฅธ ์ธ์ด์ ์ฐจ์ด๋ R์ ์ธ๋ฑ์ค๋ 1๋ก ์์ํ๋๋ฐ ๋ฐํด ๋ค๋ฅธ ์ธ์ด๋ 0์ผ๋ก ์์ํ๋ค. ์ด์ ๋ํด ์ข๋ ์์๋ณด์
nations=c(Joyce="Irish", Twain="American", Dickens="English")
nations=c(nations, Smith=NA)
is.na(nations) # is.na๋ na๊ฐ์ ํ์ธํ๋ ํจ์
## Joyce Twain Dickens Smith
## FALSE FALSE FALSE TRUE
NA๋ก ์ ๋ ฅ๋์ด์๋ Smith์ ๋ํด์๋ง TRUE์ ๊ฐ์ด ๋ํ๋๋ค. nchar(nations) Smith๊ฐ ๊ฐ์ง ๊ฐ์ NA๋ก 0์ด ๋์์ผ ํ๋๋ฐ 2๊ฐ ๋์จ๋ค. ์ฌ๊ธฐ์ ncharํจ์๋ NA์ ๋ํด์ 2๋ฅผ ๋ฐํํ๋ ๊ฒ์ ์์์ผ ํ๋ค.
input.dir="E:/TextAnalysisWithR/TextAnalysisWithR/data/XMLAuthorCorpus"
์ด๋ฒ์ฅ์์ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ค์ ์์ ์์น์ ์์นํด์๋ค. dirํจ์๋ฅผ ์ด์ฉํ์ฌ ์ ์ฅ๋์ด์๋ ํ์ผ๋ค์ ๋ถ๋ฌ์จ๋ค
files.v=dir(path=input.dir, pattern=".*xml")
์์ ๊ฒฐ๊ณผ๊ฐ์ ์ฒซ๋ฒ์งธ ํ์ผ์ ๋ณด๋ฉด ์๊ฐ๋ฏธ์์ธ๋ฐ ์ด๋ฒ์ฅ์์ ๋จ์ด์ ๋ฌด๋ฆฌ๋ฅผ ์ง์ํ ๋จ์ด๋ฅผ ๋น๊ตํ์ฌ ์ต๋ช ์ ์๊ฐ๋ฅผ ์ถ์ธกํ ์ ์๋ค. 10์ฅ์์ ๋ดค๋ฏ์ด XMLํ์ผ์ ๋ค๋ฃฐ๋๋ xmlTreeParseํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋๋ฐ ์ด๋ ๋ช๊ฐ์ง ์ต์ ์ ๊ฐ์ง๊ณ ์๋ค. ์ฒซ๋ฒ์งธ๋ ํ์ผ์ ์์น์ ๋ํ ์ต์ ์ด๋ค. ๋ฐ๋ผ์ ํ์ผ์ ์ ์ฅ๋ ์์น๋ฅผ ์์๋ณด๋ ๋ฐฉ๋ฒ์ ์์๋ณด์
library(XML) file.path(input.dir, files.v) # input.dir๊ณผ ํฉ์ณ์ ธ ์ ์ฅ๋ ์์น๋ฅผ ๋ฐํํ๋ค. xmlTreeParse(file.path(input.dir, files.v[1], useInternalNodes=TRUE))
์์ ๊ณต๊ฐ๋ด์ ๋ชจ๋ XMlํ์ผ์ ํ๋์ ๊ฐ์ฒด์ ์ ์ฅํ๊ธฐ ์ํด ๋จผ์ listํํ์ ๋น ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ for๋ฐ๋ณต๋ฌธ์ ์์ํ๋ค.
library(XML)
book.freqs.l=list()
for(i in 1:length(files.v)){
doc.object=xmlTreeParse(file.path(input.dir, files.v[i]),
useInternalNodes=TRUE)
}
XMLํ์ผ์ ๋ถ๋ฌ์์ผ๋ ํ์ผ๋ค์์ ๋จ์ด์ ๋น๋์๋ฅผ ์ถ์ถํด๋ด์ผ ํ๋ค. ์ด๊ฒ์ ์ฒซ ์ฅ์์ ํ๋๊ฒ๊ณผ ์ ์ฌํ๋ XML์ ์ฌ์ฉํ๋ค๋ ์ฐจ์ด๊ฐ ์์ด ์ฝ๊ฐ์ ์ฐจ์ด๊ฐ ์๋ค. ์ฌ๊ธฐ์๋ getTEIWordTableListํจ์๋ฅผ ์ฌ์ฉํ๊ณ ์ด ํจ์๋ XML๊ฐ์ฒด๋ง ํ์๋ก ํ๋ค. getTEIWordTableList=function(doc.object){ ํ์ํ ์ฝ๋ } ํ์์ผ๋ก ์ฌ์ฉํ๋ค. 1.์์ ์ ์คํํ๊ธฐ ์ํด ๋ชจ๋ XMLํ์ผ์ ํฉ์ณ์ผ ํ๋ค. ์ด๋ getNodesetํจ์๋ฅผ ์ฌ์ฉํ๋ค
paras=getNodeSet(doc.object, "/tei:TEI/tei:text/tei:body//tei:p",
c(tei="http://www.tei-c.org/ns/1.0"))
2.์ด๋ ๊ฒ ๋ถ๋ฌ์จ ๋ฌธ๋จ์ ๋ํด ํฉ์น๋ ์์ ์ ์ํํ๋ค. ๋ฐํ๊ฐ์ด listํํ์ ๋น์ทํ๋ฏ๋ก sapply์ xmlValue ํจ์๋ฅผ ์ด์ฉํ๋ค.
words=paste(sapply(paras, xmlValue), collapse=" ")
3.tolowerํจ์๋ฅผ ์ด์ฉํด ๋ชจ๋ ๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ฐ๊พธ๊ณ \W๋ฅผ ์ด์ฉํด ์ ๊ทํํ์์ ์ญ์ ํ๋ค.
words.lower=tolower(words)
words.l=strsplit(words.lower, "\\W")
word.v=unlist(words.l)
book.freqs.t=table(word.v[which(word.v!="")])
book.freqs.rel.t=100*(book.freqs.t/sum(book.freqs.t))
์์ ๊ณผ์ ์ ์ฌ์ฉ์ ํจ์๋ก ๋ง๋ ํจ์๋ ๋ค์๊ณผ ๊ฐ๋ค.
getTEIWordTableList=function(doc.object){
paras=getNodeSet(doc.object,
"/tei:TEI/tei:text/tei:body//tei:p",
c(tei="http://www.tei-c.org/ns/1.0"))
words=paste(sapply(paras, xmlValue), collapse=" ")
words.lower=tolower(words)
words.l=strsplit(words.lower, "\\W")
word.v=unlist(words.l)
book.freqs.t=table(word.v[which(word.v!="")])
book.freqs.rel.t=100*(book.freqs.t/sum(book.freqs.t))
return(book.freqs.rel.t)
}
๋ง๋ ์ฌ์ฉ์ํจ์๋ฅผ corpusFunctions.r ํ์ผ์ ์ ์ฅํ๋ค. ์ฌ์ฉ์ ์ ์ํจ์๋ฅผ ๋ถ๋ฌ์ for๋ฌธ์ ์ด์ฉํด ์ด์ ์ ์์ ํ ๋ ค๊ณ ํ๋ ์์ ์ ์ํํ๋ค.
source("E:/TextAnalysisWithR/TextAnalysisWithR/code/corpusFunctions.r")
library(XML)
book.freqs.l=list()
for(i in 1:length(files.v)){
doc.object=xmlTreeParse(file.path(input.dir, files.v[i]),
useInternalNodes=TRUE)
worddata=getTEIWordTableList(doc.object)
book.freqs.l[[files.v[i]]]=worddata
}
๋์จ ๊ฒฐ๊ณผ๊ฐ์ ๋ํด ํ์ธํ๋ค.
class(book.freqs.l)
## [1] "list"
names(book.freqs.l)
## [1] "anonymous.xml" "Carleton1.xml" "Carleton10.xml" "Carleton11.xml"
## [5] "Carleton12.xml" "Carleton13.xml" "Carleton14.xml" "Carleton2.xml"
## [9] "Carleton3.xml" "Carleton4.xml" "Carleton5.xml" "Carleton6.xml"
## [13] "Carleton7.xml" "Carleton8.xml" "Carleton9.xml" "Donovan1.xml"
## [17] "Donovan2.xml" "Driscoll1.xml" "Driscoll2.xml" "Driscoll3.xml"
## [21] "Edgeworth1.xml" "Jessop1.xml" "Jessop2.xml" "Jessop3.xml"
## [25] "Kyne1.xml" "Kyne2.xml" "LeFanu1.xml" "LeFanu2.xml"
## [29] "LeFanu3.xml" "LeFanu4.xml" "LeFanu5.xml" "LeFanu6.xml"
## [33] "LeFanu7.xml" "Lewis.xml" "McHenry1.xml" "McHenry2.xml"
## [37] "Norris1.xml" "Norris2.xml" "Norris3.xml" "Norris4.xml"
## [41] "Polidori1.xml" "Quigley1.xml" "Quigley2.xml"
str(book.freqs.l)
## List of 43
## $ anonymous.xml : table [1:8536(1d)] 0.000979 0.000979 0.000979 0.000979 0.003918 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:8536] "_i_" "02" "03" "05" ...
## $ Carleton1.xml : table [1:6148(1d)] 0.03282 0.00193 0.00193 0.00193 0.00193 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:6148] "_" "_abouchal_" "_acushla" "_acushla_" ...
## $ Carleton10.xml: table [1:8807(1d)] 0.00897 0.00069 0.00345 0.00138 0.00207 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:8807] "_" "_abouchal" "_amin" "_aqua" ...
## $ Carleton11.xml: table [1:6068(1d)] 0.03581 0.00565 0.00188 0.00188 0.00188 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:6068] "_" "_a" "_aegroto_" "_ahir" ...
## $ Carleton12.xml: table [1:8463(1d)] 0.007506 0.000834 0.000834 0.000834 0.000834 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:8463] "_" "_a" "_as" "_at" ...
## $ Carleton13.xml: table [1:11695(1d)] 0.001905 0.001429 0.000476 0.000476 0.000476 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:11695] "_" "_a" "_ad" "_all_" ...
## $ Carleton14.xml: table [1:9706(1d)] 0.00362 0.000603 0.000603 0.000603 0.000603 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:9706] "_" "_a" "_alanna_" "_always_" ...
## $ Carleton2.xml : table [1:2456(1d)] 0.01856 0.00928 0.01856 0.00928 0.00928 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:2456] "_" "_acushla_" "_black" "_cardin_" ...
## $ Carleton3.xml : table [1:8241(1d)] 0.004975 0.000829 0.000829 0.000829 0.000829 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:8241] "_" "__banaght" "_a" "_achora" ...
## $ Carleton4.xml : table [1:4942(1d)] 0.00171 0.00171 0.00171 0.00171 0.00171 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:4942] "_belles" "_cast" "_castaway_" "_dramatis" ...
## $ Carleton5.xml : table [1:2702(1d)] 0.00797 0.00797 0.00797 0.00797 0.00797 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:2702] "_ballydhas_" "_bouchaleens_" "_chevalier" "_coortin_" ...
## $ Carleton6.xml : table [1:2014(1d)] 2.9506 0.0115 0.0344 0.0115 0.0344 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:2014] "a" "abandon" "abandoned" "abbey" ...
## $ Carleton7.xml : table [1:8443(1d)] 0.006834 0.00205 0.00205 0.001367 0.000683 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:8443] "_" "_a" "_achora" "_achora_" ...
## $ Carleton8.xml : table [1:3773(1d)] 0.017 0.0034 0.0034 0.0034 0.0034 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:3773] "_" "_arraghid_" "_avich" "_avillish_" ...
## $ Carleton9.xml : table [1:8778(1d)] 0.003773 0.001509 0.000755 0.000755 0.000755 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:8778] "_" "_a" "_ab" "_adequate_" ...
## $ Donovan1.xml : table [1:5991(1d)] 0.00223 0.0067 0.00223 0.00223 2.24343 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:5991] "0" "1" "16" "47the" ...
## $ Donovan2.xml : table [1:5147(1d)] 0.00262 0.00262 0.00262 0.00262 0.00262 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:5147] "000" "1" "18" "216" ...
## $ Driscoll1.xml : table [1:12373(1d)] 0.003968 0.000661 0.001984 0.001323 0.000661 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:12373] "__________" "________________________charles" "__________a" "__________as" ...
## $ Driscoll2.xml : table [1:9409(1d)] 0.00819 0.00234 0.00234 0.00117 0.00117 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:9409] "0" "000" "10" "136" ...
## $ Driscoll3.xml : table [1:10629(1d)] 0.003997 0.001599 0.000799 0.000799 0.001599 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:10629] "000" "1" "10" "100" ...
## $ Edgeworth1.xml: table [1:1912(1d)] 0.0263 0.00877 0.00877 0.00877 0.00877 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:1912] "_can_" "_child_" "_edinburgh" "_extract" ...
## $ Jessop1.xml : table [1:4040(1d)] 0.00491 0.00983 0.00491 0.00491 0.00491 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:4040] "1188" "17" "1874" "1876" ...
## $ Jessop2.xml : table [1:4292(1d)] 0.00732 0.00366 0.00366 0.00366 0.00366 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:4292] "000" "1813" "1876" "1877" ...
## $ Jessop3.xml : table [1:2207(1d)] 0.0104 0.0104 2.2314 0.0415 0.0623 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:2207] "1897" "23d" "a" "abandoned" ...
## $ Kyne1.xml : table [1:9179(1d)] 0.00103 0.00103 0.00103 0.00103 0.00103 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:9179] "08" "102the" "118" "12th" ...
## $ Kyne2.xml : table [1:9996(1d)] 0.00171 0.000855 0.000855 0.00171 0.000855 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:9996] "1" "10" "12" "13" ...
## $ LeFanu1.xml : table [1:4045(1d)] 0.00354 0.01415 0.00354 0.00354 0.00354 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:4045] "_" "_a" "_all_" "_an" ...
## $ LeFanu2.xml : table [1:4085(1d)] 0.02313 0.00771 0.00386 0.00386 0.00386 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:4085] "_" "_a" "_alias_" "_all_" ...
## $ LeFanu3.xml : table [1:2588(1d)] 0.00701 0.00701 0.00701 0.00701 0.00701 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:2588] "_are_" "_can" "_dried" "_here_" ...
## $ LeFanu4.xml : table [1:5495(1d)] 0.00214 0.00214 0.00214 0.00214 0.00214 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:5495] "_" "_de" "_ma" "_vous" ...
## $ LeFanu5.xml : table [1:14656(1d)] 0.011292 0.000452 0.000452 0.000452 0.000452 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:14656] "_" "_a" "_a_" "_ad" ...
## $ LeFanu6.xml : table [1:11489(1d)] 0.010677 0.000593 0.007118 0.001186 0.000593 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:11489] "_" "_30th" "_a" "_a_lon" ...
## $ LeFanu7.xml : table [1:11909(1d)] 0.02825 0.00113 0.000565 0.000565 0.000565 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:11909] "_" "_a" "_abhor_" "_ably_" ...
## $ Lewis.xml : table [1:4668(1d)] 0.00279 0.02786 0.00279 0.00836 0.00279 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:4668] "_" "_i_" "04" "1" ...
## $ McHenry1.xml : table [1:7347(1d)] 0.00246 0.00123 0.00123 0.00123 0.00246 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:7347] "1" "1723" "1730" "1750" ...
## $ McHenry2.xml : table [1:6864(1d)] 0.00121 0.00242 0.00242 0.00121 0.00121 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:6864] "1734" "1754" "1755" "1776" ...
## $ Norris1.xml : table [1:9055(1d)] 0.000756 0.000756 2.276713 0.003024 0.000756 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:9055] "_" "1" "a" "abandoned" ...
## $ Norris2.xml : table [1:4388(1d)] 2.21192 0.00322 0.00322 0.00966 0.00322 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:4388] "a" "abandon" "abandonment" "able" ...
## $ Norris3.xml : table [1:8989(1d)] 0.000819 0.000819 0.000819 0.000819 0.000819 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:8989] "100" "15c" "1858" "1878" ...
## $ Norris4.xml : table [1:10783(1d)] 0.000584 2.366981 0.000584 0.000584 0.001169 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:10783] "2" "a" "aback" "abandon" ...
## $ Polidori1.xml : table [1:2926(1d)] 0.01555 0.01555 0.00778 0.0311 0.01555 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:2926] "__________" "______________" "____________________" "________________________________________________________________" ...
## $ Quigley1.xml : table [1:7155(1d)] 0.12579 0.00217 0.00217 0.00217 0.00217 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:7155] "000" "00970" "01082" "019" ...
## $ Quigley2.xml : table [1:8058(1d)] 0.00165 0.07423 0.01155 0.0033 0.00165 ...
## ..- attr(*, "dimnames")=List of 1
## .. ..$ : chr [1:8058] "00" "000" "1" "10" ...
์๋ก ๋ค๋ฅธ ๋๊ฐ์ ํ ์คํธ๋ฅผ ๊ตฌ๋ถํ๋ ๊ฐ์ฅ ํจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ์ ์์ฃผ ๋ฑ์ฅํ๋ ํน์ ๋น๋์ ์ ๋น๊ตํ๋ ๊ฒ์ด๋ค.(์๋ฅผ๋ค๋ฉด, the, of, and, to ๋ฑ์ด ์๋ค) ์ด๋ฅผ ์ธก์ ํ๋ ๋ฐฉ๋ฒ์ ์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ์ ๊ด๋ จ์ด ์๋ค. ์ ํด๋ฆฌ๋๊ฑฐ๋ฆฌ์ ํผํ๊ณ ๋ผ์ค๊ฑฐ๋ฆฌ๋ฅผ ์ด์ฉํด ๊ฐ๊ฐ์ ๋ถ๊ณผ ๋ค๋ฅธ ๋ถ๋ค๊ฐ์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ ์ ์๋ค. ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ๊น์ธ์๋ก ๋น์ทํ ํน์ง์ด ์๋ค๊ณ ํ ์ ์๋ค. ์ด ๋ฐฉ๋ฒ์ ์๊ธฐ์ํด ์๋ฅผ ๋ค์ด๋ณด์ a,b,c ๋ผ๋ 3๊ฐ์ ๋ถ์ด ์๊ณ f1,f2์ ํน์ง์ด ์์ผ๋ฉฐ ์ธก์ ๊ฐ์ ๊ฐ๊ฐ์ ๋ถ์์์ ๋ฐฑ๋ถ์จ์ด๋ค
a=c(10,5)
b=c(11,6)
c=c(4,13)
my.m=rbind(a,b,c)
colnames(my.m)=c("f1","f2")
์ ๋ค๊ฐ์ ๊ฑฐ๋ฆฌ ๊ณ์ฐ์ ์ํด distํจ์๋ฅผ ์ด์ฉํ๋ค.
dist(my.m)
## a b
## b 1.414214
## c 10.000000 9.899495
๋จ์ ๊ฑฐ๋ฆฌ๋ง ๊ฐ์ง๊ณ a,b๋ ๊ฐ๊น์ด ๊ด๊ณ์ด๊ณ c๋ ๋ค๋ฅด๋ค๋ ๊ฒ์ ์ ์ ์๋ค. ํน์ง์ ์๊ฐ ๋ง์์ง๋ค๊ณ ํด๋ ๊ณ์ฐ์ ๋ณต์กํด์ง์ง๋ง ๊ธฐ๋ณธ ๋ฐฉ๋ฒ์ ๋ณํ์ง ์๋๋ค.(์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ๋ฅผ ์ด์ฉ) book.freqs.l ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฏ๋ก ์ด๋ฅผ data matrixํํ๋ก ๋ณํ์์ผ ๋ถ์์ ํ๋ค.
๋ณํํ๊ธฐ ์ํด mapply๋ฅผ ์ฌ์ฉํ๋ค. ๊ฒฐ๊ณผ๊ฐ์ผ๋ก listํํ๋ก๋ freqs.l๊ฐ์ฒด๋ฅผ ์ป๋๋ฐ ์ด๋ book.freqs.l๊ณผ ๋น์ทํ๋ freqs.l์ ๊ฐ ๋ฆฌ์คํธ๋ด์์๋ data.frameํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
freqs.l=mapply(data.frame,
ID=seq_along(book.freqs.l),
book.freqs.l,
SIMPLIFY=FALSE,
MoreArgs=list(stringsAsFactors=FALSE))
๋ ๊ฐ์ฒด์ ์ฐจ์ด๋ฅผ ํ์ธํ ์ ์๋ค.
class(freqs.l[[1]])
## [1] "data.frame"
class(book.freqs.l[[1]])
## [1] "table"
์ฃผ์ํด์ผ ํ ๊ฒ์ ์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๊ธฐ์ํด์๋ ๊ฐ์ฒด๋ฅผ matrixํํ๋ก ๋ฐ๊ฟ์ผ ํ๋ค.
freqs.df=do.call(rbind, freqs.l)
dim(freqs.df)
## [1] 300447 3
result=xtabs(Freq~ID+Var1, data=freqs.df)
dim(result)
## [1] 43 53233
colnames(result)[500:600]
## [1] "attempt" "attempted" "attend"
## [4] "attendant" "attended" "attention"
## [7] "attentions" "attentive" "attested"
## [10] "attire" "attitude" "attorney"
## [13] "attorneys" "attrition" "au"
## [16] "auburn" "auction" "audible"
## [19] "audibly" "auditor" "auditory"
## [22] "aught" "august" "auld"
## [25] "aunt" "authentic" "authoritatively"
## [28] "authority" "authorize" "automatically"
## [31] "automobile" "automobiles" "autumn"
## [34] "avail" "available" "avenue"
## [37] "average" "averred" "avoid"
## [40] "avoided" "avoiding" "avowal"
## [43] "avuncular" "await" "awaited"
## [46] "awaiting" "awake" "awakening"
## [49] "aware" "away" "awe"
## [52] "awesome" "awful" "awfully"
## [55] "awkward" "axe" "axes"
## [58] "aye" "azaleas" "b"
## [61] "baby" "back" "backbone"
## [64] "backed" "background" "backs"
## [67] "backward" "bad" "bade"
## [70] "badge" "badly" "baffled"
## [73] "baffling" "bag" "baggage"
## [76] "bah" "bait" "balance"
## [79] "balanced" "bale" "baleful"
## [82] "ball" "ballad" "ballads"
## [85] "bally" "band" "bandaged"
## [88] "bandmaster" "bandsaw" "bandsaws"
## [91] "bang" "banged" "banishment"
## [94] "banisters" "bank" "banker"
## [97] "bankers" "bankrupt" "bankruptcy"
## [100] "banks" "banter"
class(result)
## [1] "xtabs" "table"
clusting์ ํ๊ธฐ์ํด result๊ฐ์ matrix๊ฐ์ฒด๋ก ๋ณํ์์ผ์ค์ผ ํ๋ค.(applyํจ์ ์ด์ฉ)
final.m=apply(result, 2, as.numeric)
clustering์ ํ๊ธฐ์ ์ ๋น๋์ ๋ฐ๋ผ ๋ฐ์ดํฐ๋ฅผ ์ ๋ณํด๋๋๊ฒ ์ ์ฉํ๋ค ์ ๋ณํ๋ ๋ฐฉ๋ฒ์๋ ์ฌ๋ฌ๊ฐ์ง๊ฐ ์๋๋ฐ ๊ทธ์ค ํ๊ฐ์ง๋ ๋น๋์๊ฐ ๋์ 100๊ฐ์ง๋ง์ ๊ฐ์ ธ์ค๋ ๊ฒ์ด๋ค ๋ํ ๋์์ ๋ํ ๊ฒฝ๊ณ๋ฅผ ๊ตฌํ๋๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ธ๋ฐ ์ ์ฒด์์ ๊ทธ ๋จ์ด๊ฐ ๋ชํ๋ก ์ฐจ์งํ๋์ง๋ฅผ ๋ฐ์ง๊ณ .25๊ฐ์ ๋๋ ๊ฐ๋ง์ ๊ฐ์ง๊ณ ์ค๋๊ฒ์ ์ ํธํ๋ค. ์ด๋ฅผ ์ํด applyํจ์๋ฅผ ์คํํ๋ค
smaller.m=final.m[,apply(final.m, 2, mean)>=.25]
๋ ๊ฐ์ฒด๋ฅผ ๋น๊ตํด๋ณธ๋ค
dim(final.m)
## [1] 43 53233
dim(smaller.m)
## [1] 43 53
distํจ์(์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ ๊ตฌํ๊ธฐ)์ hclust(๋ฐ์ดํฐ๊ฐ ๊ฑฐ๋ฆฌ๋ฅผ ์ด์ฉํด ๋ฌด๋ฆฌ์ง๊ธฐ)๋ฅผ ์ด์ฉํ๋ค 1.๊ฑฐ๋ฆฌ๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค
dm=dist(smaller.m)
cluster=hclust(dm)
cluster$labels=names(book.freqs.l)
plot(cluster)
anonymous.xml์ kyne1.xml๊ณผ kyne2.xml์ฌ์ด์ ์๋๊ฑธ๋ก ๋ด์ ์ด ์ ์๋ฅผ Peter B.Kyne์ผ ๊ฐ๋ฅ์ฑ์ด ๋๋ค.