I'm experimenting with Tidytext (Text Mining with R) and I want to use the functions pairwise_count and pairwise_cor from the widyr library. My corpus is from a per-processed text file.
library(readr)
library(dplyr)
library(tidytext)
library(widyr)
set.seed(2017)
Korpus <- read_file("/home/knecht/korpus.res")
print(Korpus)
Korpus_DF <-data_frame(document= 1, text=Korpus)
spon_words <- Korpus_DF %>%
unnest_tokens(word, text)
print(spon_words)
spon_words %>%
count(word, sort=TRUE)
word_cors <- spon_words %>%
group_by(word) %>%
filter(n()>= 10) %>%
pairwise_cor(word, document, sort = TRUE, upper= FALSE)
word_cors
pair_test <- spon_words %>%
pairwise_count(word, document)
print(pair_test)
I think, I don't get a correct result, because the corpus contains multiply phrases like "spiegel online" or "spiegel plus" phrases but these are not appear in the result table:
> library(readr)
> library(dplyr)
> library(tidytext)
> library(widyr)
> set.seed(2017)
> Korpus <- read_file("/home/knecht/korpus.res")
> print(Korpus)
[1] "29.12.2017 17:24:57 Results of ResultWriter 'Write as Text' [1]: \n29.12.2017 17:24:57 SimpleExampleSet:\n1 examples,\n0 regular attributes,\nspecial attributes = {\n text = #0: text (text/single_value)/values=[SPIEGEL ONLINE Aktuelle Nachrichten Nachrichten SPIEGEL ONLINE Mein SPIEGEL 29. Dezember 2017 TV-Programm Wetter Schlagzeilen Themenwochen Wahl Verbraucher Service Unternehmen Märkte Staat Soziales LOTTO 6aus49 Spielerindex SPIX Champions League Formel Bundesliga präsentiert von Continental Uno-Klimakonferenz 2017 Diagnose Therapie Ernährung Fitness Sex Partnerschaft Schwangerschaft Kind Erster Weltkrieg Zweiter Weltkrieg Leben und Lernen Deals der Woche IAA 2017 Front Page SPIEGEL Plus SPIEGEL Plus Deutschland SPIEGEL Plus Wirtschaft SPIEGEL Plus Gesellschaft SPIEGEL Plus Ausland SPIEGEL Plus Sport SPIEGEL Plus Wissenschaft SPIEGEL Plus Kultur SPIEGEL AKADEMIE DER SPIEGEL live DER SPIEGEL DER SPIEGEL digitales Magazin Titelbilder Heftarchive SPIEGEL SPIEGEL Magazin SPIE... <truncated>
> Korpus_DF <-data_frame(document= 1, text=Korpus)
> spon_words <- Korpus_DF %>%
+ unnest_tokens(word, text)
> print(spon_words)
# A tibble: 3,267 x 2
document word
<dbl> <chr>
1 1 29.12.2017
2 1 17
3 1 24
4 1 57
5 1 results
6 1 of
7 1 resultwriter
8 1 write
9 1 as
10 1 text
# ... with 3,257 more rows
> spon_words %>%
+ count(word, sort=TRUE)
# A tibble: 1,645 x 2
word n
<chr> <int>
1 mehr 84
2 die 78
3 und 75
4 der 63
5 spiegel 58
6 von 35
7 sie 32
8 das 31
9 ein 31
10 für 31
# ... with 1,635 more rows
> word_cors <- spon_words %>%
+ group_by(word) %>%
+ filter(n()>= 10) %>%
+ pairwise_cor(word, document, sort = TRUE, upper= FALSE)
> word_cors
# A tibble: 561 x 3
item1 item2 correlation
<chr> <chr> <dbl>
1 spiegel online NaN
2 spiegel 2017 NaN
3 online 2017 NaN
4 spiegel von NaN
5 online von NaN
6 2017 von NaN
7 spiegel und NaN
8 online und NaN
9 2017 und NaN
10 von und NaN
# ... with 551 more rows
> pair_test <- spon_words %>%
+ pairwise_count(word, document)
> print(pair_test)
# A tibble: 2,704,380 x 3
item1 item2 n
<chr> <chr> <dbl>
1 17 29.12.2017 1
2 24 29.12.2017 1
3 57 29.12.2017 1
4 results 29.12.2017 1
5 of 29.12.2017 1
6 resultwriter 29.12.2017 1
7 write 29.12.2017 1
8 as 29.12.2017 1
9 text 29.12.2017 1
10 1 29.12.2017 1
# ... with 2,704,370 more rows
Is here maybe someone, who can give me a hint, please?
regards Tobias