Skip to content

Instantly share code, notes, and snippets.

@kawasaki2013
Last active November 19, 2018 13:16
Show Gist options
  • Select an option

  • Save kawasaki2013/0ae87d598bf2e679188d4bb1d294d1e3 to your computer and use it in GitHub Desktop.

Select an option

Save kawasaki2013/0ae87d598bf2e679188d4bb1d294d1e3 to your computer and use it in GitHub Desktop.
Rによるやさしいテキストマイニング[機械学習編] 第1章 

########## 第1章 ##########

# 1から4までの数値をxという変数に代入(c関数を使用)
x <- c(1, 2, 3, 4)
# sum関数を使って,変数xの中の数値の総和を計算
sum(x)
# 変数xに代入された4つの数値から2行2列の行列を作成し,変数matに代入
mat <- matrix(x, nrow = 2, ncol = 2, byrow = TRUE)
# 変数matの中身を確認
mat
# 行ラベルを付与
rownames(mat) <- c("R1", "R2")
# 列ラベルを付与
colnames(mat) <- c("C1", "C2")
# ラベルをつけた行列を確認
mat
# sum関数のヘルプを参照
help(sum)

RMeCabのインストール

# すでにRとMeCabの両方がインストールされている場合
install.packages("RMeCab", repos = "http://rmecab.jp/R")
# RMeCabパッケージの読み込み
library("RMeCab")
# 形態素解析
RMeCabC.result <- RMeCabC("形態素解析は文を単語単位に分割する技術です")
# リスト形式の解析結果をベクトル形式に変換
unlist(RMeCabC.result)

Rで英文を解析

# パッケージのインストール
install.packages(c("openNLP", "NLP"), dependencies = TRUE)
# パッケージの読み込み
library("openNLP")
library("NLP")
library("purrr")
library("magrittr")
# 分析するテキストデータを用意
text <- "R is a free software environment for statistical computing and graphics."
text <- as.String(text)
# 品詞情報を付与
sent_token_annotator <- Maxent_Sent_Token_Annotator()
word_token_annotator <- Maxent_Word_Token_Annotator()
text_an <- annotate(text, list(sent_token_annotator, word_token_annotator))
pos_tag_annotator <- Maxent_POS_Tag_Annotator()
text_an2 <- annotate(text, pos_tag_annotator, text_an)
# 品詞情報付与結果を整形
text_an3 <- subset(text_an2, type == "word")
tags <- map_chr(text_an3$features, extract(1))
sprintf("%s: %s", text[text_an3], tags)
# ローカルからパッケージのインストール(openNLPmodels.en_1.5-1.tar.gzを選択)
install.packages(pkgs = file.choose(), repos = NULL, type = "source")
# パッケージの読み込み
library("openNLP")
library("NLP")
library("purrr")
library("magrittr")
# 分析するテキストデータを用意
s <- "R is a free software environment for statistical computing and graphics."
s <- as.String(s)
# 構文解析
sent_token_annotator <- Maxent_Sent_Token_Annotator()
word_token_annotator <- Maxent_Word_Token_Annotator()
a2 <- annotate(s, list(sent_token_annotator, word_token_annotator))
parse_annotator <- Parse_Annotator()
p <- parse_annotator(s, a2)
ptexts <- map_chr(p$features, extract(1))
# 構文解析結果を表示
ptexts
# 構文解析結果を木構造で表示
ptrees <- lapply(ptexts, Tree_parse)
ptrees
# パッケージのインストール
install.packages(c("tm", "stringr"), dependencies = TRUE)
# パッケージの読み込み
library("tm")
library("stringr")
# テキストファイルからのデータ読み込み(Trump.txtを選択)
# 単語ごとに読み込み
Trump <- scan(file.choose(), what = "char", quiet = TRUE)
# 読み込んだデータを確認
head(Trump)
# すべての文字を小文字に変換
Trump.lower <- str_to_lower(Trump)
# 句読点の削除
Trump.cleaned <- removePunctuation(Trump.lower)
# 整形済みのデータを確認
head(Trump.cleaned)

Rで評判分析

# パッケージのインストール
install.packages(c("tidytext", "dplyr"), dependencies = TRUE)
# パッケージの読み込み
library("tidytext")
library("dplyr")
# ポジティブな単語を辞書から抽出
positive <- filter(get_sentiments("nrc"), sentiment == "positive")
# 分析データからポジティブな単語を抽出
Trump.positive <- Trump.cleaned %in% positive$word
Trump.positive.2 <- grep("TRUE", Trump.cleaned %in% positive$word)
# 分析データにおけるポジティブな単語の数を計算
length(Trump.positive.2)
# 分析データで使われているポジティブな単語を表示
Trump.cleaned[Trump.positive]
# ネガティブな単語を辞書から抽出
negative <- filter(get_sentiments("nrc"), sentiment == "negative")
# 分析データからネガティブな単語を抽出
Trump.negative <- Trump.cleaned %in% negative$word
Trump.negative.2 <- grep("TRUE", Trump.cleaned %in% negative$word)
# 分析データにおけるネガティブな単語の数を計算
length(Trump.negative.2)
Trump.cleaned[Trump.negative]
# パッケージのインストール
install.packages("devtools", dependencies = TRUE)
devtools::install_github("sfeuerriegel/SentimentAnalysis")
# パッケージの読み込み
library("SentimentAnalysis")
library("NLP")
# テキストファイルからのデータ読み込み(Trump.txtを選択)
# 文ごとに読み込み
Trump.2 <- scan(file.choose(), what = "char", sep = "\n", quiet = TRUE)
# 読み込んだデータを確認
head(Trump.2)
# 文単位の評判分析
sentiment <- analyzeSentiment(Trump.2)
convertToDirection(sentiment$SentimentGI)
# ポジティブな文,ネガティブな文,ニュートラルな文の集計
table(convertToDirection(sentiment$SentimentGI))
# パッケージのインストール
install.packages("textcat", dependencies = TRUE)
# パッケージの読み込み
library("textcat")
# プログラムの設定
prof <- TC_byte_profiles[names(TC_byte_profiles)]
# 多言語データの読み込み
dat <- c(
  "R is an open source programming language and software environment for statistical computing and graphics that is supported by the R Foundation for Statistical Computing.",
  "R ist eine freie Programmiersprache für statistische Berechnungen und Grafiken.",
  "R est un langage informatique dédié aux statistiques et à la science des données.",
"R es un entorno y lenguaje de programación con un enfoque al análisis estadístico."
)
# 読み込んだデータの言語を判定
textcat(dat, p = prof)
# 言語判定がうまくいかない例
textcat("Good morning!", p = prof)
# パッケージのインストール
install.packages("LSAfun", dependencies = TRUE)
# パッケージの読み込み
library("LSAfun")
# テキストファイルからのデータ読み込み(summarization.txtを選択)
text <- scan(file.choose(), what = "char", sep = "\n", quiet = TRUE)
# 読み込んだデータを確認
text
# 文書全体を最も的確に表す1文を抽出
genericSummary(text, k = 1)
# 文書全体を最も的確に表す2文を抽出
genericSummary(text, k = 2)
# 文書全体を最も的確に表す3文を抽出
genericSummary(text, k = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment