library(topicmodels)
library(tm)
library(tidyverse)
library(tidytext)
library(tidyr)
library(slam)
library(ggrepel)
library(MASS)
library(textstem)
library(readtext)
Visualize word differences
library(tidyr)
# using CTM model object, extract beta values and store in a matrix
review_topics <- tidy(CTM9, matrix = "beta")
# pivot from long to a wide data format and
# calculate a ratio of topic 2: topic 1 likelihood (uses beta)
beta_wide1 <- review_topics %>%
mutate(topic = paste0("topic", topic)) %>%
pivot_wider(names_from = topic, values_from = beta) %>%
filter(topic1 > .001 | topic2 > .001) %>%
mutate(log_ratio = log2(topic2 / topic1))
# create the plot with a random selection
beta_wide1 %>%
sample_n(size = 25) %>%
ggplot(aes(x = reorder(term, desc(log_ratio)), y = log_ratio)) + geom_histogram(stat = 'identity') + coord_flip() + ylab("Log Ratio of beta in Topic 2 / Topic 1") + xlab("Term")
## Warning in geom_histogram(stat = "identity"): Ignoring unknown parameters:
## `binwidth`, `bins`, and `pad`
beta_wide2 <- review_topics %>%
mutate(topic = paste0("topic", topic)) %>%
pivot_wider(names_from = topic, values_from = beta) %>%
filter(topic8 > .001 | topic9 > .001) %>%
mutate(log_ratio = log2(topic9 / topic8))
# create the plot
beta_wide2 %>%
sample_n(size = 50) %>%
ggplot(aes(x = reorder(term, desc(log_ratio)), y = log_ratio)) + geom_histogram(stat = 'identity') + coord_flip() + ylab("Log Ratio of beta in Topic 9 / Topic 8") + xlab("Term")
## Warning in geom_histogram(stat = "identity"): Ignoring unknown parameters:
## `binwidth`, `bins`, and `pad`
Silge and Robinson (2017) - Relationship between sentiment and topics
library(textfeatures)
# assign topic from CTM to observations
# use the function topics() from topicmodels library to assign the most
# likely topics for each document (in this case combined reasons)
topicmodels::topics(CTM9)
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## 3 2 8 8 7 4 1 5 7 5 7 7 3 8 9 6 1 2 5 5
## 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
## 5 4 5 7 6 4 7 5 8 4 5 4 2 8 6 4 1 5 5 4
## 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
## 2 9 7 8 2 8 6 6 7 7 7 4 8 5 3 4 6 3 4 6
## 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
## 3 2 2 1 5 5 7 2 7 8 8 1 6 9 8 7 1 2 1 3
## 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
## 6 6 3 4 6 7 3 3 6 8 6 1 4 8 6 9 6 9 1 2
topic_assigned <- as.data.frame(topicmodels::topics(CTM9))
topic_assigned$row_id <- rownames(topic_assigned)
colnames(topic_assigned) <- c("topic_assigned", "row_id")
topic_assigned
## topic_assigned row_id
## 1 3 1
## 2 2 2
## 3 8 3
## 4 8 4
## 5 7 5
## 6 4 6
## 7 1 7
## 8 5 8
## 9 7 9
## 10 5 10
## 11 7 11
## 12 7 12
## 13 3 13
## 14 8 14
## 15 9 15
## 16 6 16
## 17 1 17
## 18 2 18
## 19 5 19
## 20 5 20
## 21 5 21
## 22 4 22
## 23 5 23
## 24 7 24
## 25 6 25
## 26 4 26
## 27 7 27
## 28 5 28
## 29 8 29
## 30 4 30
## 31 5 31
## 32 4 32
## 33 2 33
## 34 8 34
## 35 6 35
## 36 4 36
## 37 1 37
## 38 5 38
## 39 5 39
## 40 4 40
## 41 2 41
## 42 9 42
## 43 7 43
## 44 8 44
## 45 2 45
## 46 8 46
## 47 6 47
## 48 6 48
## 49 7 49
## 50 7 50
## 51 7 51
## 52 4 52
## 53 8 53
## 54 5 54
## 55 3 55
## 56 4 56
## 57 6 57
## 58 3 58
## 59 4 59
## 60 6 60
## 61 3 61
## 62 2 62
## 63 2 63
## 64 1 64
## 65 5 65
## 66 5 66
## 67 7 67
## 68 2 68
## 69 7 69
## 70 8 70
## 71 8 71
## 72 1 72
## 73 6 73
## 74 9 74
## 75 8 75
## 76 7 76
## 77 1 77
## 78 2 78
## 79 1 79
## 80 3 80
## 81 6 81
## 82 6 82
## 83 3 83
## 84 4 84
## 85 6 85
## 86 7 86
## 87 3 87
## 88 3 88
## 89 6 89
## 90 8 90
## 91 6 91
## 92 1 92
## 93 4 93
## 94 8 94
## 95 6 95
## 96 9 96
## 97 6 97
## 98 9 98
## 99 1 99
## 100 2 100
# add the same row_id column to the original data:
sample_clean <- data.frame(sample_reviews)
sample_clean$row_id <- rownames(sample_clean)
# perform data join to attach topic proportions to original data
sample_clean <- sample_clean %>%
left_join(topic_assigned, by = "row_id")
head(sample_clean)
## doc_id
## 1 10137_10.txt
## 2 9461_8.txt
## 3 4890_9.txt
## 4 7792_10.txt
## 5 3609_8.txt
## 6 2736_9.txt
## text
## 1 EXCUSE ME!!! HellOOOOOOOOOO!!!!!!!!!!! CUBA GOODING,Jr. Should Have Won An Oscar For His Portrayal In This Film!!! He WAS the film! While the film may be lacking in some areas, Cuba was awesome... and for me, this is the best role that he has ever played! The scene in the movie where he finds out that his mother has died made me break down and cry IN THE THEATER!! I guess I could really relate to this film because I saw the same treatment of people just like that at my own school growing up... what a tragedy! Getting to see the "real" Radio and coach at the end of the movie was really special too! If you can watch this movie and not be moved to tears, you need a heart check! If you liked "Simon Birch" and "The Mighty", you'll love "Radio" too! I wish they made more movies like this...Radio is the Real Deal!
## 2 I think a person would be well-advised to read or see (I favour reading) "Twelfth Night" before seeing, or re-seeing "She's The Man". The movie is good on its' own, but comparing the two, and looking for the in-jokes makes it a lot more fun. Shakespeare was inspired by others. I think he'd give a thumbs-up.<br /><br />Harld Bloom said in "Shakespeare, The Invention of the Human", that most of the people in "Twelfth Night" need to be locked up. Malvolio, the person Malcolm is based on, is-for no good reason. People in "She's The Man" are sane in contrast. For instance, Duke Orsino is far more leval than the Duke of Orsino. He also shows that a man can have feelings without being gay. He displays a lot of self-control.<br /><br />It's a teen comedy (a clean one), so it doesn't have the dark edges of the play. For instance Olivia in the play is mourning the death of her brother. In the movie, she has been dumped.<br /><br />If you like Sir Andrew and Sir Toby in the play, they don't have the same attention in the movie.<br /><br />The in-jokes are quite often quick. The hairdresser Pauls' last name is given once. It is Antonio. Lots of people who've read the play say that Antonio has more love for a man than is just friendship. Deep love between men was noted in those days. Some see a sexual side to it-homosexuality was illegal.<br /><br />The only line from the play I caught is where Duke Orsino quotes the coach on greatness during the soccar game. In the play, it is said by Malvolio, quoting Maria).
## 3 I originally watched this because I thought it was going to be the sequel to the League of EXTRAORDINARY Gentlemen and this movie is a whole different thing entirely going on here-a comedy! However, I loved it anyways! <br /><br />The League of Gentlemen is apparently some British TV series with some rather odd characters and some sharp humour. This is British comedy so it revolves around being very silly, dressing up in costumes and making lots of fun of Germans and french, homosexual references- in short it's very very funny!<br /><br />THe plot revolves around the writers of the TV show deciding to cancel some of the characters and the characters coming out of their dimension into the writer's dimension to stop that from happening. It's a fun twist and there's plenty of great scenes in this idiotic adventure. I laughed out loud numerous times and applauded the brazen style of humour. This makes Mr Bean look like the watered down wimp he is.(Rowan is much better in Black Adder series btw)<br /><br />This is not Monty Python, but you can never escape the comparison when you are talking British humour, and there are a few similarities but not so many as to keep it from being it's own thing and being fresh. It leans more towards the young ones and Guest House Paradiso in its' comedic style.<br /><br />If you liked this, check out The Young Ones series and Guest House Paradiso movie, and of course, I assume the TV series League of Gentlemen must be rather funny as well.
## 4 I watched this movie probably more than 20 times. The jokes are now 10-15 years old but every time I watch it it makes me fall off my chair. Two of the finest actor Salman Khan and Aamir Khan plays the lead roles here. Even if Aamir Khan is a much better actor and got the better role(smart guy) in the movie Salman Khan matches and sometimes perform even better as the dumb guy. All the characters are memorable. This movie is filled with hilarious one-liners and funny situations(a little too silly probably). Don't try to look for logic in this movie. Let your brain relax for some time. I promise it will be an experience to remember.
## 5 Warning: If the Coen Brothers or David Lynch define your taste in film, disregard this review and move on.<br /><br />Yes, I borrowed the "one line summary" from the book about President Ronald Reagan, but, among other virtues, this movie emphasizes the role that character plays in the lives of honorable human beings. This film is full of honest, decent people, and they have integrity to spare. In a word, they have "character."<br /><br />A small nitpick: Unless you know the history of WW II, you probably don't know that, from Captain Correlli's arrival on the island to the fall of Mussolini, 3 and one-half years have passed. The average viewer might think the romance was of the "whirlwind" variety. That is not so. The romance develops slowly, which gives it both dignity and meaning. The film's deliberate pace may be the director's way of marking time.<br /><br />Some reviews have criticized Cage's Italian accent. The Italian-speaking members of my family assure me that his accent is quite good. <br /><br />The history was right on the mark. Yes, the Germans turned against their Italian allies, who, for the most part, were reluctant allies from the start. If you find that shocking, keep in mind that the French Mediterranean fleet was blown up by the British in 1940, just after France's capitulation, lest it fall in the hands of the Vichy government, or worse, the Nazis.<br /><br />The depiction of the Italians as educated and cultured was a compliment to an educated and cultured civilization.<br /><br />This film was beautifully photographed, and its story was lyrical. The script was not thought-provoking, nor was it clever, but here was a situation where confusion and cleverness were not needed, nor would they have been appropriate. <br /><br />The story is tender, and the message is uplifting. The characters are honest, brave, earnest, sympathetic, and likeable. It's a nice little film. 8/10.<br /><br /> <br /><br />
## 6 If you repeat a lie enough number of times will it become the truth? 15 park avenue is the story of an alternative reality of a schizophrenic (Mithi). The movie is about her search for her home at a fictitious address where her imaginary husband and 5 children live. Aparna Sen delivers yet another masterpiece. Each and every actor of the movie was better than the other. Konkona Sen looks unbelievably convincing as a schizophrenic. She pulls off the role with such ease and maturity beyond her age. Shabana Azmi is incredible as usual. She plays the dominating and fiercely independent elder sister of Mithi who takes care of her ailing sister and aging mother. She refuses to accept that in-spite of all her strength and courage, she still feels lonely at times. This should have been a very easy movie for Rahul Bose. The role was least bit demanding and anyone could have done the role.<br /><br />The ending of the movie was the most surreal part of the whole park avenue experience. It took me a while to digest that the movie had ended. It left me confused and maybe even a bit disturbed. But later on, it started sinking in. My eyes are black. But if everyone says they are blue, will I still believe that its black??!
## row_id topic_assigned
## 1 1 3
## 2 2 2
## 3 3 8
## 4 4 8
## 5 5 7
## 6 6 4
sample_clean %>%
filter(topic_assigned == 7) %>%
unnest_tokens(word, text) %>%
anti_join(stop_words) %>%
inner_join(get_sentiments("bing")) %>%
mutate(sent_score = ifelse(sentiment == "negative", 1, -1)) %>%
group_by(row_id) %>%
summarize(sent_count = sum(sent_score)) %>%
ggplot(aes(x = row_id, y = sent_count)) + geom_histogram(stat = 'identity') +
labs(x = "Review ID", y = "Sentiment Total", title = "Sentiment Distribution for Movie Reviews Categorized by Topic 7")
## Joining, by = "word"
## Joining, by = "word"
## Warning in geom_histogram(stat = "identity"): Ignoring unknown parameters:
## `binwidth`, `bins`, and `pad`