0% found this document useful (0 votes)
4 views4 pages

Bilingual Text Analysis with TF-IDF

The document describes a data analysis process involving bilingual texts and their ratings, utilizing R programming for text processing and visualization. It includes steps for tokenizing words and bigrams, calculating term frequency-inverse document frequency (tf-idf), and generating plots to visualize the results. The analysis aims to identify significant words and bigrams associated with different ratings, culminating in graphical representations of the findings.

Uploaded by

Nguyen The Huy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Bilingual Text Analysis with TF-IDF

The document describes a data analysis process involving bilingual texts and their ratings, utilizing R programming for text processing and visualization. It includes steps for tokenizing words and bigrams, calculating term frequency-inverse document frequency (tf-idf), and generating plots to visualize the results. The analysis aims to identify significant words and bigrams associated with different ratings, culminating in graphical representations of the findings.

Uploaded by

Nguyen The Huy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Bilinguals = read_document(file='C:\\Bilinguals.

txt')
Rating = read_document(file='C:\\[Link]')

one <- data_frame(Bilinguals,Rating)

text <- one %>%


group_by(Rating) %>%
mutate(linenumber = row_number(),
ignore_case = TRUE)%>%
ungroup()

text_df <- mutate(text, text = text$Bilinguals)

new <- text_df %>%


unnest_tokens(word, text) %>%
count(Rating, word, sort = TRUE)

data(stop_words)
new_df1 <- new %>%
anti_join(stop_words) %>%
group_by(Rating)%>%
ungroup()

total_words <- new_df1 %>%


group_by(Rating) %>%
summarize(total = sum(n))
Bilinguals_words <- left_join(new_df1, total_words)
Bilinguals_words

library(ggplot2)
ggplot(Bilinguals_words, aes(n/total, fill = Rating)) +
geom_histogram([Link] = FALSE, bins = 20) +
facet_wrap(~Rating, ncol = 2, scales = "free_y" )

Bilinguals_words <- Bilinguals_words %>%


bind_tf_idf(word, Rating, n)
Bilinguals_words

Bilinguals_words %>%
select(-total) %>%
arrange(desc(tf_idf)) %>%
print(n=78)

Bilinguals_words %>%
arrange(desc(tf_idf)) %>%
mutate(word = factor(word, levels = rev(unique(word)))) %>%
group_by(Rating) %>%
top_n(4) %>%
ungroup %>%
ggplot(aes(word, tf_idf, fill = Rating)) +
geom_col([Link] = FALSE) +
labs(x = NULL, y = "tf-idf") +
facet_wrap(~Rating, ncol = 2, scales = "free") +
coord_flip()
# Filter the data for TF-IDF values greater than 0.03
high_tf_idf_words <- Bilinguals_words %>%
filter(tf_idf > 0.025) %>%
arrange(desc(tf_idf)) %>%
mutate(word = factor(word, levels = rev(unique(word))))

# Plot the filtered data


ggplot(high_tf_idf_words, aes(word, tf_idf, fill = Rating)) +
geom_col([Link] = FALSE) +
labs(x = NULL, y = "tf-idf") +
facet_wrap(~Rating, ncol = 2, scales = "free") +
coord_flip() +
theme_minimal()

# Save the plot to a file


ggsave("high_tf_idf_plot.png", width = 10, height = 8)

****************************************************************
Bilinguals = read_document(file='C:\\[Link]')
Rating = read_document(file='C:\\[Link]')

one <- data_frame(Bilinguals,Rating)

text <- one %>%


group_by(Rating) %>%
mutate(linenumber = row_number(),
ignore_case = TRUE)%>%
ungroup()

text_df <- mutate(text, text = text$Bilinguals)

Bilinguals_bigrams <- text_df %>%


unnest_tokens(bigram, text, token = "ngrams", n = 2)

Bilinguals_bigrams %>%
count(bigram, sort = TRUE) %>%
print(n=20)

library(tidyr)
bigrams_separated <- Bilinguals_bigrams %>%
separate(bigram, c("word1", "word2"), sep = " ")

bigrams_filtered <- bigrams_separated %>%


filter(!word1 %in% stop_words$word) %>%
filter(!word2 %in% stop_words$word)

# new bigram counts:


bigram_counts <- bigrams_filtered %>%
count(word1, word2, sort = TRUE) %>%
print(n=50)

bigram_counts
*******************************************************************************
Above is the key for building graphic modeling

*******************************************************************************

bigrams_united <- bigrams_filtered %>%


unite(bigram, word1, word2, sep = " ")

list(bigrams_united$bigram)

bigram_tf_idf <- bigrams_united %>%


count(Rating, bigram) %>%
bind_tf_idf(bigram, Rating, n) %>%
arrange(desc(tf_idf))
bigram_tf_idf

bigram_tf_idf %>% print(n=50)

----------------------------------------------------------------------------
bigram_tf_idf %>%
arrange(desc(tf_idf)) %>%
mutate(bigram = factor(bigram, levels = rev(unique(bigram)))) %>%
group_by(Rating) %>%
top_n(4) %>%
ungroup %>%
ggplot(aes(bigram, tf_idf, fill = Rating)) +
geom_col([Link] = FALSE) +
labs(x = NULL, y = "tf-idf") +
facet_wrap(~Rating, ncol = 2, scales = "free") +
coord_flip()

-------------------------------------------------------------------------------
-

library(widyr)
library(igraph)
library(ggraph)

bigram_graph <- bigram_counts %>%


filter(n > 0) %>%
graph_from_data_frame()

# Assuming bigram_graph is correctly created from bigram_counts

[Link](2016)
a <- grid::arrow(type = "closed", length = unit(.15, "cm"))
ggraph(bigram_graph, layout = 'kk') + # Corrected layout specification
geom_edge_link(arrow = a, end_cap = circle(.07, 'inches')) +
geom_node_point(color = "red", size = 4) +
geom_node_text(aes(label = name), vjust = 1, hjust = 1) +
theme_void() +
theme([Link] = element_text(size=22, color="darkgrey"), [Link]
=element_text(size=14)) +
labs(title = "Title", subtitle = "Drinks Comments")
[Link](2016)
aes(edge_alpha = 1), [Link] = FALSE,

You might also like