繁体   English   中英

如何替换文本中的字符串列表,其中一些字符串是python中其他字符串的子串?

[英]How to replace a list of strings in a text where some of them are substrings of other in python?

我有一个包含我想要标记的单词的文本,要标记的单词包含在List中。 问题是其中一些单词是其他单词的子串,但我想从列表中标记最长的识别字符串。

例如,如果我的文字是“foo和bar与foo bar不同”。 我的列表包含“foo”,“bar”和“foo bar”,结果应为“[tag] foo [/ tag]和[tag] bar [/ tag]与[tag] foo bar [/ tag]不同“。

text = "foo and bar are different from foo bar."
words = ["foo", "bar", "foo bar"]

tagged = someFunction(text, words)

someFunction的代码应该是什么,使得字符串taggedText的值为"<tag>foo</tag> and <tag>bar</tag> are different from <tag>foo bar</tag>."

如果我理解你的问题,那么这就是你要找的东西: -

text = "foo and bar are different from foo bar."
words = ["foo", "bar", "foo bar"]

add_tag = lambda var : "<tag>"+var+"</tag>"

result = ''    # for final string
for var in text.split():
    if var in words:
        tmp = add_tag(var)
    else:
        tmp = var
    result += " "+tmp

print result    
return result

这里add_tag()方法,服务你都看在someFunction

实现这一目标的一种简单方法是按相反的顺序按长度对words进行排序,然后创建正则表达式word1|word2|... 由于重新引擎始终进行第一次匹配,因此将首先捕获更长的字符串。

import re

def tag_it(text, words):
    return re.sub(
            '|'.join(sorted(words, key=len, reverse=True)),
            lambda m: '<tag>' + m.group(0) + '</tag>',
            text)


text = "foo and bar are different from foo bar."
words = ["foo", "bar", "foo bar"]


print tag_it(text, words)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM