今天要總結的是 Word Cloud 最后一個部分了,用 Matlab 來創建 word cloud。Matlab R2018b 已經提供 [wordcloud](https://www.mathworks.com/help/matlab/ref/wordcloud.html) 函數可以直接生成詞雲了。
##### >> Create Word Cloud via Matlab
- 准備文本。
不多說了,懶人繼續用上次那個 Word Cloud History.txt 的文本吧。
- 讀取並清洗數據文本。
%read txt as a string
text = string(fileread('C:\Users\yuki\Desktop\WordCloudHistory.txt'));
%delete puchuation
punctuationCharacters = ["." "?" "!" "," ";" ":"];
text = replace(text,punctuationCharacters," ");
%convert a string to array
words = split(join(text));
%delete the words has less than 5 characters, which are problely stop words
words(strlength(words)<5) = [];
%change all words to lowercase
words = lower(words);
- 計算詞頻並生成數組。
%calculate the frequencies for every word
[numOccurrences,uniqueWords] = histcounts(categorical(words));
- 生成 word cloud。
figure
%set properties for word cloud
wordcloud(uniqueWords,numOccurrences,'Shape', "rectangle", 'MaxDisplayWords', 200);
title("Word Cloud History")
##### >> Notes
-
Matlab 也有插件可以直接生成詞雲,操作簡單,不用編程,哈哈。
-
既然已經說了各種可以創建詞雲的方法,那么就順便總結一下什么方法好用方便不花錢。
Tool | Easy Use | Free | Need Script | |||
---|---|---|---|---|---|---|
Python | Clear document, powerful text mining library | Yes | Yes | |||
JavaScript | Need to extract array by own, and need to find a way to save the image | Yes | Yes | |||
R | Clear document, powerful text mining library | Yes | Yes | |||
Matlab | Clear document, interactive interface | No | Optional |
##### >> Sample Code
##### >> Related Blogs
1. [Word Cloud (詞雲) - Python](https://www.cnblogs.com/yukiwu/p/10967037.html) 2. [Word Cloud (詞雲) - JavaScript](https://www.cnblogs.com/yukiwu/p/10968816.html) 3. [Word Cloud (詞雲) - R](https://www.cnblogs.com/yukiwu/p/10969250.html)