본문 바로가기
R

[R][stringdist]Approximate matching and string distance

by Chandler.j 2024. 2. 19.
반응형

1. stringdist

2. Tutorial


1. stringdist

- stringdist 라이브러리는 문자열 간의 거리를 계산하는 다양한 함수를 제공합니다.
- Levenshtein, Jaro-Winkler, Damerau-Levenshtein 등의 거리 측정 방법을 지원하며, 이를 통해 문자열 유사도를 측정할 수 있습니다.
- 텍스트 마이닝, 자연어 처리 등의 분야에서 활용되며, 데이터 분석 및 시각화에도 유용하게 사용됩니다.

- Github

RDocumentation

- Docs: PDF


2. Tutorial

- library

library(data.table)
library(stringdist)

- sample data

cat("================================string1\n",string1)
cat("\n================================string2\n",string2)


- stringdist finction (한글도 어느정도 커버 되는듯함)

dt_sample <- data.table(
  string1 = string1
)

for (i in 1:length(dt_sample$string1)) {
  
  # Calculate the string distances between the current string and objects
  distances <- stringdist::stringdist(string1[i], string2, method = 'lv', p=0.0001)
  
  # Find the index of the object with the lowest distance (highest similarity)
  match_index <- which.min(distances)
  
  # Retrieve the matched object from the objects vector
  match_string2 <- string2[match_index]
  
  # Add the matched object to the data.table as a new column
  dt_sample[i,"min_dist"] <- match_string2
}
dt_sample

 


TOP

Designed by 티스토리