如何在MATLAB中实现频谱核函数?
频谱核函数通过计算两个字符串之间相同的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一步是创建一个可以为给定字符串生成 n 元语法的函数。 以矢量化方式实现此目的的一种方法是使用一些巧妙的索引。
这使用函数 HANKEL 首先创建一个索引矩阵,该矩阵将从给定字符串中选择每组唯一的 N 长度子字符串。 使用此索引矩阵对给定字符串进行索引将创建一个字符数组,每行包含一个 N 长度的子字符串。 然后函数 CELLSTR 将字符数组的每一行放入一个单元格中元胞数组的。 函数 UNIQUE 然后删除重复的子字符串,函数 ACCUMARRAY 用于计算每个唯一子字符串的出现次数(如果需要它们)任何原因)。
通过上述函数,您可以使用 相交 函数:
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.
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:
您正在寻找的称为汉明距离,如果您执行
doc pdist
,您可以获得更好的描述。此表格显示有多少个字母不同。 “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
.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).