文本分析失敗的案例
一開始,你已經討論過在進行文本分析前先移除停用詞的重要性。在上一個章節中,你也複習了如何用餘弦相似度來找出彼此相似的文本。
在這個練習中,你會探索一個非常真實的情境:如果沒有正確使用文本分析,可能會失敗。你將在不移除停用詞的情況下,為《動物農莊》的各章計算餘弦相似度。
本練習屬於課程
R 的自然語言處理入門
練習說明
- 先檢視已提供的建立文字計數程式碼。這部分已幫你完成。
- 使用
widyr套件的pairwise_similarity()函式,對chapter欄位中的每個章節計算餘弦相似度。 - 將結果依
similarity從高到低排序。 - 計算
similarity值的平均數mean。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create word counts
animal_farm_counts <- animal_farm %>%
unnest_tokens(word, text_column) %>%
count(chapter, word)
# Calculate the cosine similarity by chapter, using words
comparisons <- animal_farm_counts %>%
___(___, ___, n) %>%
arrange(desc(___))
# Print the mean of the similarity values
comparisons %>%
summarize(mean = ___(___))