from nltk import CFG
import nltk
from nltk.chunk.regexp import *
from nltk.parse.stanford import StanfordParser
# import re
# nltk.download('maxent_ne_chunker')
# nltk.download('words')
# nltk.download('ieer')
# 语法分析
toy_grammar = nltk.CFG.fromstring(
"""
S -> NP VP
VP -> V NP
V -> "eats" | "drinks"
NP -> Det N
Det -> "a" | "an" | "the"
N -> "president" |"Obama" |"apple"| "coke"
""")
toy_grammar.productions()
print(toy_grammar.productions())
# S indicate the entire sentence
# VP is verb phrase the
# NP is noun phrase (chunk that has noun in it)
# N some example nouns
# V is verb
# Det is determiner used in the sentences
# 正则表达式语法分析器
chunk_rules = ChunkRule("<.*>+", "chunk everything")
# NP
# Preposition
# Verb
# PP
NLTK 对文本结构进行语法分析代码示例
最新推荐文章于 2024-06-25 17:53:03 发布
本文通过实例代码展示如何利用Python的NLTK库对文本进行语法分析,包括词法分析、句法分析等,帮助理解句子的结构和成分。
摘要由CSDN通过智能技术生成