-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
278 lines (278 loc) · 15.2 KB
/
.Rhistory
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
install.packages(c("stringr", "stringi", "tm", "dplyr", "SnowballC", "dendextend", "ca", "factoextra", "wordcloud", "sentimentr", "eply"))
# Zadanie 1
moja_pg_funkcja_wczytanie_danych_z_wielu_plikow <- function(nazwa_folderu = ""){
# Dodanie / przed sciezka/nazwa folderu
sciezka <- paste0("/",nazwa_folderu)
# Sklejenie sciezki przestrzeni roboczej z nazwa folderu
sciezka_all <- paste0(getwd(), sciezka)
# Odczyt danych i zmiana kodowania
# windows-1250, UTF-8 itd
tekst_zrodlo <- DirSource(sciezka_all, encoding = "UTF-8", mode = "text")
# Wrzucenie danych do korpusu
tekst_korpus <- VCorpus(tekst_zrodlo)
}
install.packages("tm", "sentimentr", "ggplot2", "dplyr", "proxy")
library("tm")
library("sentimentr")
library("ggplot2")
library("dplyr")
library("proxy")
install.packages("proxy")
library("proxy")
getwd()
setwd("C:/Users/Anita/Documents/analiza danych i text mining/egzamin/egzamin")
getwd()
hrabia_wczytany_do_korpusu <- moja_pg_funkcja_wczytanie_danych_z_wielu_plikow("hrabia10")
hrabia_wczytany_do_korpusu
hrabia_wczytany_do_korpusu[[10]]$content
moja_pg_funkcja_czysc_korpus <- function(korpus_do_czyszczenia){
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "”", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "“", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "-", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "'", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "’", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "‘", "")))
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) str_replace_all(x, "\n", "")))
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) str_replace_all(x, "\r", "")))
# Zamiane duzych znakow na male
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(tolower))
# Usuniecie znakow przestankowych
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removePunctuation)
# Usuniecie numerow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removeNumbers)
# Usuniecie stopwords
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removeWords, stopwords("en"))
# Usuniecie bialych znakow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, stripWhitespace)
# Usuwanie spacji z poczatku i konca stringow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) trimws(x)))
# Usuwanie pustych linii
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) stri_remove_empty(x, na_empty = FALSE)))
}
hrabia_oczyszczony_korpus <- hrabia_wczytany_do_korpusu(hrabia_wczytany_do_korpusu)
hrabia_oczyszczony_korpus <- moja_pg_funkcja_czysc_korpus(hrabia_wczytany_do_korpusu)
install.packages("stringr")
library("stringr")
moja_pg_funkcja_czysc_korpus <- function(korpus_do_czyszczenia){
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "”", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "“", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "-", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "'", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "’", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "‘", "")))
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) str_replace_all(x, "\n", "")))
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) str_replace_all(x, "\r", "")))
# Zamiane duzych znakow na male
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(tolower))
# Usuniecie znakow przestankowych
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removePunctuation)
# Usuniecie numerow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removeNumbers)
# Usuniecie stopwords
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removeWords, stopwords("en"))
# Usuniecie bialych znakow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, stripWhitespace)
# Usuwanie spacji z poczatku i konca stringow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) trimws(x)))
# Usuwanie pustych linii
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) stri_remove_empty(x, na_empty = FALSE)))
}
hrabia_oczyszczony_korpus <- moja_pg_funkcja_czysc_korpus(hrabia_wczytany_do_korpusu)
install.packages("stringi")
install.packages("stringi")
library("stringi")
moja_pg_funkcja_czysc_korpus <- function(korpus_do_czyszczenia){
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "”", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "“", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "-", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "'", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "’", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "‘", "")))
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) str_replace_all(x, "\n", "")))
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) str_replace_all(x, "\r", "")))
# Zamiane duzych znakow na male
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(tolower))
# Usuniecie znakow przestankowych
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removePunctuation)
# Usuniecie numerow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removeNumbers)
# Usuniecie stopwords
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removeWords, stopwords("en"))
# Usuniecie bialych znakow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, stripWhitespace)
# Usuwanie spacji z poczatku i konca stringow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) trimws(x)))
# Usuwanie pustych linii
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) stri_remove_empty(x, na_empty = FALSE)))
}
hrabia_oczyszczony_korpus <- moja_pg_funkcja_czysc_korpus(hrabia_wczytany_do_korpusu)
hrabia_oczyszczony_korpus[[10]]$content
moja_pg_funkcja_ogarnij_stringi_w_korpusie<- function(korpus_oczyszczony, ilosc_datasetow){
for (i in 1:ilosc_datasetow){
wszystkie_slowa <- unlist(strsplit(korpus_oczyszczony[[i]]$content, " "))
korpus_oczyszczony[[i]]$content <- paste(wszystkie_slowa, collapse=' ')
}
return(korpus_oczyszczony)
}
hrabia_korpus_uporzadkowany <- moja_pg_funkcja_ogarnij_stringi_w_korpusie(hrabia_oczyszczony_korpus, 10)
hrabia_korpus_uporzadkowany
hrabia_korpus_uporzadkowany[[10]]$content
# Zadanie 3
moja_pg_funkcja_wyodrebnij_zdania<- function(vector){
return (get_sentences(vector))
}
View(hrabia_korpus_uporzadkowany)
hrabia_wyodrebnione_zdanie <- moja_pg_funkcja_wyodrebnij_zdania(hrabia_korpus_uporzadkowany["rozdz1.txt"]$content)
View(hrabia_korpus_uporzadkowany)
rozdzial_1 <- hrabia_korpus_uporzadkowany[[1]]$content
rozdzial_1
hrabia_wyodrebnione_zdanie <- moja_pg_funkcja_wyodrebnij_zdania(rozdzial_1)
hrabia_wyodrebnione_zdanie
rozdzial_1_characters <- as.character(rozdzial_1)
hrabia_wyodrebnione_zdanie <- moja_pg_funkcja_wyodrebnij_zdania(rozdzial_1_characters)
hrabia_wyodrebnione_zdanie
# ------------------------------------------------------------------------------------------------------------------
# Funkcja rysujaca magiczny wykres analizy sentymentu
# --- Przyjmuje data.frame z analiza sentymentu
# ------------------------------------------------------------------------------------------------------------------
f_rysuj_wykres_analizy_sentymentu <- function(sentiments){
moje_kolory <- c("deeppink", "gold", "green3")
sentiments %>%
mutate(kolor = ifelse(sentiment == 0, "Neutralna", ifelse(sentiment < 0, "Negatywna", "Pozytywna"))) %>%
ggplot(aes(element_id, sentiment, fill = kolor, color = kolor)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = moje_kolory) +
scale_color_manual(values = moje_kolory) +
labs(x = "Opinie", y = "Ocena sentymentu") +
theme_minimal()
}
kawa <- read.csv2("coffee_tweets.csv")
kawa <- read.csv2("coffee_tweets.csv")
coffee_tweets <- kawa$text
coffee_tweets_vector <- as.character(coffee_tweets)
coffee_tweets_vector
# ------------------------------------------------------------------------------------------------------------------
# Funkcja wyodrebnia zdania z tekstu
# --- Przyjmuje wektor
# --- Zwraca liste z wyodrebnionymi zdaniami
# ------------------------------------------------------------------------------------------------------------------
f_wyodrebnij_zdania<- function(vector){
return (get_sentences(vector))
}
f_wyodrebnij_zdania(coffee_tweets_vector)
hrabia_wyodrebnione_zdanie
moja_pg_funkcja_czysc_korpus <- function(korpus_do_czyszczenia){
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "”", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "“", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "-", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "'", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "’", "")))
#korpus <- tm_map(korpus, content_transformer(function(x) str_replace_all(x, "‘", "")))
#korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) str_replace_all(x, "\n", "")))
#korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) str_replace_all(x, "\r", "")))
# Zamiane duzych znakow na male
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(tolower))
# Usuniecie znakow przestankowych
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removePunctuation)
# Usuniecie numerow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removeNumbers)
# Usuniecie stopwords
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, removeWords, stopwords("en"))
# Usuniecie bialych znakow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, stripWhitespace)
# Usuwanie spacji z poczatku i konca stringow
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) trimws(x)))
# Usuwanie pustych linii
korpus_do_czyszczenia <- tm_map(korpus_do_czyszczenia, content_transformer(function(x) stri_remove_empty(x, na_empty = FALSE)))
}
hrabia_wczytany_do_korpusu <- moja_pg_funkcja_wczytanie_danych_z_wielu_plikow("hrabia10")
hrabia_wczytany_do_korpusu
hrabia_wczytany_do_korpusu[[10]]$content
hrabia_korpus_uporzadkowany <- moja_pg_funkcja_ogarnij_stringi_w_korpusie(hrabia_oczyszczony_korpus, 10)
hrabia_korpus_uporzadkowany[[10]]$content
rozdzial_1 <- hrabia_korpus_uporzadkowany[[1]]$content
rozdzial_1_characters <- as.character(rozdzial_1)
hrabia_wyodrebnione_zdanie <- moja_pg_funkcja_wyodrebnij_zdania(rozdzial_1_characters)
hrabia_wyodrebnione_zdanie
moja_pg_funkcja_analiza_sentymentu <- function(vector){
#można wybrać inne slowniki sentymentu
return (sentiment(vector, polarity_dt = lexicon::hash_sentiment_huliu))
}
wynik_analizy_sentymentu <- moja_pg_funkcja_analiza_sentymentu(rozdzial_1)
wynik_analizy_sentymentu
kawa <- read.csv2("coffee_tweets.csv")
coffee_tweets <- kawa$text
coffee_tweets_vector <- as.character(coffee_tweets)
f_wyodrebnij_zdania(coffee_tweets_vector)
coffee_result <- f_analiza_sentymentu(coffee_tweets_vector)
# ------------------------------------------------------------------------------------------------------------------
# Funkcja przeprowadza analize sentymentu
# --- Przyjmuje wektor
# --- Zwraca data.frame z opisem czy slowo w zdaniu bylo pozytywne, negatywne lub neutralne
# ------------------------------------------------------------------------------------------------------------------
f_analiza_sentymentu <- function(vector){
#można wybrać inne slowniki sentymentu
return (sentiment(vector, polarity_dt = lexicon::hash_sentiment_huliu))
}
coffee_result
coffee_result <- f_analiza_sentymentu(coffee_tweets_vector)
coffee_result
moja_pg_funkcja_rysuj_wykres_analizy_sentymentu <- function(sentiments){
moje_kolory <- c("deeppink", "gold", "green3")
sentiments %>%
mutate(kolor = ifelse(sentiment == 0, "Neutralna", ifelse(sentiment < 0, "Negatywna", "Pozytywna"))) %>%
ggplot(aes(element_id, sentiment, fill = kolor, color = kolor)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = moje_kolory) +
scale_color_manual(values = moje_kolory) +
labs(x = "Opinie", y = "Ocena sentymentu") +
theme_minimal()
}
moja_pg_funkcja_rysuj_wykres_analizy_sentymentu(wynik_analizy_sentymentu)
rozdzial_1 <- hrabia_oczyszczony_korpus[[1]]$content
rozdzial_1
rozdzial_1_characters <- as.character(rozdzial_1)
hrabia_wyodrebnione_zdanie <- moja_pg_funkcja_wyodrebnij_zdania(rozdzial_1_characters)
hrabia_wyodrebnione_zdanie
wynik_analizy_sentymentu <- moja_pg_funkcja_analiza_sentymentu(hrabia_wyodrebnione_zdanie)
wynik_analizy_sentymentu
moja_pg_funkcja_rysuj_wykres_analizy_sentymentu(wynik_analizy_sentymentu)
# Zadanie 4
moja_pg_funkcja_steeming_korpusu <- function(korpus){
# Steeming - wyszukiwanie rdzenia slow dla korpusu
korpus <- tm_map(korpus, stemDocument)
# Konwersja na macierz Term-Dokument
macierz_term_dokument <- TermDocumentMatrix(korpus)
}
hrabia_macierz_term_dokument <- moja_pg_funkcja_steeming_korpusu(hrabia_oczyszczony_korpus)
hrabia_macierz_term_dokument
hrabia_macierz_dokument_term <- as.DocumentTermMatrix(hrabia_macierz_term_dokument)
hrabia_macierz_dokument_term
hrabia_odleglosci <- moja_pg_funkcja_oblicz_odleglosci_miedzy_dokumentami(hrabia_macierz_dokument_term)
moja_pg_funkcja_oblicz_odleglosci_miedzy_dokumentami<- function(macierz_dokument_term){
# Obliczenie odleg�o�ci pomi�dzy dokumentami
dokumenty_data_matrix <- as.matrix(macierz_dokument_term)
odl <- dist(dokumenty_data_matrix) # domyslnie uzyta jest miara euklidesowa (method = "euclidean")
}
hrabia_odleglosci <- moja_pg_funkcja_oblicz_odleglosci_miedzy_dokumentami(hrabia_macierz_dokument_term)
hrabia_odleglosci
# Grupowanie hierarchiczne
hg <- hclust(hrabia_odleglosci)
hg
plot(hg)
moja_pg_funkcja_oblicz_odleglosci_miedzy_dokumentami<- function(macierz_dokument_term){
# Obliczenie odleg�o�ci pomi�dzy dokumentami
dokumenty_data_matrix <- as.matrix(macierz_dokument_term)
odl <- dist(dokumenty_data_matrix, method = "jaccard") # domyslnie uzyta jest miara euklidesowa (method = "euclidean")
}
hrabia_odleglosci <- moja_pg_funkcja_oblicz_odleglosci_miedzy_dokumentami(hrabia_macierz_dokument_term)
hrabia_odleglosci
hg <- hclust(odl, method = "ward.D")
hg <- hclust(hrabia_odleglosci, method = "ward.D")
hg_ladne <- as.dendrogram(hg)
hg_ladne <- color_labels(hg_ladne,3, col = c("royalblue", "orangered", "seagreen"))
hg_ladne <- color_branches(hg_ladne,3, col = c("royalblue", "orangered", "seagreen"))
plot(hg_ladne, main = paste("Dendogram na podstawie miary:\n", "Jaccard"))
rect.dendrogram(hg_ladne, k = 3, border = "grey20", lty = 2)
hrabia_macierz_term_dokument <- moja_pg_funkcja_steeming_korpusu(hrabia_oczyszczony_korpus)
hrabia_macierz_dokument_term <- as.DocumentTermMatrix(hrabia_macierz_term_dokument)