smprl.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. This module defines classes related to SPMRL constituency and dependency
  5. annotation format.
  6. Version 1.0 (02-Jun-2015)
  7. - SPMRLConstTree and SPMRLConstNode are added.
  8. """
  9. import constparse
  10. #¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
  11. # SPMRLConstTree
  12. #¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
  13. class SPMRLConstTree(constparse.ConstTree):
  14. '''
  15. Class for SPMRL-format constituency tree
  16. '''
  17. def __init__(self):
  18. '''
  19. Constructor
  20. '''
  21. constparse.ConstTree.__init__(self)
  22. def _createRoot(self):
  23. '''
  24. Creates and returns the root node
  25. '''
  26. return SPMRLConstNode()
  27. def removeFeatures(self):
  28. '''
  29. Removes feature annotation from the syntactic label.
  30. Feature annotations are surrounded by ## and follow the syntactic
  31. label (e.g. (CLS-SUJ##lem=il|cpos=CL|g=m|n=p|p=1|s=suj## Nous)).
  32. '''
  33. self.root.removeFeatures(pflgAllSubtree = True)
  34. #¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
  35. # SPMRLConstNode
  36. #¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
  37. class SPMRLConstNode(constparse.ConstNode):
  38. '''
  39. Class for SPMRL-format constituency tree node
  40. '''
  41. def __init__(self):
  42. '''
  43. Constructor
  44. '''
  45. constparse.ConstNode.__init__(self)
  46. def _getNewNode(self):
  47. '''
  48. Creates and returns a node
  49. '''
  50. return SPMRLConstNode()
  51. def getFeatures(self):
  52. '''
  53. Returns the feature annotation attached to the label
  54. '''
  55. vGluePos = self.getLabel().find('##')
  56. if vGluePos != -1:
  57. return self.getLabel()[vGluePos + 2 : -2]
  58. else:
  59. return ''
  60. def removeFeatures(self, pflgAllSubtree = False):
  61. '''
  62. Removes feature annotation from the syntactic label.
  63. Feature annotations are surrounded by ## and follow the syntactic
  64. label (e.g. (CLS-SUJ##lem=il|cpos=CL|g=m|n=p|p=1|s=suj## Nous)).
  65. The removal can be done to only the current node or the entire subtree
  66. under the node.
  67. '''
  68. vGluePos = self.getLabel().find('##')
  69. if vGluePos != -1:
  70. self.synTag = self.getLabel()[ : vGluePos]
  71. if pflgAllSubtree:
  72. for vChild in self.getChildren():
  73. vChild.removeFeatures(pflgAllSubtree = pflgAllSubtree)
  74. #¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
  75. # SPMRLConstParseLoader
  76. #¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
  77. class SPMRLConstParseLoader(constparse.ConstParseLoader):
  78. '''
  79. Constituency parse loader
  80. see the parent class for details
  81. '''
  82. def __init__(self):
  83. '''
  84. Constructor
  85. '''
  86. # list of ConstParses
  87. self.parses = None
  88. def _createNewTree(self):
  89. '''
  90. Creates and returns a new constituency tree
  91. '''
  92. return SPMRLConstTree()