当前位置:   article > 正文

NLTK 对文本结构进行语法分析代码示例_nltk.download('words')

nltk.download('words')
  1. from nltk import CFG
  2. import nltk
  3. from nltk.chunk.regexp import *
  4. from nltk.parse.stanford import StanfordParser
  5. # import re
  6. # nltk.download('maxent_ne_chunker')
  7. # nltk.download('words')
  8. # nltk.download('ieer')
  9. # 语法分析
  10. toy_grammar = nltk.CFG.fromstring(
  11. """
  12. S -> NP VP
  13. VP -> V NP
  14. V -> "eats" | "drinks"
  15. NP -> Det N
  16. Det -> "a" | "an" | "the"
  17. N -> "president" |"Obama" |"apple"| "coke"
  18. """)
  19. toy_grammar.productions()
  20. print(toy_grammar.productions())
  21. # S indicate the entire sentence
  22. # VP is verb phrase the
  23. # NP is noun phrase (chunk that has noun in it)
  24. # N some example nouns
  25. # V is verb
  26. # Det is determiner used in the sentences
  27. # 正则表达式语法分析器
  28. chunk_rules = ChunkRule("<.*>+", "chunk everything")
  29. # NP
  30. # Preposition
  31. # Verb
  32. # PP
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号