Main Content

本页采用了机器翻译。点击此处可查看最新英文版本。

可视化词云中的词频

此示例展示了如何使用 Text Analytics Toolbox™ 在词云中可视化文本的分布。使用 ThingSpeak™ 对文本进行排序并查看哪些参数影响分布或情绪。

CheerLights 允许世界各地的人们同步到 Twitter 设置的一种颜色。ThingSpeak Channel 1417 存储 CheerLights 颜色的历史记录。读取 ThingSpeak通道中的单词并比较早期和晚期时间戳的分布。

读取文本数据并计算词频

从 CheerLights ThingSpeak通道读取最后 500 个数据点。使用文本分析工具箱中的 bagofwords 函数查找集合中每个文档中单词的频率。

colorData = thingSpeakRead(1417,'numPoints',500,'OutputFormat','Timetable','fields', [1]);
bagOfWords(colorData.LastCheerLightsCommand')
ans = 

  bagOfWords with properties:

          Counts: [61 68 40 59 23 42 52 59 40 46 10]
      Vocabulary: [1×11 string]
        NumWords: 11
    NumDocuments: 1

按时间对结果排序

查找时间戳早于午餐时间的每个数据点的索引,然后查找午餐时间之后数据点的所有索引。使用索引为每种情况创建所有单词的数组。

lateTimeIndex = (hour(colorData.Timestamps) > 12);
earlyTimeIndex = (hour(colorData.Timestamps) < 12);

lateColors = colorData.LastCheerLightsCommand(lateTimeIndex);
earlyColors = colorData.LastCheerLightsCommand(earlyTimeIndex);

比较词云中的词频

使用排序后的数据生成词云。subplot 函数允许您并排比较它们。

subplot(1,2,1);
earlyWordCloud = wordcloud(earlyColors,'Color','blue')
title('Early Morning CheerLights');

subplot(1,2,2);
lateWordCloud = wordcloud(lateColors,'color','green')
title('Late Night CheerLights');
earlyWordCloud = 

  WordCloudChart with properties:

           WordData: [1×11 string]
           SizeData: [67 60 58 58 49 46 42 40 40 22 10]
    MaxDisplayWords: 100

  Use GET to show all properties


lateWordCloud = 

  WordCloudChart with properties:

           WordData: ["cyan"    "magenta"    "red"    "white"]
           SizeData: [1 1 1 1]
    MaxDisplayWords: 100

  Use GET to show all properties

发送到 CheerLights 的颜色分布似乎仅大致取决于发布的时间。有些颜色仅在下午强烈,有些则全天持续强烈。

另请参阅

(Text Analytics Toolbox) | (Text Analytics Toolbox)

外部网站