[1-4]语法分析
# toy CFG
>>> from nltk import CFG
>>>toy_grammar =
nltk.CFG.fromstring(
"""
S -> NP VP # S indicate the entire sentence
VP -> V NP # VP is verb phrase the
V -> "eats" | "drinks" # V is verb we are using only 2 verbs in the example
NP -> Det N # NP is noun phrase (chunk that has noun in it)
Det -> "a" | "an" | "the" # Det is determiner used in the sentences
N -> "president" |"Obama" |"apple"| "coke" # N some example nouns
""")
>>> toy_grammar.productions()# similarly a PCFG also can be built
>>> from nltk import PCFG
>>> toy_pcfg1 = PCFG.fromstring("""
S -> NP VP [1.0]
NP -> Det N [0.5] | NP PP [0.25] | 'John' [0.1] | 'I' [0.15]
Det -> 'the' [0.8] | 'my' [0.2]
N -> 'man' [0.5] | 'telescope' [0.5]
VP -> VP PP [0.1] | V NP [0.7] | V [0.2]
V -> 'ate' [0.35] | 'saw' [0.65]
PP -> P NP [1.0]
P -> 'with' [0.61] | 'under' [0.39]
""")
# ref :http://www.nltk.org/howto/grammar.html语法分析器
递归下降的语法分析器
移位归约语法分析器
图表语法分析器
正则表达式语法分析器
依存分析
组块化
信息抽取
Last updated