当前位置: 首页 > news >正文

R -- 体验 stringdist

文章目录

  • 安装
  • 使用
    • stringdist :返回列表
      • example
    • stringdistmatrix :返回矩阵
      • example
  • amatch & ain
  • 延伸:距离计算公式
      • Hamming distance
      • Longest Common Substring distance
      • Levenshtein distance (weighted)
      • The optimal string alignment distance dosa
      • Full Damerau-Levenshtein distance (weighted)
      • Q-gram distance
      • Jaccard distance for q-gram count vectors (= 1-Jaccard similarity)
      • cosine distance for q-gram count vectors (= 1-cosine similarity)
    • At last

安装

install.packages('stringdist')

or

git clone https://github.com/markvanderloo/stringdist.git
cd stringdist
bash ./build.bash
R CMD INSTALL output/stringdist_*.tar.gz

使用

The package offers the following main functions:

  • stringdist computes pairwise distances between two input character vectors (shorter one is recycled)
  • stringdistmatrix computes the distance matrix for one or two vectors
  • stringsim computes a string similarity between 0 and 1, based on stringdist
  • amatch is a fuzzy matching equivalent of R’s native match function
  • ain is a fuzzy matching equivalent of R’s native %in% operator
  • afind finds the location of fuzzy matches of a short string in a long string.
  • seq_dist, seq_distmatrix, seq_amatch and seq_ain for distances between, and matching of integer sequences.

stringdist :返回列表

stringdist(a,b,method = c("osa", "lv", "dl", "hamming", "lcs", "qgram", "cosine", "jaccard", "jw","soundex"),useBytes = FALSE,weight = c(d = 1, i = 1, s = 1, t = 1),q = 1,p = 0,bt = 0,nthread = getOption("sd_num_thread")
)
a	:R object (target); will be converted by as.characte
b	 :R object (source); will be converted by as.character This argument is optional for stringdistmatrix (see section Value).
method	 :Method for distance calculation. 
useBytes	:Perform byte-wise comparison
weight	:For method='osa' or 'dl', the penalty for deletion, insertion, substitution and transposition, in that order. When method='lv', the penalty for transposition is ignored.When method='jw', the weights associated with characters of a, characters from b and the transposition weight, in that order. Weights must be positive and not exceed 1. weight is ignored completely when method='hamming', 'qgram', 'cosine', 'Jaccard', 'lcs', or soundex.q	:Size of the q-gram; must be nonnegative. Only applies to method='qgram', 'jaccard' or 'cosine'.
p	:Prefix factor for Jaro-Winkler distance. The valid range for p is 0 <= p <= 0.25.If p=0 (default), the Jaro-distance is returned. Applies only to method='jw'.
bt	:Winkler's boost threshold. Winkler's prefix factor is only applied when the Jaro distance is larger than bt. Applies only to method='jw' and p>0.
useNames	:Use input vectors as row and column names?

example

注意:String distance functions have two possible special output values.
NA is returned whenever at least one of the input strings to compare is NA .
And Inf is returned when the distance between two strings is undefined according to the selected algorithm.

stringdist("bar","foo",method = "lv") #使用的是Levenshtein distance  & return  3
stringdist("ba","foo",method = "lv") #使用的是Levenshtein distance  &  return  3 ,注意这里是不等长的序列stringdist('fu', 'foo', method='hamming') # 使用的是 Hamming distance &  return Inf

stringdistmatrix :返回矩阵

stringdistmatrix(a,b,method = c("osa", "lv", "dl", "hamming", "lcs", "qgram", "cosine", "jaccard", "jw","soundex"),useBytes = FALSE,weight = c(d = 1, i = 1, s = 1, t = 1),q = 1,p = 0,bt = 0,useNames = c("none", "strings", "names"),nthread = getOption("sd_num_thread")
)
Arg

example

- 只输入一个vertor:返回一个 dist函数的结果

在这里插入图片描述

- 输入两个vector :返回矩阵

在这里插入图片描述


amatch & ain

  • Function amatch(x,table) finds the closest match of elements of x in table. When multiple equivalent matches are found, the
    first match is returned
  • A call to ain(x,table) returns a logical vector indicating which elements of x were (approximately) matched in table.
  • Both amatch and ain have been designed to approach the behaviour of R’s native match and %in% functionality as much as possible. By default amatch and ain locate exact matches, just like match.
  • This may be changed by increasing the maximum string distance between the search pattern and elements of the lookup table.

amatch仿照R base function match进行设计,通过 参数maxDist控制该函数的行为,如果maxDist 设置的很小其表现近似于 exact match,当 maxDist 设置的比较大时则表现的是approximately match。amtch 与 ain的区别类似于match和 %in%,一个返回元素的index,一个返回TRUE/FALSE。

amatch('fu', c('foo','bar')) # return NA
amatch('fu', c('foo','bar'), maxDist=2) # return 1ain('fu', c('foo','bar')) # return FALSE
ain('fu', c('foo','bar'), maxDist=2) # return  TRUE
ain('bar', c('foo','bar')) # return TRUE
ain('bar', c('foo','bar'), maxDist=2) # return TRUE

延伸:距离计算公式

在这里插入图片描述

Hamming distance

在这里插入图片描述
在这里插入图片描述

Longest Common Substring distance

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Levenshtein distance (weighted)

在这里插入图片描述
在这里插入图片描述

The optimal string alignment distance dosa

在这里插入图片描述

Full Damerau-Levenshtein distance (weighted)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
注意,Dosa 和Ddl的区别主要是最后一个方程式,Dosa只允许前后相邻的两个字符串置换,Ddl则允许当前的字符串和其他的字符置换后计算距离



在这里插入图片描述

Q-gram distance

在这里插入图片描述

Jaccard distance for q-gram count vectors (= 1-Jaccard similarity)

在这里插入图片描述

cosine distance for q-gram count vectors (= 1-cosine similarity)

在这里插入图片描述

  • Jaro distance
    在这里插入图片描述
    在这里插入图片描述

At last

在这里插入图片描述

相关文章:

  • 【备忘录】SpringBoot+ dynamic-datasource配置自定义多数据源
  • 信号灯集,消息队列
  • 在Linux上编译gdal3.1.2指南
  • 自定义的卷积神经网络模型CNN,对图片进行分类并使用图片进行测试模型-适合入门,从模型到训练再到测试,开源项目
  • 计算机毕业设计选题推荐-超市售货微信小程序/安卓APP-项目实战
  • STM32:I²C通信原理概要
  • 可视化 | 数据可视化降维算法梳理
  • gorilla/websocket的chat示例代码简单分析
  • Web3公链之Cosmos生态的项目Celestia
  • Stable Diffusion系列(一):古早显卡上最新版 WebUI 安装及简单操作
  • Redis Functions 介绍(一)
  • go中“哨兵错误”的由来及使用建议
  • Docker compose容器编排
  • Python 自动化(十六)静态文件处理
  • 在跑腿App系统开发中,如何构建系统架构?
  • [Vue CLI 3] 配置解析之 css.extract
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 【css3】浏览器内核及其兼容性
  • 230. Kth Smallest Element in a BST
  • 4个实用的微服务测试策略
  • AWS实战 - 利用IAM对S3做访问控制
  • conda常用的命令
  • nfs客户端进程变D,延伸linux的lock
  • Redis在Web项目中的应用与实践
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • SpringBoot 实战 (三) | 配置文件详解
  • Vue--数据传输
  • win10下安装mysql5.7
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 编写符合Python风格的对象
  • 初识 webpack
  • 高度不固定时垂直居中
  • 那些年我们用过的显示性能指标
  • 前端面试之闭包
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
  • 微信小程序实战练习(仿五洲到家微信版)
  • 用jQuery怎么做到前后端分离
  • 正则学习笔记
  • 正则与JS中的正则
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • 阿里云API、SDK和CLI应用实践方案
  • 阿里云重庆大学大数据训练营落地分享
  • 国内开源镜像站点
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • 选择阿里云数据库HBase版十大理由
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #单片机(TB6600驱动42步进电机)
  • (3)STL算法之搜索
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第5节(封闭类和Final方法)
  • (ros//EnvironmentVariables)ros环境变量
  • (二)Linux——Linux常用指令
  • (附源码)php投票系统 毕业设计 121500