我有一个字符串myLongText
,大约有300 MB。现在我还有一个字符串列表(存储为单元格)myManyStrings
,其中包含N = 1到5的所有N-gram。
我现在想要的是:变量myOccurances
,其中包含length(myManyStrings)
个条目,并提供myManyStrings
中每个字符串在myLongText
中附加的次数。
直接版本将是:
myOccurances=zeros(1,length(myManyStrings));
for i=1:length(myManyStrings)
myOccurances(i)=length(strfind(myLongText,myManyStrings{i});
end
但显然,这个解决方案很慢。在早期版本中,myManyStringsOld
由单个单词组成,因此我可以使用
allSplit=strread(myLongText,'%s','delimiter',' ');
[allUnique,~,occIndex]=unique(allSplit);
myOccurancesOld = hist(occIndex,1:length(allUnique));
然而,现在myManyStrings
也涉及更高的N-gram,我不知道如何调整我的旧(并且速度惊人的快速)方法。
例如,现在仅适用于双字组合:
myLongText='Stack Overflow is a privately held website. In 2008, somebody created Stack Overflow.';
myManyStrings={'Stack', 'Overflow', 'is', 'a', 'privately', 'held', 'website', 'In', '2008', 'somebody', 'created', 'Stack Overflow', 'Overflow is', 'is a', 'a privately', 'privately held', 'held website', 'website in' 'in 2008', '2008 sombody', 'sombody created', 'created Stack'}.
因此,
myOccurances=[2 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1];
您知道产生结果的快速方法吗?