快速體驗文字探勘
有時候,只要看看最常出現的字詞,就能推測作者的用意與主旨。
就核心概念而言,bag-of-words 文字探勘是一種在多份文件中計算詞彙或「n-grams」出現次數的方法。請看下列句子,我們已存成 text 並放在你的工作環境中:
text <- "Text mining usually involves the process of structuring the input text. The overarching goal is, essentially, to turn text into data for analysis, via the application of natural language processing (NLP) and analytical methods."
手動統計上面句子的詞彙次數很麻煩!還好有 qdap 套件提供更好的作法。你可以呼叫 freq_terms 並指定 4,就能輕鬆找出 text 中出現頻率最高的 4 個詞(包含並列的情況)。
frequent_terms <- freq_terms(text, 4)
frequent_terms 物件會儲存所有不重複的詞與其計數。接著只要對 frequent_terms 物件呼叫 plot,就能直接畫出長條圖。
plot(frequent_terms)
本練習屬於課程
R 的 Bag-of-Words 文本探勘
練習說明
我們已在你的工作環境中建立一個名為 new_text 的物件,裡面包含多個句子。
- 載入
qdap套件。 - 將
new_text列印到主控台。 - 建立
term_count,內容為new_text中出現頻率最高的 10 個詞。 - 以
term_count的結果繪製長條圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Load qdap
___
# Print new_text to the console
new_text
# Find the 10 most frequent terms: term_count
term_count <- ___
# Plot term_count
___