開始使用免費開始

找出拼字錯誤的相近匹配

人為輸入很容易出錯。人們會把各種文字打錯,包括姓名或地址,身為資料科學家,你需要找到方法來處理。計算字串距離就是一種做法。

在小小的向量 usernames 裡有三個不同的名字。你的任務是找出最接近輸入名稱 "Emile Brown" 的結果。你能在 usernames 這個向量裡找到相似的名字嗎?請使用 amatch() 搜尋向量,並印出一個像你在 Google 上看過的推薦。

本練習屬於課程

R 中級 Regular Expressions

檢視課程

練習說明

  • amatch() 的最大編輯距離指定為 1
  • 使用儲存在 closest_indexamatch() 回傳值,印出 usernames 中對應的名字。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

usernames <- c("Max Power", "Emilie Brown", "Max Mustermann")

# Search usernames with a maximum edit distance of 1
closest_index <- amatch(
  x = "Emile Brown",
  table = usernames,
  ___ = ___,
  method = "lv"
)

# Print the matched name in usernames at closest_index
print(glue(
  "Did you mean {name_matched}?",
  name_matched = ___
))
編輯並執行程式碼