我正在尝试从标记化文档中提取所有名词并选择前 3 个。我怀疑它不起作用,因为我没有正确使用 strcmp 命令。这是我的代码
sT2 = tokenizedDocument([
"a strongly worded collection of words and letters"
"another collection of words"]);
tD = tokenizedDocument(sT2);
tD = addPartOfSpeechDetails(tD);
tdetails = tokenDetails(tD);
td7 = table2cell(tdetails(:,7)); % PARTS OF SPEECH
siztd7 = size(td7);
cc = 1;
for ii = 1:siztd7
if strcmp(td7(ii,1), 'noun') == 1
tDNoun(cc) = tdetails(1,:);
cc = cc + 1;
end
end
bag = bagOfWords(tDNoun);
tb100 = topkwords(bag,3)
变量
tdetails
是 MATLAB table
,您可以使用 table
索引 直接从中提取名词,如下所示:
nouns = tdetails{tdetails.PartOfSpeech == "noun", "Token"}
第一个下标将
table
变量 PartOfSpeech
与“名词”进行匹配,第二个下标仅提取 table
变量“Token”。使用大括号索引,即 {}
提取数据 - 在本例中为 string
单词数组。
这可以直接与
bagOfWords
一起使用,尽管我们必须 transpose
数组 nouns
才能获取该函数所需的行向量:
bag = bagOfWords(nouns')
topkwords(bag, 3)