3

我有一个很大的段落,想知道其中出现最多的单词是什么。谁能指出我正确的方向?任何示例和解释都会有所帮助。谢谢!

4

2 回答 2

5

这是一种非常 MATLAB-y 的方法。我试图清楚地命名变量。玩弄每一行并检查结果以了解其工作原理。主力功能:uniquehist

% First produce a cell array of words to be analyzed
paragraph_cleaned_up_whitespace = regexprep(paragraph, '\s', ' ');
paragraph_cleaned_up = regexprep(paragraph_cleaned_up_whitespace, '[^a-zA-Z0-9 ]', '');
words = regexpi(paragraph_cleaned_up, '\s+', 'split');

[unique_words, i, j] = unique(words);
frequency_count = hist(j, 1:max(j));
[~, sorted_locations] = sort(frequency_count);
sorted_locations = fliplr(sorted_locations);
words_sorted_by_frequency = unique_words(sorted_locations).';
frequency_of_those_words = frequency_count(sorted_locations).';
于 2012-11-27T21:05:32.397 回答
2

这是一个简单的解决方案,应该很快。

example_paragraph = 'This is an example corpus. Is is a verb?';

words = regexp(example_paragraph, ' ', 'split');
vocabulary = unique(words);
n = length(vocabulary);
counts = zeros(n, 1);
for i=1:n
    counts(i) = sum(strcmpi(words, vocabulary{i}));
end

[frequency_of_the_most_frequent_word, idx] = max(counts);
most_frequent_word = vocabulary{idx};

您还可以在此处查看答案,以从单词数组中获取最常见的单词。

于 2012-11-27T22:06:04.933 回答