为了账号安全,请及时绑定邮箱和手机立即绑定

自动生成正则表达式

自动生成正则表达式

临摹微笑 2021-03-08 15:20:13
我想要以下功能:def get_pattern_and_replacement(the_input, output):    """    Given the_input and output returns the pattern for matching more general case of the_input and a template string for generating the desired output.    >>> get_pattern_and_replacement("You're not being nice to me.", "I want to be treated nicely.")    ("You're not being (?P<word>\w+) to me.", "I want to be treated {{ word }}ly.")    >>> get_pattern_and_replacement("You're not meeting my needs.", "I want my needs met.")    ("You're not meeting my (?P<word>\w+).", "I want my {{ word }} met.")    """这是用于将不需要的文本转换为所需文本的程序。在Stackoverflow用户的帮助下,我的功能现在是:def flatten(nested_list):    return [item for sublist in nested_list for item in sublist]def get_pattern_and_replacement(the_input, output):    """    Given the_input and output returns the pattern for matching more general case of the_input and a template string for generating the desired output.    >>> get_pattern_and_replacement("You're not being nice to me.", "I want to be treated nicely.")    ("You're not being (?P<word>\w+) to me.", "I want to be treated {{ word }}ly.")    >>> get_pattern_and_replacement("You're not meeting my needs.", "I want my needs met.")    ("You're not meeting my (?P<word>\w+).", "I want my {{ word }} met.")    """    input_set = set(flatten([[the_input[i: i + j] for i in range(len(the_input) - j) if not ' ' in the_input[i: i + j]] for j in range(3, 12)]))    output_set = set(flatten([[output[i: i + j] for i in range(len(the_input) - j) if not ' ' in output[i: i + j]] for j in range(3, 12)]))    intersection = input_set & output_set    intersection = list(intersection)    intersection = sorted(intersection, key=lambda x: len(x))[::-1]    print intersection    pattern = the_input.replace(intersection[0], '(?P<word>\w+)')    replacement = output.replace(intersection[0], '{{ word }}')    return (pattern, replacement)
查看完整描述

1 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

如果您想要这种模板转换,则必须自己编写。认识公共部分是常识,实践和创造力的问题;没有任何一般规则可以为您做到这一点。但是,您必须阅读有关正则表达式的教程,它可能会帮助您思考问题。

您可能应该检查一下著名的聊天机器人Eliza的源代码。这是python版本的源代码。如您所见,会话规则是手写的。

如果您希望使用一种算法来生成类似于您所包含的示例的模板:这是一个非常非常困难的问题,没有一个合理的解决方案。算了吧。请改为阅读regexp教程。


查看完整回答
反对 回复 2021-03-24
  • 1 回答
  • 0 关注
  • 313 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号