繁体   English   中英

如何使用 Python 来标记句子字符串中的单词,具体取决于它们是否在一个特定单词之后和句号之前?

[英]How can use Python to mark words in a sentence string depending on whether they come after one specific word and before a full stop?

我有一个包含职位描述的字符串列表,如下所示:

direct or coordinate an organization's financial or budget activities to fund operations, maximize investments, or increase efficiency. may serve as liaisons between organizations, shareholders, and outside organizations. may attend and participate in meetings of municipal councils or council committees. represent organizations or promote their objectives at official functions, or delegate representatives to do so.

我已经有一些 python 代码将描述中的每个单词分开,并赋予它一些属性,例如它在描述中出现的次数,它的 position(就数字排名而言)或其 POS 标签(无论是名词、动词等)。 例如,如果工作描述只是“计划时间表”,我的程序已经可以给我以下内容:

[('plan', 'plan', 'NN', 0, 2, 5, 'construction managers', '11-9021.00', 245), ('schedule', 'schedul', 'NN', 1, 1, 1, 'construction managers', '11-9021.00', 245)]

我想为此添加一个标志/布尔值,它将突出显示定义中的每个单词,它是否出现单词“可能”之后和句号之前 本质上,我会寻找每个描述的布尔值列表,我可以将其 zip 作为上述结构的第 10 个属性,并知道每个单词是否介于“可能”和句号之间。

关于如何实现这一目标的任何建议?

我假设您想找到出现在单词“may”和句号之间的任何地方的关键字,即是否允许某人执行某项任务。

编译完关键字列表后,您可以使用正则表达式re库来搜索匹配的字符串。

如果在字符串中找到正则表达式,则re.search方法返回 Match object,否则返回None 但是这两种情况也可以转换为 boolean 变量:

import re
def may_matcher(string, keyword):
    return bool(re.search(r'may\s(\w*\s)*'+keyword+'\s*(\w*\s)*\w*\.',string))

应用这个小 function 会给你想要的 boolean:

string = "may attend to guests."
may_matcher(string, "attend")
may_matcher(string, "help")

第一行计算为True而第二行计算为False

然后,您可以通过所有关键字对 go 使用列表理解:

keywords = ["attend", "help"]
may_list = [may_matcher(string,keyword) for keyword in keywords]

需要注意的是,要注意否定句:如果这样的句子也存在,那么这个 function 也会匹配带有“may not”的句子。 您将不得不修改正则表达式。

暂无
暂无

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

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