python最长的单词判断_python – 文本分段:将输入与字典中最长的单词匹配的算法...

基本上你的问题是树问题,在每个级别,形成树前缀的所有单词形成分支.不留下字符串一部分的分支是正确的解决方案.

thisisinsane

|

|

(this)isinsane

/ \

/ \

(this,i)sinsane (this,is)insane

/ / \

/ / \

(this,i,sin)ane (this,is,in)sane (this,is,insane)

/

/

(this,is,in,sane)

因此,在此示例中有两个解决方案,但我们希望使用最长的单词选择解决方案,即我们希望使用深度优先搜索策略从右侧探索树.

所以我们的算法应该:

>按降序长度排序字典.

>查找当前字符串的所有前缀.如果没有,则返回False.

>将前缀设置为最长的未探索前缀.

>将其从字符串中删除.如果字符串为空,我们找到了一个解决方案,返回所有前缀的列表.

>递归到2.

>此分支失败,返回False.

此解决方案的示例实现:

def segment(string,wset):

"""Segments a string into words prefering longer words givens

a dictionary wset."""

# Sort wset in decreasing string order

wset.sort(key=len, reverse=True)

result = tokenize(string, wset, "")

if result:

result.pop() # Remove the empty string token

result.reverse() # Put the list into correct order

return result

else:

raise Exception("No possible segmentation!")

def tokenize(string, wset, token):

"""Returns either false if the string can't be segmented by

the current wset or a list of words that segment the string

in reverse order."""

# Are we done yet?

if string == "":

return [token]

# Find all possible prefixes

for pref in wset:

if string.startswith(pref):

res = tokenize(string.replace(pref, '', 1), wset, pref)

if res:

res.append(token)

return res

# Not possible

return False

print segment("thisisinsane", ['this', 'is', 'in', 'insane']) # this is insane

print segment("shareasale", ['share', 'a', 'sale', 'as']) # share a sale

print segment("asignas", ['as', 'sign', 'a']) # a sign as

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值