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 1

ain('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

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/111518.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

阿里云2核2G3M带宽轻量服务器87元一年,经济型e实例99元一年

2023阿里云双十一优惠活动2核2G3M轻量应用服务器一年优惠价87元&#xff0c;云服务器ECS经济型e实例优惠价格99元一年&#xff0c;也是2核2G配置&#xff0c;自带3M带宽&#xff0c;并且续费不涨价&#xff0c;阿里云百科aliyunbaike.com还是很建议大家选择e实例的&#xff0c;…

什么测试自动化测试?

什么测试自动化测试&#xff1f; 做测试好几年了&#xff0c;真正学习和实践自动化测试一年&#xff0c;自我感觉这一个年中收获许多。一直想动笔写一篇文章分享自动化测试实践中的一些经验。终于决定花点时间来做这件事儿。 首先理清自动化测试的概念&#xff0c;广义上来讲&a…

小说网站源码带管理后台手机端和采集

搭建教程&#xff0c;安装宝塔 php7.2&#xff0c;绑定域名&#xff0c;上传源码到根目录解压 源码获取请自行百度&#xff1a;一生相随博客 一生相随博客致力于分享全网优质资源&#xff0c;包括网站源码、游戏源码、主题模板、插件、电脑软件、手机软件、技术教程等等&#…

AI:47-基于深度学习的人像背景替换研究

🚀 本文选自专栏:AI领域专栏 从基础到实践,深入了解算法、案例和最新趋势。无论你是初学者还是经验丰富的数据科学家,通过案例和项目实践,掌握核心概念和实用技能。每篇案例都包含代码实例,详细讲解供大家学习。 📌📌📌本专栏包含以下学习方向: 机器学习、深度学…

xcode 安装及运行个人app编程应用

1.xcode 介绍 Xcode 是运行在操作系统Mac OS X上的集成开发工具&#xff08;IDE&#xff09;&#xff0c;由Apple Inc开发。Xcode是开发 macOS 和 iOS 应用程序的最快捷的方式。Xcode 具有统一的用户界面设计&#xff0c;编码、测试、调试都在一个简单的窗口内完成 2.xcode 下…

iOS开发-CoreNFC实现NFC标签Tag读取功能

iOS开发-CoreNFC实现NFC标签Tag读取功能 一、NFC近场通信 近场通信&#xff08;NFC&#xff09;是一种无线通信技术&#xff0c;它使设备能够在不使用互联网的情况下相互通信。它首先识别附近配备NFC的设备。NFC常用于智能手机和平板电脑。 二、实现NFC标签Tag读取功能 在…

机器学习2:决策树--基于信息增益的ID3算法

1.决策树的简介 建立决策树的过程可以分为以下几个步骤: 计算每个特征的信息增益或信息增益比,选择最优的特征作为当前节点的划分标准。根据选择的特征将数据集划分为不同的子集。对每个子集递归执行步骤 1 和步骤 2,直到满足终止条件。构建决策树,并输出。基于信息增益的…

PP-MobileSeg: 探索移动设备上又快又准的语义分割模型

论文&#xff1a;https://arxiv.org/abs/2304.05152 代码&#xff1a;https://github.com/open-mmlab/mmsegmentation/tree/main/projects/pp_mobileseg 0、摘要 transformer在CV领域的成功之后&#xff0c;出现了很多在移动设备上使用它们的尝试性工作&#xff0c;但是这些工作…

一道简单的C#面试题

试题&#xff1a; 抽顺序问题&#xff1a;有10位面试者&#xff0c;需要随机抽号面试。 1&#xff09;总共十个号数&#xff0c;用数组表示&#xff1b; 2&#xff09;每一位面试者输入1开始抽签&#xff0c;然后得到抽签号&#xff0c;输入2结束抽签&#xff1b; 3&#x…

超融合数据库:解锁全场景数据价值的钥匙

前言 近日&#xff0c;四维纵横对外官宣已完成上亿元 B 轮融资。作为超融合数据库理念的提出者&#xff0c;三年来 YMatrix 持续在超融合数据库领域中保持精进与迭代&#xff0c;对于超融合数据库在行业、场景中的应用和理解也更为深刻。 本篇文章&#xff0c;我们将基于 YMa…

HTML标题、段落、文本格式化

HTML标题&#xff1a; 在HTML文档中&#xff0c;标题是很重要的。标题是通过<h1> - <h6标签进行定义的&#xff0c;<h1> 定义最大的标题&#xff1b;<h6>定义最小的标题。 <hr> 标签在HTML页面中用于创建水平线&#xff0c;hr元素可用于分隔内容。…

springboot整合日志,并在本地查看

目录 1.导入依赖 2.编写配置 3.使用 4.验证 5.打印错误信息 1.导入依赖 <!-- logback&#xff0c;向下兼容log4j,还支持SLF4J--> <dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId> </depen…

【Docker】Linux路由连接两个不同网段namespace,连接namespace与主机

如果两个namespace处于不同的子网中&#xff0c;那么就不能通过bridge进行连接了&#xff0c;而是需要通过路由器进行三层转发。然而Linux并未像提供虚拟网桥一样也提供一个虚拟路由器设备&#xff0c;原因是Linux自身就具备有路由器功能。 路由器的工作原理是这样的&#xff…

MySQL操作命令整理

MySQL操作命令整理 SQL分类 SQL语句按照其功能范围不同可分为3个类别: 数据定义语言(DDL ,Data Defintion Language)语句:数据定义语句,用于定义不同的数据段、数据库、表、列、索引等。常用的语句关键字包括create、drop、alter等。数据操作语言(DML , Data Manipulatio…

历年上午真题笔记(2014年)

解析:A 网络设计的三层模型 : 接入层:Layer 2 Switching,最终用户被许可接入网络的点,用户通过接入层可以访问网络设备。 汇聚层:Layer2/3 Switching,访问层设备的汇聚点,负责汇接配线单元,利用二、三层技术实现工作组分段及网络故障的隔离,以免对核心层网络设备造…

Istio实战(九)-Envoy 流量劫持

前言 Envoy 是一款面向 Service Mesh 的高性能网络代理服务。它与应用程序并行运行,通过以平台无关的方式提供通用功能来抽象网络。当基础架构中的所有服务流量都通过 Envoy 网格时,通过一致的可观测性,很容易地查看问题区域,调整整体性能。 Envoy也是istio的核心组件之一…

java参数中的-、--、-X、-XX、-D

详细描述请参考&#xff1a;https://docs.oracle.com/en/java/javase/19/docs/specs/man/java.html Java标准选项&#xff08;以-、或者–开头&#xff09; Java标准选项被所有的Java虚拟机&#xff08;JVM&#xff09;实现所支持。 这些选项用于普通的动作&#xff0c;例如检…

某国产中间件企业:提升研发安全能力,助力数字化建设安全发展

​某国产中间件企业是我国中间件领导者&#xff0c;国内领先的大安全及行业信息化解决方案提供商&#xff0c;为各个行业领域近万家企业客户提供先进的中间件、信息安全及行业数字化产品、解决方案及服务支撑&#xff0c;致力于构建安全科学的数字世界&#xff0c;帮助客户实现…

【地理位置识别】IP归属地应用的特点

IP归属地应用是一类用于确定特定IP地址的地理位置信息&#xff08;通常是城市、地区或国家&#xff09;的工具和服务。以下是IP归属地应用的几个主要特点&#xff1a; 地理位置识别&#xff1a; IP归属地应用主要用于确定IP地址的地理位置。这可以帮助组织更好地了解其网站访问…

<多线程章节八> 单例模式中的饿汉模式与懒汉模式的讲解,以及懒汉模式中容易引起的Bug

&#x1f490;专栏导读 本篇文章收录于多线程&#xff0c;也欢迎翻阅博主的其他文章&#xff0c;可能也会让你有不一样的收获&#x1f604; &#x1f337;JavaSE &#x1f342;多线程 &#x1f33e;数据结构 文章目录 &#x1f490;专栏导读&#x1f4a1;饿汉模式&#x1f4a1;…
最新文章