R !!!quanteda を使った例 {{outline}} ---- !サンプル: [学習者コーパスNICEST 1.0|http://sgr.gsid.nagoya-u.ac.jp/wordpress/?page_id=1159]の学習者データ8つ *フォルダー内に、テキストデータのみのテキストファイルが複数入っている *作業ディレクトリーをそのフォルダーに設定してある状態で {{pre > getwd() [1] "C:/Users/ /Documents/NICEST-samples" > list.files() [1] "JAN0001_P1B.txt" "JAN0001_P2B.txt" "JAN0001_P3B.txt" "JAN0001_P4B.txt" "JAN0001_P5B.txt" "JAN0001_P6B.txt" [7] "JAN0001_P7B.txt" "JAN0001_P8B.txt" }} !readtextパッケージの <> を使ってテキストを読み込む {{pre nicest.tmp <- readtext("*.txt") > nicest.tmp readtext object consisting of 8 documents and 0 docvars. # Description: df[,2] [8 x 2] doc_id text 1 JAN0001_P1B.txt "\"Some peopl\"..." 2 JAN0001_P2B.txt "\"You may th\"..." 3 JAN0001_P3B.txt "\"Compared w\"..." 4 JAN0001_P4B.txt "\"You may ha\"..." 5 JAN0001_P5B.txt "\"Elderly pe\"..." 6 JAN0001_P6B.txt "\"Group tour\"..." # ... with 2 more rows }} !コーパスデータ化する <> {{pre nicestJ1 <- corpus(nicest.tmp) nicestJ1 Corpus consisting of 8 documents and 0 docvars. }} *文書変数の追加修正 docvars() *文書属性の追加修正 metadoc() こちらは分析対象とならない備考欄的情報 *reference https://github.com/koheiw/workshop-IJTA/blob/master/documents/corpus.md https://quanteda.io/articles/pkgdown/examples/quickstart_ja.html !コーパスデータから条件で絞り込んで、サブ・セットを作成 <> *絞り込みの例 ** 見出し > 数字 ** 見出し == "文字列" !概要を見る <> > summary(nicestJ1) Corpus consisting of 8 documents: Text Types Tokens Sentences JAN0001_P1B.txt 116 214 12 JAN0001_P2B.txt 138 268 17 JAN0001_P3B.txt 97 169 11 JAN0001_P4B.txt 68 99 8 JAN0001_P5B.txt 120 262 16 JAN0001_P6B.txt 114 224 13 JAN0001_P7B.txt 121 268 18 JAN0001_P8B.txt 71 108 8 Source: C:/Users/ /Documents/NICEST-samples/* on x86-64 by sugiura Created: Thu Nov 14 15:26:21 2019 Notes: !文書変数(属性情報)を追加する docvars(コーパス, 文書変数) {{pre > summary(nicestJAN.corpus2) Corpus consisting of 10 documents: Text Types Tokens Sentences JAN0001_P1B.txt 116 214 12 JAN0001_P2B.txt 138 268 17 JAN0001_P3B.txt 97 169 11 JAN0001_P4B.txt 68 99 8 JAN0001_P5B.txt 120 262 16 JAN0001_P6B.txt 114 224 13 JAN0001_P7B.txt 121 268 18 JAN0001_P8B.txt 71 108 8 JAN0002_P1A.txt 98 170 15 JAN0002_P2A.txt 117 216 19 > docvars(nicestJAN.corpus2, "lang") <- "jan" > summary(nicestJAN.corpus2) Corpus consisting of 10 documents: Text Types Tokens Sentences lang JAN0001_P1B.txt 116 214 12 jan JAN0001_P2B.txt 138 268 17 jan JAN0001_P3B.txt 97 169 11 jan JAN0001_P4B.txt 68 99 8 jan JAN0001_P5B.txt 120 262 16 jan JAN0001_P6B.txt 114 224 13 jan JAN0001_P7B.txt 121 268 18 jan JAN0001_P8B.txt 71 108 8 jan JAN0002_P1A.txt 98 170 15 jan JAN0002_P2A.txt 117 216 19 jan }} !中身を見る <> *このままだと、すべて出力される *特定のデータを見る場合は、何番目のデータを見るかを [番号] で指定する。 {{pre > texts(nicestJ1)[3] JAN0001_P3B.txt "Compared with past, young people nowadays do not give enough time to (以下略) }} !KWIC検索する <> {{pre > kwic(nicestJ1, pattern="however") [JAN0001_P1B.txt, 13] not important for human, | however | , who make todays life [JAN0001_P2B.txt, 31] compared with young people, | however | , I recognize that is [JAN0001_P7B.txt, 71] misunderstanding about that area. | However | , if you only know }} !トークン化する <> *結果は、各文書を要素とするリスト形式で保存される *句読点を除くオプション <> *数字を除くオプション <> *stopwordsを除くオプション < tokens(nicestJ1, remove_numbers=T, remove_punct=T) tokens from 8 documents. JAN0001_P1B.txt : [1] "Some" "people" "say" "that" "specialized" "knowledge" "is" "not" "important" "for" [11] "human" "however" "who" "make" "todays" "life" "such" "a" "convenience" "are" [21] "always" "a" "few" "number" "of" "genius" "with" "very" "specific" "knowledges" [31] "To" "consider" "this" "it" "can" "be" "said" "that" "to" "specialized" [41] "in" "one" "specific" "subject" "is" "better" "than" "to" "get" "bload" [51] "knowledge" "of" "many" "academic" "subjects" "There" "is" "some" "more" "reasons" (以下略) }} *token化したものをすべて小文字にする tokens_tolower() *token化したものをstem化する tokens_wordstem() !文書行列の作成 document-feature matrix (DFM) <> *tmより新しいので、こっちのほうが良い(Welbers et al 2017) *オプション **ステム化 <> **句読点の削除 <> *対象は、トークン化したオブジェクトだが、 **トークン化してなくても自動でトークン化してくれる {{pre > dfm(nicestJ1) > nicestJ1.dfm <- dfm(nicestJ1, stem=T, remove_punct=T) > nicestJ1.dfm Document-feature matrix of: 8 documents, 342 features (72.8% sparse). }} *機能語などの頻出単語(stopwords)を除くオプション remove = stopwords("english") *文書をグループ化することもできる groups = "見出し" !文書行列の閲覧 <> !単語頻度一覧表 <> *デフォルトは、上位10語 *オプションで数字をつけるとそこまで {{pre > topfeatures(nicestJ1.dfm) to you not in is of it the can and 60 53 33 30 27 27 25 25 23 22 > topfeatures(nicestJ1.dfm, 20) to you not in is of it the can and i this they peopl have that do think use are 60 53 33 30 27 27 25 25 23 22 21 19 18 17 17 16 16 15 14 13 }} !ワードクラウドの作成 <> {{pre > textplot_wordcloud(nicestJ1.dfm) }} {{ref_image nicestJ1.png}} !いくつかの単語をまとめてグループにし、そのグループに該当するものの頻度を調べる。 *例えば、接続語句のリストのグループを複数作っておいて、それぞれのグループに属する接続語句の頻度をかぞえる。 *グループを「辞書」と呼ぶ。コマンド名は dictionary() {{pre connectives <- dictionary(list(additive = c("moreover", "furthermore", "and"), adversative = c("however","but","conversely"), resultative = c("therefore", "thus", "so"))) connectives.list <- dfm(コーパスデータ, dictionary = connectives) }} {{pre Document-feature matrix of: 10 documents, 3 features (10.0% sparse). 10 x 3 sparse Matrix of class "dfm" features docs additive adversative resultative JAN0001_P1B.txt 1 1 2 JAN0001_P2B.txt 4 4 3 JAN0001_P3B.txt 3 1 1 JAN0001_P4B.txt 2 1 1 JAN0001_P5B.txt 4 1 3 JAN0001_P6B.txt 2 2 2 JAN0001_P7B.txt 5 3 0 JAN0001_P8B.txt 1 1 0 JAN0002_P1A.txt 3 0 1 JAN0002_P2A.txt 4 3 2 }} !文書に特徴的な単語を選び出す keyness ---- *Reference https://quanteda.io/articles/pkgdown/quickstart_ja.html http://i.amcat.nl/lda/1_textanalysis.html