Trong bài viết này, Quý sẽ trình bày cho các bạn các khái niệm để mô phỏng dữ liệu dạng WordCloud sử dụng TextMining trong R.
Khái niệm Text Mining
Về bản chất, Text Mining là các phương pháp cho phép chúng ta miêu tả, làm sáng tỏ các từ khóa được sử dụng nhiều nhất trong 1 đoạn văn bản hoặc 1 đoạn văn bản. Khi mô phỏng dữ liệu trong đoạn văn bản này, dữ liệu sẽ được mô phỏng theo dạng hình đám mây từ ngữ - word cloud và diễn giải các từ được sử dụng nhiều nhất.
Trong R, chúng ta sử dụng hai thư viện khi làm việc liên quan đến nội dung này: - Thư viện tm : Sử dụng cho khai phá văn bản - text mininig - Thư viện wordcloud: Sử dụng để mô phỏng dữ liệu dạng đám mây
Mô phỏng WordCloud
Những lý do để chọn Word Cloud:
Có nhiều cách để chúng ta có thể mô phỏng, trực quan hóa dữ liệu, từ cách trình bày bảng thống kê với những dữ liệu số, những dữ liệu biểu đồ thì trình bày dữ liệu chữ có một số nội dung sau có thể khái quát:
- Sử dụng phương pháp Word Cloud rất trực quan và trong sáng trong việc trình bày dữ liệu, dữ liệu có thể hiểu được ngay và rất dễ hiểu;
- Sử dụng phương pháp Word Cloud dễ sử dụng, dễ dàng trực quan;
- Sử dụng phương pháp Word Cloud trực quan hơn so với việc trình bày bảng dữ liệu.
Ứng dụng Word Cloud trong R
Tạo file text
Trong nội dung này, Quý đưa ra nội dung của bài phát biểu diễn văn “Tôi có một giấc mơ” - Martin Luther King. Cụ thể nội dung và ý nghĩa của bài diễn văn này, có thể xem tại đây
Khái niệm Corpus
Corpus là 1 dữ liệu tập hợp các văn bản, ngôn ngữ đã được số hoá. Cách dịch thông thường ở VN là “kho ngữ liệu”. Ví dụ về corpus như “tuyển tập các tác phẩm của Nam Cao”, hay “tuyển tập ca từ của Trịnh Công Sơn”, …
Các corpus là 1 tài nguyên quan trọng trong NLP (Xử lý ngôn ngữ tự nhiên). Từ các corpus, ta có thể rút ra những dữ liệu quan trọng sau :
Từ các corpus, ta có thể chiết suất 1 cách tự động các qui tắc ngữ pháp “văn mạch tự do”.
Từ các corpus có thể tính toán được xác suất, tần suất xuất hiện của các từ. Để đảm bảo tính chính xác cho 2 kết luận trên, corpus phải đảm bảo 1 số nguyên tắc nhất định :
Tính đại diện : các thành phần trong corpus phải có tính phổ quát, đa dạng và phong phú.
Kích thước : kích thước của corpus càng lớn thì càng được đánh giá cao. Dựa vào mục đích, cách xây dựng corpus, người ta chia corpus thành các loại sau :
Corpus thô (raw corpus): đơn giản chỉ là tập hợp các dữ liệu mà không có xử lý gì thêm.
corpus được gắn nhãn (tagged corpus) : các dữ liệu trong corpus đã được xử lý như phân tích từ, phân tích cú pháp, gắn nhãn từ loại, …
Parallel Corpus : được sử dụng nhiều trong ứng dụng máy dịch.
Bài phát biểu của Martin Luther King
## <<SimpleCorpus>>
## Metadata: corpus specific: 1, document level (indexed): 0
## Content: documents: 46
##
## [1]
## [2] And so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream.
## [3]
## [4] I have a dream that one day this nation will rise up and live out the true meaning of its creed:
## [5]
## [6] We hold these truths to be self-evident, that all men are created equal.
## [7]
## [8] I have a dream that one day on the red hills of Georgia, the sons of former slaves and the sons of former slave owners will be able to sit down together at the table of brotherhood.
## [9]
## [10] I have a dream that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice.
## [11]
## [12] I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character.
## [13]
## [14] I have a dream today!
## [15]
## [16] I have a dream that one day, down in Alabama, with its vicious racists, with its governor having his lips dripping with the words of interposition and nullification, one day right there in Alabama little black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers.
## [17]
## [18] I have a dream today!
## [19]
## [20] I have a dream that one day every valley shall be exalted, and every hill and mountain shall be made low, the rough places will be made plain, and the crooked places will be made straight; and the glory of the Lord shall be revealed and all flesh shall see it together.
## [21]
## [22] This is our hope, and this is the faith that I go back to the South with.
## [23]
## [24] With this faith, we will be able to hew out of the mountain of despair a stone of hope. With this faith, we will be able to transform the jangling discords of our nation into a beautiful symphony of brotherhood. With this faith, we will be able to work together, to pray together, to struggle together, to go to jail together, to stand up for freedom together, knowing that we will be free one day.
## [25]
## [26] And this will be the day, this will be the day when all of God s children will be able to sing with new meaning:
## [27]
## [28] My country tis of thee, sweet land of liberty, of thee I sing.
## [29] Land where my fathers died, land of the Pilgrim s pride,
## [30] From every mountainside, let freedom ring!
## [31] And if America is to be a great nation, this must become true.
## [32] And so let freedom ring from the prodigious hilltops of New Hampshire.
## [33] Let freedom ring from the mighty mountains of New York.
## [34] Let freedom ring from the heightening Alleghenies of Pennsylvania.
## [35] Let freedom ring from the snow-capped Rockies of Colorado.
## [36] Let freedom ring from the curvaceous slopes of California.
## [37]
## [38] But not only that:
## [39] Let freedom ring from Stone Mountain of Georgia.
## [40] Let freedom ring from Lookout Mountain of Tennessee.
## [41] Let freedom ring from every hill and molehill of Mississippi.
## [42] From every mountainside, let freedom ring.
## [43] And when this happens, when we allow freedom ring, when we let it ring from every village and every hamlet, from every state and every city, we will be able to speed up that day when all of God s children, black men and white men, Jews and Gentiles, Protestants and Catholics, will be able to join hands and sing in the words of the old Negro spiritual:
## [44] Free at last! Free at last!
## [45]
## [46] Thank God Almighty, we are free at last!
Làm sạch text (Cleaning the Text)
Sử dụng hàm tm_map () được sử dụng để loại bỏ khoảng trắng không cần thiết, để chuyển văn bản thành chữ thường, để loại bỏ các từ dừng phổ biến như ‘the’, “we”.
Giá trị thông tin của ‘từ dừng’ gần bằng 0 do thực tế là chúng rất phổ biến trong một ngôn ngữ. Loại bỏ loại từ này rất hữu ích trước khi phân tích sâu hơn. Đối với ‘stopwords’, các ngôn ngữ được hỗ trợ là tiếng Tây Ban Nha, tiếng Hà Lan, tiếng Anh, tiếng Phần Lan, tiếng Pháp, tiếng Đức, tiếng Hungary, tiếng Ý, tiếng Na Uy, tiếng Bồ Đào Nha, tiếng Nga, tiếng Tây Ban Nha và tiếng Thụy Điển.
Xóa số và dấu chấm câu với các đối số removeNumbers và removePunctuation.
Một bước tiền xử lý quan trọng khác là tạo một văn bản gốc để giảm các từ về dạng gốc của chúng. Nói cách khác, quá trình này loại bỏ các hậu tố khỏi các từ để làm cho nó trở nên đơn giản và có được nguồn gốc chung. Ví dụ, quá trình tạo gốc làm giảm các từ “chuyển động”, “di chuyển” và “chuyển động” thành từ gốc, “di chuyển”. Mời bạn đọc kỹ nội dung tiếng Anh sau để hiểu rõ hơn:
Another important preprocessing step is to make a text stemming which reduces words to their root form. In other words, this process removes suffixes from words to make it simple and to get the common origin. For example, a stemming process reduces the words “moving”, “moved” and “movement” to the root word, “move”.
Xây dựng ma trận tài liệu thuật ngữ
Xây dựng ma trận tài liệu thuật ngữ - Build a term-document matrix:
Ma trận tài liệu là một bảng chứa tần suất xuất hiện của các từ. Tên cột là từ và tên hàng là từ văn bản. Hàm TermDocumentMatrix () từ gói khai thác văn bản có thể được sử dụng như sau:
<- TermDocumentMatrix(docs)
dtm <- as.matrix(dtm)
m <- sort(rowSums(m),decreasing=TRUE)
v <- data.frame(word = names(v),freq=v)
d head(d, 10)
## word freq
## will will 17
## freedom freedom 13
## ring ring 12
## dream dream 11
## day day 11
## let let 11
## every every 9
## one one 8
## able able 8
## together together 7
Tạo đám mây Word Cloud
set.seed(1234)
wordcloud(words = d$word, freq = d$freq, min.freq = 1,
max.words=200, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(8, "Dark2"))
Kết luận: Đám mây từ ở trên cho thấy rõ ràng rằng “Ý chí - will”, “tự do - freedom”, “ước mơ - dream”, “ngày - day” và “cùng nhau - together” là năm từ quan trọng nhất trong bài phát biểu “Tôi có một giấc mơ - I have a dream” của Martin Luther King.
Đối số của chức năng tạo Word Cloud: - words : the words to be plotted - freq : their frequencies - min.freq : words with frequency below min.freq will not be plotted - max.words : maximum number of words to be plotted - random.order : plot words in random order. If false, they will be plotted in decreasing frequency - rot.per : proportion words with 90 degree rotation (vertical text) - colors : color words from least to most frequent. Use, for example, colors =“black” for single color.
Bổ sung
Khám phá các thuật ngữ thường dùng
Chúng ta có thể xem các thuật ngữ thường dùng trong ma trận tài liệu thuật ngữ. Trong ví dụ dưới đây, Quý muốn tìm các từ xuất hiện ít nhất bốn lần:
findFreqTerms(dtm, lowfreq = 4)
## [1] "dream" "day" "nation" "one" "will" "able"
## [7] "together" "freedom" "every" "mountain" "shall" "faith"
## [13] "free" "let" "ring"
Sự liên quan của các từ trong đoạn
Chúng ta có thể phân tích mối liên hệ giữa các từ thường gặp (tức là các thuật ngữ có tương quan) bằng cách sử dụng hàm findAssocs (). Đoạn mã R bên dưới xác định những từ nào có liên quan đến “tự do - freedom”:
findAssocs(dtm, terms = "freedom", corlimit = 0.3)
## $freedom
## let ring mississippi stone mountainside state
## 0.89 0.86 0.34 0.34 0.34 0.32
## every mountain
## 0.32 0.32
Bảng tần số các từ (Frequency Table)
head(d, 10)
## word freq
## will will 17
## freedom freedom 13
## ring ring 12
## dream dream 11
## day day 11
## let let 11
## every every 9
## one one 8
## able able 8
## together together 7
Vẽ biểu đồ mô tả
Dưới đây là biểu đồ tần số mô tả 10 từ hay gặp nhất trong bài phát biểu trên:
barplot(d[1:10,]$freq, las = 2, names.arg = d[1:10,]$word,
col ="lightblue", main ="Most frequent words",
ylab = "Word frequencies")