如何在MATLAB中实现频谱核函数?

发布于 2024-07-29 04:30:56 字数 155 浏览 8 评论 0原文

频谱核函数通过计算两个字符串之间相同的 n 元语法来对字符串进行操作。 例如,“tool”具有三个 2-gram(“to”、“oo”和“ol”),“tool”和“fool”之间的相似度为 2。(“oo”和“ol”的共同点)。

如何编写可以计算该指标的 MATLAB 函数?

A spectrum kernel function operates on strings by counting the same n-grams in between two strings. For example, 'tool' has three 2-grams ('to', 'oo', and 'ol'), and the similarity between 'tool' and 'fool' is 2. ('oo' and 'ol' in common).

How can I write a MATLAB function that could calculate this metric?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2024-08-05 04:30:56

第一步是创建一个可以为给定字符串生成 n 元语法的函数。 以矢量化方式实现此目的的一种方法是使用一些巧妙的索引。

function [subStrings, counts] = n_gram(fullString, N)
  if (N == 1)
    [subStrings, ~, index] = unique(cellstr(fullString.'));  %.'# Simple case
  else
    nString = numel(fullString);
    index = hankel(1:(nString-N+1), (nString-N+1):nString);
    [subStrings, ~, index] = unique(cellstr(fullString(index)));
  end
  counts = accumarray(index, 1);
end

这使用函数 HAN​​KEL 首先创建一个索引矩阵,该矩阵将从给定字符串中选择每组唯一的 N 长度子字符串。 使用此索引矩阵对给定字符串进行索引将创建一个字符数组,每行包含一个 N 长度的子字符串。 然后函数 CELLSTR 将字符数组的每一行放入一个单元格中元胞数组的。 函数 UNIQUE 然后删除重复的子字符串,函数 ACCUMARRAY 用于计算每个唯一子字符串的出现次数(如果需要它们)任何原因)。

通过上述函数,您可以使用 相交 函数:

subStrings1 = n_gram('tool',2);
subStrings2 = n_gram('fool',2);
sharedStrings = intersect(subStrings1,subStrings2);
nShared = numel(sharedStrings);

The first step would be to create a function that can generate an n-gram for a given string. One way to do this in a vectorized fashion is with some clever indexing.

function [subStrings, counts] = n_gram(fullString, N)
  if (N == 1)
    [subStrings, ~, index] = unique(cellstr(fullString.'));  %.'# Simple case
  else
    nString = numel(fullString);
    index = hankel(1:(nString-N+1), (nString-N+1):nString);
    [subStrings, ~, index] = unique(cellstr(fullString(index)));
  end
  counts = accumarray(index, 1);
end

This uses the function HANKEL to first create a matrix of indices that will select each set of unique N-length substrings from the given string. Indexing the given string with this index matrix will create a character array with one N-length substring per row. The function CELLSTR then places each row of the character array into a cell of a cell array. The function UNIQUE then removes repeated substrings, and the function ACCUMARRAY is used to count the occurrences of each unique substring (if they are needed for any reason).

With the above function you can then easily count the number of n-grams shared between two strings using the INTERSECT function:

subStrings1 = n_gram('tool',2);
subStrings2 = n_gram('fool',2);
sharedStrings = intersect(subStrings1,subStrings2);
nShared = numel(sharedStrings);
夜声 2024-08-05 04:30:56

您正在寻找的称为汉明距离,如果您执行doc pdist,您可以获得更好的描述。

A=['Marcin'; 'Martin'; 'Marsha']  %data

squareform(pdist(A, 'hamming'))  returns

         0    0.1667    0.5000

    0.1667         0    0.5000

    0.5000    0.5000         0

此表格显示有多少个字母不同。 “Marcin”和“Martin”之间的差异是 6 个字母中的 1 个,因此您得到 1/6=0.1667 “Marcin”与“Marsha”有 6 个字母中的 3 个,因此 3/6=0.5
如果您想要不同的字母的实际数量,只需将整个矩阵乘以长度(A)即可。

What you're looking for is called the Hamming distance, you can get a better description of it if you do doc pdist.

A=['Marcin'; 'Martin'; 'Marsha']  %data

squareform(pdist(A, 'hamming'))  returns

         0    0.1667    0.5000

    0.1667         0    0.5000

    0.5000    0.5000         0

This form shows you how many letters are different. Difference between 'Marcin' and 'Martin' is 1 out of 6 letters, so you get 1/6=0.1667 'Marcin' vs 'Marsha' has 3 of 6, so 3/6=0.5
If you want the actual number of letters that are different, just multiply the whole matrix by length(A).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文