在任意python序列上计算编辑距离。
Edit_Distance的Python项目详细描述
编辑距离
用于计算序列之间编辑距离和对齐的python模块。
我需要一种方法来计算python中序列之间的编辑距离。我没有 能够找到任何合适的库来实现这一点,所以我自己编写了一个。在那里 似乎有许多可用于计算编辑的编辑距离库 两个字符串之间的距离,但不是两个序列之间的距离。
这完全是用python编写的。这种实现可能是 在python中优化为更快。如果 在C中实现。
库API是根据difflib.sequencematcher建模的。这很相似 到difflib,除了此模块计算编辑距离(levenshtein 而不是python的difflib的ratcliff和oberhelp方法 使用。difflib“不产生最小的编辑序列,但是倾向于产生 与人的“正确外观”匹配。“
如果你觉得这个图书馆有用或有什么建议,请给我寄一个 信息。
安装和卸载
最简单的安装方法是使用pip:
pip install edit_distance
或者,您可以克隆此git repo并使用distutils安装:
git clone git@github.com:belambert/edit_distance.git
cd edit_distance
python setup.py install
使用pip卸载:
pip uninstall edit_distance
API使用
要查看用法示例,请查看difflib documentation。 有关ReadTheDocs
这需要python 2.7+,因为它使用argparse作为命令行 接口。剩下的代码对于早期版本的python应该没问题
API使用示例:
importedit_distanceref=[1,2,3,4]hyp=[1,2,4,5,6]sm=edit_distance.SequenceMatcher(a=ref,b=hyp)sm.get_opcodes()sm.ratio()sm.get_matching_blocks()
与difflib的区别
除了SequenceMatcher
方法之外,distance()
和matches()
方法
提供用于计算编辑距离和匹配数的。
sm.distance()sm.matches()
即使两个序列的比对与difflib
相同,
get_opcodes()
和get_matching_blocks()
可能返回略有不同
顺序。此库返回的操作码表示单个字符
操作,因此不应跨越两个或多个字符。
sm=edit_distance.SequenceMatcher(a=ref,b=hyp,action_function=edit_distance.highest_match_action)
注释
这不会在difflib中实现“垃圾”匹配功能。
贡献和行为准则
对于贡献,最好是github问题和拉取请求。适当的 建议的测试和文档。
行为准则应该是合理的,特别是 Contributor Covenant