2007-12-05 23:49:42
新納浩幸『Rで学ぶクラスタ解析』(3200円+税/オーム社)[amazon.co.jp, 紀伊國屋書店]を買ったので、Rで文字列を扱う練習をしてみようと思った。ダンセイニの『ペガーナの神々』のファイルを読込んで、Sishを含む行を抜きだしてみる。表示させるときには、Sishを#で挟んでみたりする。
> text <- scan(file="pegana.txt",sep="\n", what="") > for(i in 1:length(text)) if(any(grep("Sish",text[i]))){ + print(gsub("Sish","#Sish#",text[i])) + }とやると、下のような結果が表示された。
[1] "Concerning #Sish#" [1] "There are in Pegana Mung and #Sish# and Kib, and the maker of all" [1] "Time is the hound of #Sish#." [1] "At #Sish#'s bidding do the hours run before him as he goeth upon" [1] "Never hath #Sish# stepped backward nor ever hath he tarried; never" [1] "Before #Sish# is Kib, and behind him goeth Mung." [1] "Very pleasant are all things before the face of #Sish#, but behind" [1] "And #Sish# goeth ceaselessly upon his way." [1] "neither did #Sish# assail it with his hours." [1] "on the slopes of it #Sish# rested among the flowers when #Sish# was" [1] "Thence #Sish# went forth into the world to destroy its cities, and" [1] "And Time, which is the hound of #Sish#, devoured all things; and" [1] "#Sish# sent up the ivy and fostered weeds, and dust fell from the" [1] "hand of #Sish# and covered stately things. Only the valley where" [1] "#Sish# rested when he and Time were young did #Sish# not provoke his" [1] "beyond mountains, a garden exceeding fair with flowers; but #Sish#" [1] "And Mung said: \"With the gods it is always Now. And before #Sish#" [1] "And #Sish# throughout the Worlds hurled Time away, and slew the" [1] "to assail all things. And #Sish# cast a whiteness over the hairs of" [1] "And when #Sish# became a god less durable to Yun-Ilara than ever" [1] "Kib giveth but toil and trouble; and #Sish#, he sendeth regrets with" [1] "save One, Priest of Kib, Priest of #Sish#, and Priest of Mung," [1] "Priest of Kib, Priest of #Sish#, and Priest of Mung, Teller of the" [1] "may know.' And he showed me the paths of #Sish# stretching far down" [1] "walked with Dorozhand adown the paths of #Sish# into the future" [1] "that #Sish# dealt not too harshly with the hours, and that all the" 15 件の警告がありました (警告を見るには warnings() を使って下さい)Sishの位置が揃わないと見づらいのだ。位置を数えて、足りない文字分を左側に追加するやり方が判らなかった。一定の長さに切り詰める方法は判ったのだが。しかし、Rでkwicはあまり意味がないかも。もっと単語を集計してクラスター解析とかを練習した方がいいだろうか。