I have a data frame called text
with two columns, year
and text
. Find the dput output below for an example:
text <- structure(list(year = 2000:2007, text = c("I went to McDonald's and they charge me 50 for Big Mac when I only came with 49. The casher told me that I can't read correctly and told me to get glasses. I am file a report on your casher and now I'm mad.",
"I went to McDonald's and they charge me 50 for Big Mac when I only came with 49. The casher told me that I can't read correctly and told me to get glasses. I am file a report on your casher and now I'm mad.",
"I really think that if you can buy breakfast anytime then I should be able to get a cheeseburger anytime especially since I really don't care for breakfast food. I really like McDonald's food but I preferred tree lunch rather than breakfast. Thank you thank you thank you.",
"I guess the employee decided to buy their lunch with my card my card hoping I wouldn't notice but since it took so long to run my car I want to head and check my bank account and sure enough they had bought food on my card that I did not receive leave. Had to demand for and for a refund because they acted like it was my fault and told me the charges are still pending even though they are for 2 different amounts.",
"Never order McDonald's from Uber or Skip or any delivery service for that matter, most particularly one on Elgin Street and Rideau Street, they never get the order right. Workers at either of these locations don't know how to follow simple instructions. Don't waste your money at these two locations.",
"Employees left me out in the snow and wouldn’t answer the drive through. They locked the doors and it was freezing. I asked the employee a simple question and they were so stupid they answered a completely different question. Dumb employees and bad food.",
"McDonalds food was always so good but ever since they add new/more crispy chicken sandwiches it has come out bad. At first I thought oh they must haven't had a good day but every time I go there now it's always soggy, and has no flavor. They need to fix this!!!",
"I just ordered the new crispy chicken sandwich and I'm very disappointed. Not only did it taste horrible, but it was more bun than chicken. Not at all like the commercial shows. I hate sweet pickles and there were two slices on my sandwich. I wish I could add a photo to show the huge bun and tiny chicken."
)), class = "data.frame", row.names = c(NA, -8L))
I want to calculate cosine similarity for each two-row text combination in the data frame. I am using the package textTinyR
and the function cosine_distance
to calculate cosine similarity. I wrote the following code however it failed:
result <- text %>%
mutate(cosine_similarity = cosine_distance(text, dplyr::lag(text)))
specifically, I get this error:
Error in `mutate()`:
ℹ In argument: `cosine_similarity = cosine_distance(text, dplyr::lag(text))`.
Caused by error:
! Expecting a single string value: [type=character; extent=8].
I understand that the function cosine_distance()
function takes individual string values and because I use dplyr
's mutate
, it passes on the whole vector, resulting in the error.
As an example, I tried the following and it worked:
sentence1 = 'this is one sentence'
sentence2 = 'this is second sentence'
cds = cosine_distance(sentence1, sentence2)
print(cds)
However, I am not sure how to apply this so that I can have cosine similarity as an additional variable in the dataframe. Such that, for example, in the row with the year 2003, I have cosine similarity for text in year 2003 compared to 2002, and so on. Would really appreciate your help.
You can use Map
to vectorize over text
and lag(text)
:
text %>%
as_tibble() %>%
mutate(cosine_similarity = unlist(Map(cosine_distance,
text, dplyr::lag(text))))
#> # A tibble: 8 x 3
#> year text cosin~1
#> <int> <chr> <dbl>
#> 1 2000 I went to McDonald's and they charge me 50 fo~ 0
#> 2 2001 I went to McDonald's and they charge me 50 fo~ 1
#> 3 2002 I really think that if you can buy breakfast ~ 0.336
#> 4 2003 I guess the employee decided to buy their lun~ 0.285
#> 5 2004 Never order McDonald's from Uber or Skip or a~ 0.179
#> 6 2005 Employees left me out in the snow and wouldn’~ 0.170
#> 7 2006 McDonalds food was always so good but ever si~ 0.252
#> 8 2007 I just ordered the new crispy chicken sandwic~ 0.291
#> # ... with abbreviated variable name 1: cosine_similarity
Note that the first entry is zero because there is no preceding string to compare. The first two entries in text
are identical, so you get a value of 1 in the second year, etc.