我必须为nltk
中的list
创建语法python
。我有这个语法的文字:
grammar1 = nltk.CFG.fromstring("""
S -> NP VP
VP -> V NP | V NP PP
PP -> P NP
V -> "saw" | "ate" | "walked"
NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
Det -> "a" | "an" | "the" | "my"
N -> "man" | "dog" | "cat" | "telescope" | "kitchen"
P -> "in" | "on" | "by" | "with"
""")
sent = "the cat ate a telescope in the kitchen".split()
rd_parser = nltk.RecursiveDescentParser(grammar1)
for tree in rd_parser.parse(sent):
print(tree)
现在,我如何为list
做同样的事情?我需要用基本语法测试合法和非法的list
。我没有找到关于nltk
和列表的任何情报,我真的不明白我该怎么做......
答案 0 :(得分:0)
请注意,以下代码行已经创建了一个列表(字符串)。
sent = "the cat ate a telescope in the kitchen".split()
您还使用以下行为您的语法创建了一个递归下降解析器。请注意,您只需要执行一次。
rd_parser = nltk.RecursiveDescentParser(grammar1)
现在,如果您想测试不同的令牌列表,只需执行以下操作:
L = ["John", "walked", "the", "dog"]
result = rd_parser.parse(L)
答案 1 :(得分:0)
您有一个可以应用于令牌列表的解析器。您有不同格式的测试材料集合。从您的comment:"空列表,带有一个令牌的列表,带有多个令牌的列表,带有数字,元组和字典的列表中引用。"
解析器可以处理"序列"字符串,在您的情况下意味着一个列表或元组,其元素是字符串(每个字符串是一个单词)。解析器无法处理任何其他内容;如果你的代码必须处理其他类型,编写python代码以在解析器看到它们之前检查它们的类型。
您会对内置函数isinstance()
(首选)和type()
感兴趣。如,
if (isinstance(sent, (tuple, list)) and all(isinstance(w, str) for w in sent)):
# A tuple or list of strings; try to parse it.
trees = rd_parser.parse(sent)