-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
187 lines (134 loc) · 3.6 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
---
output:
github_document:
html_preview: false
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<!-- badges: start -->
[![Travis build status](https://travis-ci.org/news-r/textanalysis.svg?branch=master)](https://travis-ci.org/news-r/textanalysis)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
<!-- badges: end -->
# textanalysis
Text Analysis in R via Julia.
<img src="./man/figures/logo.png" height="200" align="right" />
## Installation
Being a wrapper to a [Julia](https://julialang.org/) package, textanalysis requires the latter to be installed.
```{r, eval=FALSE}
# install.packages("remotes")
remotes::install_github("news-r/textanalysis") # github
```
## Setup
You _must_ run `init_textanalysis` at the begining of every session, you will otherwise encounter errors and be prompted to do so.
```{r}
library(textanalysis) # load the package
init_textanalysis() # initialise
```
Some funtions depend on the development version of the Julia package, to install it run:
```r
install_textanalysis(version = "latest")
```
## Basic Examples
```{r}
# build document
str <- paste(
"They <span>write</span>, it writes too!!!",
"This is another sentence.",
"More stuff in this document."
)
doc <- string_document(str)
# basic cleanup
prepare(doc)
get_text(doc)
# stem
stem_words(doc)
get_text(doc)
# corpus
doc2 <- token_document("Hey write another document.")
# combine
corpus <- corpus(doc, doc2)
# standardize
standardize(corpus, "token_document")
# prepare corpus
prepare(corpus, strip_html_tags = FALSE)
get_text(corpus)
# lexicon + lexical stats
(lexicon <- lexicon(corpus))
lexical_frequency(corpus, "document")
# inverse index
inverse_index(corpus)
# dtm
m <- document_term_matrix(corpus)
# create func to easily add lexicon
bind_lexicon <- function(data){
data %>%
as.data.frame() %>%
dplyr::bind_cols(
lexicon %>%
dplyr::select(-n),
.
)
}
# term-frequency
tf(m) %>% bind_lexicon()
# tf-idf
tf_idf(m) %>% bind_lexicon()
# bm-25
# https://opensourceconnections.com/blog/2015/10/16/bm25-the-next-generation-of-lucene-relevation/
bm_25(m) %>% bind_lexicon()
# sentiment
sentiment(corpus)
# summarise in 2 sentences
summarize(string_document(str), ns = 2L)
```
## Latent Dirichlet Allocation
fit LDA on the [gensimr](https://gensimr.news-r.org) data.
```{r}
set_seed(42L)
data("corpus", package = "gensimr")
documents <- to_documents(corpus) # convert vector to documents
crps <- corpus(documents)
dtm <- document_term_matrix(crps)
# 2 topics
# 1K iterations
lda_data <- lda(dtm, 2L, 1000L)
# classification
lda_data$ntopics_ndocs
mat <- dtm_matrix(dtm, "dense")
tfidf <- tf_idf(mat)
km <- kmeans(tfidf, centers = 2)
```
## Hash trick
```{r}
hash_func <- create_hash_function(10L)
hash("a", hash_func)
hash(doc) # doc has built-in has
```
## Naive Bayes Classifier
```{r}
classes <- factor(c("financial", "legal"))
model <- init_naive_classifer(classes)
train <- tibble::tibble(
text = c("this is financial doc", "this is legal doc"),
labels = factor(c("financial", "legal"))
)
train_naive_classifier(model, train, text, labels)
test <- tibble::tibble(
text = "this should be predicted as a legal document"
)
predict_class(model, test, text)
```
## Co-occurence Matrix
Plot method uses [echarts4r](https://echarts4r.john-coene.com)
```r
matrix <- coom(crps)
plot(matrix)
```
![](man/figures/coom.png)