1

我在 Octave/Matlab 中创建 MinHash 和 LSH。但是我试图从给定的文档中获取一组大小为 k 的带状疱疹(单元数组或数组),但我不知道该怎么做。

我现在拥有的是这个简单的代码:

doc = fopen(document);
i = 1;
while (! feof(doc) )
  txt{i} = strread(fgetl(doc), '%s');
  i++;
endwhile
fclose(doc);

这将创建一个包含文档每一行中所有单词的单元格数组,这是我正在尝试执行的函数的一个参数。

4

1 回答 1

0

这段代码可以解决问题。它从元胞数组中读取并创建指定大小的带状疱疹(n-gram)。

function S = shingles(txt, shingle_size)
  l = size(txt)(2) - shingle_size + 1;
  for i = 1:l
    t='';
    for j = i:(i + shingle_size - 2)
      t = strcat(t,txt{j},' ');
    end
    t = strcat(t, txt{i + shingle_size - 1});
    S{i} = t;
  end

您可以使用以下示例测试代码:

txt={'a','b','c'}
shingles(txt, 2)
S =
{
  [1,1] = ab
  [1,2] = bc
}
于 2017-12-02T22:42:55.857 回答