Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式测试句子是否有效_Python_Regex_Compiler Construction - Fatal编程技术网

Python正则表达式测试句子是否有效

Python正则表达式测试句子是否有效,python,regex,compiler-construction,Python,Regex,Compiler Construction,通过应用上述规则,我可以生成 ACTIVE_LIST = ACTOR | ACTIVE_LIST and ACTOR ACTOR = NOUN | ARTICLE NOUN ARTICLE = a | the NOUN = tom | jerry | goofy | mickey | jimmy | dog | cat | mouse 但不是 a tom tom and a jerry the tom and a jerry the tom and a jerry and tom and

通过应用上述规则,我可以生成

ACTIVE_LIST = ACTOR | ACTIVE_LIST and ACTOR
ACTOR = NOUN | ARTICLE NOUN
ARTICLE = a | the
NOUN = tom | jerry | goofy | mickey | jimmy | dog | cat | mouse
但不是

a tom 
tom and a jerry 
the tom and a jerry 
the tom and a jerry and tom and dog
我可以只使用pythonre模块检查句子是否正确吗。我知道如何通过[abc]匹配某些字符,但不知道word。 事实上,我正在努力解决这个问题。如果有人帮我一部分,我可以做其余的。 这是我在这个竞技场上的第一个问题。高度赞赏任何建议或改进

使用re.compile

Tom 
the Tom and me
在下面的主题中,您将有其他方法不用重新编译。(搜索/匹配)

使用重新编译

Tom 
the Tom and me
在下面的主题中,您将有其他方法不用重新编译。(搜索/匹配)


这可以看作是NLP(自然语言处理)问题。有一个称为NLTK(Natural Language Toolkit)的特殊python模块,可以最好地用于解决此任务,比使用正则表达式更容易完成

1) 首先,您需要下载NLTK()

2) 导入NLTK:

re.compile('tom', re.IGNORECASE)
3) 创建一个小语法,一个包含四条规则的上下文无关语法()。通过NLTK中的CFG模块,您可以通过一行代码轻松完成这一任务:

import nltk
4) 创建将使用acm_语法的解析器:

acm_grammar = nltk.CFG.fromstring("""
ACTIVE_LIST -> ACTOR | ACTIVE_LIST 'and' ACTOR
ACTOR -> NOUN | ARTICLE NOUN
ARTICLE -> 'a' | 'the'
NOUN -> 'tom' | 'jerry' | 'goofy' | 'mickey' | 'jimmy' | 'dog' | 'cat' | 'mouse' """)
5) 在一些输入上测试它。输入的句子必须是以逗号分隔的单词(字符串)列表的形式。split()方法可用于以下操作:

parser = nltk.ChartParser(acm_grammar)
在最后一步中,我们检查解析器是否可以根据acm_语法解析句子。如果不能,则对解析器的调用将导致ValueError。 以下是此代码的输出:

input= ["a tom", "tom and a jerry", "the tom and a jerry","the tom and a jerry and tom and dog","Tom", "the Tom and me"]

for sent in input:
    split_sent = sent.split()
    try:
        parser.parse(split_sent)
        print(sent,"-- YES I WILL")
    except ValueError:
        print(sent,"-- NO I WON'T")

这可以看作是一个NLP(自然语言处理)问题。有一个称为NLTK(Natural Language Toolkit)的特殊python模块,可以最好地用于解决此任务,比使用正则表达式更容易完成

1) 首先,您需要下载NLTK()

2) 导入NLTK:

re.compile('tom', re.IGNORECASE)
3) 创建一个小语法,一个包含四条规则的上下文无关语法()。通过NLTK中的CFG模块,您可以通过一行代码轻松完成这一任务:

import nltk
4) 创建将使用acm_语法的解析器:

acm_grammar = nltk.CFG.fromstring("""
ACTIVE_LIST -> ACTOR | ACTIVE_LIST 'and' ACTOR
ACTOR -> NOUN | ARTICLE NOUN
ARTICLE -> 'a' | 'the'
NOUN -> 'tom' | 'jerry' | 'goofy' | 'mickey' | 'jimmy' | 'dog' | 'cat' | 'mouse' """)
5) 在一些输入上测试它。输入的句子必须是以逗号分隔的单词(字符串)列表的形式。split()方法可用于以下操作:

parser = nltk.ChartParser(acm_grammar)
在最后一步中,我们检查解析器是否可以根据acm_语法解析句子。如果不能,则对解析器的调用将导致ValueError。 以下是此代码的输出:

input= ["a tom", "tom and a jerry", "the tom and a jerry","the tom and a jerry and tom and dog","Tom", "the Tom and me"]

for sent in input:
    split_sent = sent.split()
    try:
        parser.parse(split_sent)
        print(sent,"-- YES I WILL")
    except ValueError:
        print(sent,"-- NO I WON'T")

经过深思熟虑,我自己解决了这个问题

a tom -- YES I WILL
tom and a jerry -- YES I WILL
the tom and a jerry -- YES I WILL
the tom and a jerry and tom and dog -- YES I WILL
Tom -- NO I WON'T
the Tom and me -- NO I WON'T

经过深思熟虑,我自己解决了这个问题

a tom -- YES I WILL
tom and a jerry -- YES I WILL
the tom and a jerry -- YES I WILL
the tom and a jerry and tom and dog -- YES I WILL
Tom -- NO I WON'T
the Tom and me -- NO I WON'T

是的,您可以将其作为正则表达式模式编写,因为语法是规则的。正则表达式将相当长,但它可以以相当直接的方式生成;一旦有了正则表达式,您只需编译它并将其应用于每个输入

关键是把规则化为重复。比如说,

ARTICLE = ( 'a', 'the')
NOUN = ('tom' , 'jerry' , 'goofy' , 'mickey' , 'jimmy' , 'dog' , 'cat' , 'mouse')

all_a = NOUN +tuple([' '.join([x,y]) for x in ARTICLE for y in NOUN])


def aseKi(str):
    return str in all_a

st = 'the tom and jerry'
st1 = 'tom and a jerry'

st2 = 'tom and jerry and the mouse'

st = 'tom and goofy and goofy and the goofy and a dog and cat'

val = st.split('and')

nice_val = [x.strip() for x in val]


s = [aseKi(x) for x in nice_val]

if all(s):
    print 'YES I WILL'
else:
    print "NO I WON'T"
可以变成

STATEMENT = ACTION | STATEMENT , ACTION
当然,这只是问题的一部分,因为首先必须将
操作
转换为正则表达式,才能为
语句
创建正则表达式

问题描述掩盖了一个重要问题,即输入不只是由小写字母和逗号组成。它还包含空格,正则表达式需要在适当的点上使用空格。例如,上面的
可能必须(当然可能)后跟一个(或多个)空格。如果前面有一个或多个空格也可以;问题描述不清楚

因此,
名词
的修正正则表达式实际上是:

ACTION (, ACTION)*

(我还发现有趣的是,上面的语法让
动词
匹配“hatesss”。我不知道这是否是有意的。)

是的,你可以把它写成正则表达式模式,因为语法是规则的。正则表达式将相当长,但它可以以相当直接的方式生成;一旦有了正则表达式,您只需编译它并将其应用于每个输入

关键是把规则化为重复。比如说,

ARTICLE = ( 'a', 'the')
NOUN = ('tom' , 'jerry' , 'goofy' , 'mickey' , 'jimmy' , 'dog' , 'cat' , 'mouse')

all_a = NOUN +tuple([' '.join([x,y]) for x in ARTICLE for y in NOUN])


def aseKi(str):
    return str in all_a

st = 'the tom and jerry'
st1 = 'tom and a jerry'

st2 = 'tom and jerry and the mouse'

st = 'tom and goofy and goofy and the goofy and a dog and cat'

val = st.split('and')

nice_val = [x.strip() for x in val]


s = [aseKi(x) for x in nice_val]

if all(s):
    print 'YES I WILL'
else:
    print "NO I WON'T"
可以变成

STATEMENT = ACTION | STATEMENT , ACTION
当然,这只是问题的一部分,因为首先必须将
操作
转换为正则表达式,才能为
语句
创建正则表达式

问题描述掩盖了一个重要问题,即输入不只是由小写字母和逗号组成。它还包含空格,正则表达式需要在适当的点上使用空格。例如,上面的
可能必须(当然可能)后跟一个(或多个)空格。如果前面有一个或多个空格也可以;问题描述不清楚

因此,
名词
的修正正则表达式实际上是:

ACTION (, ACTION)*

(我还发现,有趣的是,所展示的语法让
动词
匹配“hatesss”。我不知道这是否是有意的。)

查看文档,您可以使用re.IGNORECASE。和[A-Z]将匹配小写和大写。这不是正则表达式的最佳描述。这是一个解析问题,像这样的工具会让你看到这个问题,就像作者认为你应该看到的那样。看文档,你可以使用re.IGNORECASE。和[A-Z]将匹配小写和大写。这不是正则表达式的最佳描述。这是一个解析问题,像这样的工具会让你看到这个问题,因为作者似乎认为你应该这样做。我对你的答案投了赞成票,因为这比过去要好。tnx阅读链接。最好的答案会被接受。这有点像用大锤打苍蝇。它确实起到了作用,但如果OP(未声明)的愿望是理解解析,那就失去了。我已经投票支持你的答案,因为这比过去更好。tnx阅读链接。最好的答案会被接受,这就是答案