尝试不同的方法
很好,您已经了解了多种计算字符串距离的方法。选择哪一种取决于很多因素,所以建议您多尝试不同的方法及其参数,以便更好地理解它们。本练习中,您将使用搜索词 "Marya Carey"——这是 "Mariah Carey" 的拼写错误版本。使用不同的字符串距离方法时,这个错误拼写与真实名字的相似度如何?
目标是找到一组参数:对上述两个名字给出较小的距离,同时与列表中不是目标人物的其他名字保持较大的距离。
本练习是课程的一部分
R 中级正则表达式
练习说明
- 生成子串长度为
1和2的 q-gram。 - 使用 q-gram 方法,计算
search与names在子串长度为1和2时的字符串距离。 - 使用
"osa"方法,计算search与names的字符串距离。
交互式实操练习
通过完成这段示例代码来试试这个练习。
search <- "Mariah Carey"
names <- c("M. Carey", "Mick Jagger", "Michael Jackson")
# Pass the values 1 and 2 as "q" and inspect the qgrams
qgrams("Mariah Carey", "M. Carey", q = ___)
qgrams("Mariah Carey", "M. Carey", q = ___)
# Try the qgram method on the variables search and names
stringdist(___, ___, method = "___", q = 1)
stringdist(___, ___, method = "___", q = 2)
# Try the default method (osa) on the same input and compare
stringdist(___, ___, method = "___")