treetransforms.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. # Natural Language Toolkit: Tree Transformations
  2. #
  3. # Copyright (C) 2005-2007 Oregon Graduate Institute
  4. # Author: Nathan Bodenstab <bodenstab@cslu.ogi.edu>
  5. # URL: <http://nltk.org/>
  6. # For license information, see LICENSE.TXT
  7. """
  8. A collection of methods for tree (grammar) transformations used
  9. in parsing natural language.
  10. Although many of these methods are technically grammar transformations
  11. (ie. Chomsky Norm Form), when working with treebanks it is much more
  12. natural to visualize these modifications in a tree structure. Hence,
  13. we will do all transformation directly to the tree itself.
  14. Transforming the tree directly also allows us to do parent annotation.
  15. A grammar can then be simply induced from the modified tree.
  16. The following is a short tutorial on the available transformations.
  17. 1. Chomsky Normal Form (binarization)
  18. It is well known that any grammar has a Chomsky Normal Form (CNF)
  19. equivalent grammar where CNF is defined by every production having
  20. either two non-terminals or one terminal on its right hand side.
  21. When we have hierarchically structured data (ie. a treebank), it is
  22. natural to view this in terms of productions where the root of every
  23. subtree is the head (left hand side) of the production and all of
  24. its children are the right hand side constituents. In order to
  25. convert a tree into CNF, we simply need to ensure that every subtree
  26. has either two subtrees as children (binarization), or one leaf node
  27. (non-terminal). In order to binarize a subtree with more than two
  28. children, we must introduce artificial nodes.
  29. There are two popular methods to convert a tree into CNF: left
  30. factoring and right factoring. The following example demonstrates
  31. the difference between them. Example::
  32. Original Right-Factored Left-Factored
  33. A A A
  34. / | \ / \ / \
  35. B C D ==> B A|<C-D> OR A|<B-C> D
  36. / \ / \
  37. C D B C
  38. 2. Parent Annotation
  39. In addition to binarizing the tree, there are two standard
  40. modifications to node labels we can do in the same traversal: parent
  41. annotation and Markov order-N smoothing (or sibling smoothing).
  42. The purpose of parent annotation is to refine the probabilities of
  43. productions by adding a small amount of context. With this simple
  44. addition, a CYK (inside-outside, dynamic programming chart parse)
  45. can improve from 74% to 79% accuracy. A natural generalization from
  46. parent annotation is to grandparent annotation and beyond. The
  47. tradeoff becomes accuracy gain vs. computational complexity. We
  48. must also keep in mind data sparcity issues. Example::
  49. Original Parent Annotation
  50. A A^<?>
  51. / | \ / \
  52. B C D ==> B^<A> A|<C-D>^<?> where ? is the
  53. / \ parent of A
  54. C^<A> D^<A>
  55. 3. Markov order-N smoothing
  56. Markov smoothing combats data sparcity issues as well as decreasing
  57. computational requirements by limiting the number of children
  58. included in artificial nodes. In practice, most people use an order
  59. 2 grammar. Example::
  60. Original No Smoothing Markov order 1 Markov order 2 etc.
  61. __A__ A A A
  62. / /|\ \ / \ / \ / \
  63. B C D E F ==> B A|<C-D-E-F> ==> B A|<C> ==> B A|<C-D>
  64. / \ / \ / \
  65. C ... C ... C ...
  66. Annotation decisions can be thought about in the vertical direction
  67. (parent, grandparent, etc) and the horizontal direction (number of
  68. siblings to keep). Parameters to the following functions specify
  69. these values. For more information see:
  70. Dan Klein and Chris Manning (2003) "Accurate Unlexicalized
  71. Parsing", ACL-03. http://www.aclweb.org/anthology/P03-1054
  72. 4. Unary Collapsing
  73. Collapse unary productions (ie. subtrees with a single child) into a
  74. new non-terminal (Tree node). This is useful when working with
  75. algorithms that do not allow unary productions, yet you do not wish
  76. to lose the parent information. Example::
  77. A
  78. |
  79. B ==> A+B
  80. / \ / \
  81. C D C D
  82. """
  83. from nltk.tree import Tree
  84. def chomsky_normal_form(
  85. tree, factor="right", horzMarkov=None, vertMarkov=0, childChar="|", parentChar="^"
  86. ):
  87. # assume all subtrees have homogeneous children
  88. # assume all terminals have no siblings
  89. # A semi-hack to have elegant looking code below. As a result,
  90. # any subtree with a branching factor greater than 999 will be incorrectly truncated.
  91. if horzMarkov is None:
  92. horzMarkov = 999
  93. # Traverse the tree depth-first keeping a list of ancestor nodes to the root.
  94. # I chose not to use the tree.treepositions() method since it requires
  95. # two traversals of the tree (one to get the positions, one to iterate
  96. # over them) and node access time is proportional to the height of the node.
  97. # This method is 7x faster which helps when parsing 40,000 sentences.
  98. nodeList = [(tree, [tree.label()])]
  99. while nodeList != []:
  100. node, parent = nodeList.pop()
  101. if isinstance(node, Tree):
  102. # parent annotation
  103. parentString = ""
  104. originalNode = node.label()
  105. if vertMarkov != 0 and node != tree and isinstance(node[0], Tree):
  106. parentString = "%s<%s>" % (parentChar, "-".join(parent))
  107. node.set_label(node.label() + parentString)
  108. parent = [originalNode] + parent[: vertMarkov - 1]
  109. # add children to the agenda before we mess with them
  110. for child in node:
  111. nodeList.append((child, parent))
  112. # chomsky normal form factorization
  113. if len(node) > 2:
  114. childNodes = [child.label() for child in node]
  115. nodeCopy = node.copy()
  116. node[0:] = [] # delete the children
  117. curNode = node
  118. numChildren = len(nodeCopy)
  119. for i in range(1, numChildren - 1):
  120. if factor == "right":
  121. newHead = "%s%s<%s>%s" % (
  122. originalNode,
  123. childChar,
  124. "-".join(
  125. childNodes[i : min([i + horzMarkov, numChildren])]
  126. ),
  127. parentString,
  128. ) # create new head
  129. newNode = Tree(newHead, [])
  130. curNode[0:] = [nodeCopy.pop(0), newNode]
  131. else:
  132. newHead = "%s%s<%s>%s" % (
  133. originalNode,
  134. childChar,
  135. "-".join(
  136. childNodes[max([numChildren - i - horzMarkov, 0]) : -i]
  137. ),
  138. parentString,
  139. )
  140. newNode = Tree(newHead, [])
  141. curNode[0:] = [newNode, nodeCopy.pop()]
  142. curNode = newNode
  143. curNode[0:] = [child for child in nodeCopy]
  144. def un_chomsky_normal_form(
  145. tree, expandUnary=True, childChar="|", parentChar="^", unaryChar="+"
  146. ):
  147. # Traverse the tree-depth first keeping a pointer to the parent for modification purposes.
  148. nodeList = [(tree, [])]
  149. while nodeList != []:
  150. node, parent = nodeList.pop()
  151. if isinstance(node, Tree):
  152. # if the node contains the 'childChar' character it means that
  153. # it is an artificial node and can be removed, although we still need
  154. # to move its children to its parent
  155. childIndex = node.label().find(childChar)
  156. if childIndex != -1:
  157. nodeIndex = parent.index(node)
  158. parent.remove(parent[nodeIndex])
  159. # Generated node was on the left if the nodeIndex is 0 which
  160. # means the grammar was left factored. We must insert the children
  161. # at the beginning of the parent's children
  162. if nodeIndex == 0:
  163. parent.insert(0, node[0])
  164. parent.insert(1, node[1])
  165. else:
  166. parent.extend([node[0], node[1]])
  167. # parent is now the current node so the children of parent will be added to the agenda
  168. node = parent
  169. else:
  170. parentIndex = node.label().find(parentChar)
  171. if parentIndex != -1:
  172. # strip the node name of the parent annotation
  173. node.set_label(node.label()[:parentIndex])
  174. # expand collapsed unary productions
  175. if expandUnary == True:
  176. unaryIndex = node.label().find(unaryChar)
  177. if unaryIndex != -1:
  178. newNode = Tree(
  179. node.label()[unaryIndex + 1 :], [i for i in node]
  180. )
  181. node.set_label(node.label()[:unaryIndex])
  182. node[0:] = [newNode]
  183. for child in node:
  184. nodeList.append((child, node))
  185. def collapse_unary(tree, collapsePOS=False, collapseRoot=False, joinChar="+"):
  186. """
  187. Collapse subtrees with a single child (ie. unary productions)
  188. into a new non-terminal (Tree node) joined by 'joinChar'.
  189. This is useful when working with algorithms that do not allow
  190. unary productions, and completely removing the unary productions
  191. would require loss of useful information. The Tree is modified
  192. directly (since it is passed by reference) and no value is returned.
  193. :param tree: The Tree to be collapsed
  194. :type tree: Tree
  195. :param collapsePOS: 'False' (default) will not collapse the parent of leaf nodes (ie.
  196. Part-of-Speech tags) since they are always unary productions
  197. :type collapsePOS: bool
  198. :param collapseRoot: 'False' (default) will not modify the root production
  199. if it is unary. For the Penn WSJ treebank corpus, this corresponds
  200. to the TOP -> productions.
  201. :type collapseRoot: bool
  202. :param joinChar: A string used to connect collapsed node values (default = "+")
  203. :type joinChar: str
  204. """
  205. if collapseRoot == False and isinstance(tree, Tree) and len(tree) == 1:
  206. nodeList = [tree[0]]
  207. else:
  208. nodeList = [tree]
  209. # depth-first traversal of tree
  210. while nodeList != []:
  211. node = nodeList.pop()
  212. if isinstance(node, Tree):
  213. if (
  214. len(node) == 1
  215. and isinstance(node[0], Tree)
  216. and (collapsePOS == True or isinstance(node[0, 0], Tree))
  217. ):
  218. node.set_label(node.label() + joinChar + node[0].label())
  219. node[0:] = [child for child in node[0]]
  220. # since we assigned the child's children to the current node,
  221. # evaluate the current node again
  222. nodeList.append(node)
  223. else:
  224. for child in node:
  225. nodeList.append(child)
  226. #################################################################
  227. # Demonstration
  228. #################################################################
  229. def demo():
  230. """
  231. A demonstration showing how each tree transform can be used.
  232. """
  233. from nltk.draw.tree import draw_trees
  234. from nltk import tree, treetransforms
  235. from copy import deepcopy
  236. # original tree from WSJ bracketed text
  237. sentence = """(TOP
  238. (S
  239. (S
  240. (VP
  241. (VBN Turned)
  242. (ADVP (RB loose))
  243. (PP
  244. (IN in)
  245. (NP
  246. (NP (NNP Shane) (NNP Longman) (POS 's))
  247. (NN trading)
  248. (NN room)))))
  249. (, ,)
  250. (NP (DT the) (NN yuppie) (NNS dealers))
  251. (VP (AUX do) (NP (NP (RB little)) (ADJP (RB right))))
  252. (. .)))"""
  253. t = tree.Tree.fromstring(sentence, remove_empty_top_bracketing=True)
  254. # collapse subtrees with only one child
  255. collapsedTree = deepcopy(t)
  256. treetransforms.collapse_unary(collapsedTree)
  257. # convert the tree to CNF
  258. cnfTree = deepcopy(collapsedTree)
  259. treetransforms.chomsky_normal_form(cnfTree)
  260. # convert the tree to CNF with parent annotation (one level) and horizontal smoothing of order two
  261. parentTree = deepcopy(collapsedTree)
  262. treetransforms.chomsky_normal_form(parentTree, horzMarkov=2, vertMarkov=1)
  263. # convert the tree back to its original form (used to make CYK results comparable)
  264. original = deepcopy(parentTree)
  265. treetransforms.un_chomsky_normal_form(original)
  266. # convert tree back to bracketed text
  267. sentence2 = original.pprint()
  268. print(sentence)
  269. print(sentence2)
  270. print("Sentences the same? ", sentence == sentence2)
  271. draw_trees(t, collapsedTree, cnfTree, parentTree, original)
  272. if __name__ == "__main__":
  273. demo()
  274. __all__ = ["chomsky_normal_form", "un_chomsky_normal_form", "collapse_unary"]