玖叶教程网

前端编程开发入门

MATLAB创建词云(含程序)(matlab怎么创建命令文件)

什么是词云?词云就是将从一大段文本中按出现频率提取的关键词组织成云朵或其他的形状,并在视觉上突出出现频率较高的关键字。比如一些例子,从实用性来说,Python制作词云较为方便,但是MATLAB也有相应的功能,对于英文也是比较方便。


实例

对英文txt内容绘制词云

程序


clc;
clear all;
close all;
%读取数据和清洗数据
%read txt as a string
text = string(fileread('E:\matlab\bin\work\研究生\微信公众号\20211106\优美文章.txt'));
%删除符号
%delete puchuation
punctuationCharacters = ["." "?" "!" "," ";" ":" ];
text = replace(text,punctuationCharacters," ");
%转变为字符向量
%convert a string to array
words = split(join(text));
%设置出现次数少于5次的并删除,只保留6次以上的
%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));
%生成词云
figure
%set properties for word cloud
wordcloud(uniqueWords,numOccurrences,'Shape', "rectangle", 'MaxDisplayWords', 200);
title("Word Cloud History")
saveas(gcf, 'Word Cloud.jpg'); %保存当前窗口的图像

运行结果


本文部分内容来源网络 ,仅供参考学习,如内容、图片有任何版权问题,请联系处理,24小时内删除。

作 者 | 郭志龙
编 辑 | 郭志龙
校 对 | 郭志龙

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言