python正则表达式模糊匹配_Python正则表达式模块模糊匹配:替换计数不符合预期...

Background

The Python module regex allows fuzzy matching.

You can specify the allowable number of substitutions (s), insertions (i), deletions (d), and total errors (e).

The fuzzy_counts property of a match result returns a tuple (0,0,0), where:

match.fuzzy_counts[0] = count for 's'

match.fuzzy_counts[1] = count for 'i'

match.fuzzy_counts[2] = count for 'd'

Problem

The deletions and insertions are counted as expected, but not the substitutions.

In the example below, the only change is a single character deleted in the query, yet the substitutions count is 6 (7 if the BESTMATCH option is removed).

How are the substitutions counted?

I would be grateful of someone can anyone explain how this works to me.

>>> import regex

>>> reference = "(TATGGGA[CT][GC]AAAG[CT]CT[AC]AA[GA]CCATGTG){s<7,i<3,d<3,e<8}"

>>> query = "TATGGACCAAAGTCTCAAGCCATGTG"

>>> match = regex.search(reference, query, regex.BESTMATCH)

>>> print(match.fuzzy_counts)

(6,0,1)

解决方案

This was caused by what looks to be a bug in the regex module's cost calculations. It was still present up until regex version 2015.10.05, but was fixed in the next version, 2015.10.22, as shown below:

$ sudo pip3 install regex==2015.10.05

Processing /root/.cache/pip/wheels/24/cb/ae/9653e30c8f801544a645e17d26fa6803aeaf76ad0482663c27/regex-2015.10.5-cp38-cp38-linux_x86_64.whl

Installing collected packages: regex

Successfully installed regex-2015.10.5

$ python3 -c 'import regex; reference = "(TATGGGA[CT][GC]AAAG[CT]CT[AC]AA[GA]CCATGTG){s<7,i<3,d<3,e<8}"; query = "TATGGACCAAAGTCTCAAGCCATGTG"; match = regex.search(reference, query, regex.BESTMATCH);print(match.fuzzy_counts)'

(5, 0, 1)

$ sudo pip3 install regex==2015.10.22

Processing /root/.cache/pip/wheels/60/f6/9a/23e723633e62a79064cb301c54a3b50482b8c690f86c9983ee/regex-2015.10.22-cp38-cp38-linux_x86_64.whl

Installing collected packages: regex

Found existing installation: regex 2015.10.5

Uninstalling regex-2015.10.5:

Successfully uninstalled regex-2015.10.5

Successfully installed regex-2015.10.22

$ python3 -c 'import regex; reference = "(TATGGGA[CT][GC]AAAG[CT]CT[AC]AA[GA]CCATGTG){s<7,i<3,d<3,e<8}"; query = "TATGGACCAAAGTCTCAAGCCATGTG"; match = regex.search(reference, query, regex.BESTMATCH);print(match.fuzzy_counts)'

(0, 0, 1)

Hg issue 161: Unexpected fuzzy match results

Fixed the bug and did some related tidying up.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值