嘗試不同的方法
太好了,你已經學會了多種計算字串距離的方法。要用哪一種方法取決於許多情況,因此先多試幾種方法與其參數,會有助於你更熟悉它們。這個練習中你會用搜尋詞 "Marya Carey"——這是姓名 "Mariah Carey" 的一個錯拼版本。用不同的字串距離方法時,這個錯拼的名字和正確名字有多相似?
目標是找出一組參數:對於上述兩個名字能得到較小的距離,同時對清單中不是目標人物的其他名字仍維持較大的距離。
本練習屬於課程
R 中級 Regular Expressions
練習說明
- 針對子字串長度
1與2產生 q-gram。 - 以 q-gram 方法,針對子字串長度
1與2,計算search與names之間的字串距離。 - 使用
"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 = "___")