dumbcode/modalite_proche.R

#!/usr/bin/Rscript
# -*- coding: utf-8 -*-

################################ Description ###################################
# Title: Test modalite proche
# Purpose: Test avec les distances entre les mots pour trouver
# des modalités proches
# Created the 2014-12-30 
# by Joris Muller <joris.muller@jom.link>
# Licence: GPLv3 <http://www.gnu.org/licenses/>
################################################################################

library(stringdist)

chaines <- c("maison", "Maisons", "Maison")
accents <- c("Bebe", "Bébé")
stringdist(a = c("Un", "Deux"), b = c("UN", "DEUX"))

stringdistmatrix(a = chaines, b = chaines)
stringdistmatrix(accents, accents)

chaine2 <- c("mainson", "maison", "cave", "caves", "Cave", "Hôpital", "Hopital", "Bachibouzouk")
dd <- stringdistmatrix(chaine2, chaine2, useNames = TRUE)
dd < 2

dd[1,]
for(un_mot in chaine2){

    proches <- ain(chaine2, un_mot, maxDist = 3)
    if(any(proches)) {
        mots_proches <- chaine2[proches]
        cat(un_mot, "est proche de '", paste(mots_proches, collaspe="', '"), "'\n")
    }
}

for (i in 1:nrow(dd)) {

    ligne <- dd[i,]
    mot <- rownames(dd)[i]
    proches <- ligne > 0 & ligne < 4 
    noms_proches <- names(ligne)[proches]

    cat("'", mot, "' est proche de '", paste(noms_proches,collapse= "', '"), "'\n", sep = "")
}

# Sous la forme d'une fonction

close_string <- function(char) {
# Only takes the unique names
char <- unique(char)    

dd <- stringdistmatrix(char, char, useNames = TRUE)

close_list <- apply(X = dd, MARGIN = 1, FUN = function(x) {
    proches <- x > 0 & x < 4 
    noms_proches <- names(x)[proches]
    return(noms_proches)
}
)
    return(close_list)
} # End of function "close_string" definition
jomuller/dfcheck documentation built on May 19, 2019, 7:26 p.m.