【发布时间】:2016-08-25 14:48:16
【问题描述】:
我有一个包含一些我想标记的单词的文本,并且要标记的单词包含在一个列表中。问题是其中一些单词是其他单词的子字符串,但我想标记列表中最长的识别字符串。
例如,如果我的文本是“foo and bar is different from 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)
如果字符串 taggedText 的值为"<tag>foo</tag> and <tag>bar</tag> are different from <tag>foo bar</tag>.",那么 someFunction 的代码应该是什么?
【问题讨论】:
-
你能提出你的一个想法吗?我会使用一个函数,按字符串的长度对
words的列表进行排序,然后循环遍历text,标记所有的积极因素,同时检查两个标记词是否已经存在由<tag>或</tag>字符串圈出...
标签: python regex string substring