gpt4 book ai didi

python - 如何匹配 NLTK CFG 中的整数?

转载 作者:太空宇宙 更新时间:2023-11-04 05:54:55 25 4
gpt4 key购买 nike

如果我想定义一个语法,其中一个标记将匹配一个整数,我如何使用 nltk 的字符串 CFG 来实现它?

例如-

S -> SK SO FK
SK -> 'SELECT'
SO -> '\d+'
FK -> 'FROM'

最佳答案

这样创建一个数字短语:

import nltk

groucho_grammar = nltk.CFG.fromstring("""
S -> NP VP
PP -> P NP
NP -> Det N | Det N PP | 'I' | NUM N
VP -> V NP | VP PP
Det -> 'an' | 'my'
N -> 'elephant' | 'pajamas' | 'elephants'
V -> 'shot'
P -> 'in'
NUM -> '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10'
""")

sent = 'I shot 3 elephants'.split()
parser = nltk.ChartParser(groucho_grammar)
for tree in parser.parse(sent):
print(tree)

[输出]:

(S (NP I) (VP (V shot) (NP (NUM 3) (N elephants))))

但请注意,它只能处理个位数。因此,让我们尝试将整数压缩为单个标记类型,例如'#NUM#':

import nltk

groucho_grammar = nltk.CFG.fromstring("""
S -> NP VP
PP -> P NP
NP -> Det N | Det N PP | 'I' | NUM N
VP -> V NP | VP PP
Det -> 'an' | 'my'
N -> 'elephant' | 'pajamas' | 'elephants'
V -> 'shot'
P -> 'in'
NUM -> '#NUM#'
""")

sent = 'I shot 333 elephants'.split()
sent = ['#NUM#' if i.isdigit() else i for i in sent]

parser = nltk.ChartParser(groucho_grammar)
for tree in parser.parse(sent):
print(tree)

[输出]:

(S (NP I) (VP (V shot) (NP (NUM #NUM#) (N elephants))))

要放回数字,请尝试:

import nltk

groucho_grammar = nltk.CFG.fromstring("""
S -> NP VP
PP -> P NP
NP -> Det N | Det N PP | 'I' | NUM N
VP -> V NP | VP PP
Det -> 'an' | 'my'
N -> 'elephant' | 'pajamas' | 'elephants'
V -> 'shot'
P -> 'in'
NUM -> '#NUM#'
""")

original_sent = 'I shot 333 elephants'.split()
sent = ['#NUM#' if i.isdigit() else i for i in original_sent]
numbers = [i for i in original_sent if i.isdigit()]

parser = nltk.ChartParser(groucho_grammar)
for tree in parser.parse(sent):
treestr = str(tree)
for n in numbers:
treestr = treestr.replace('#NUM#', n, 1)
print(treestr)

[输出]:

(S (NP I) (VP (V shot) (NP (NUM 333) (N elephants))))

关于python - 如何匹配 NLTK CFG 中的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28348485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com