# Path: dsegmenter/common.py
# WORD = "word"
# 
# REL = "rel"
# 
# DEPS = "deps"
# 
# TAG = "tag"
# 
# Path: dsegmenter/treeseg/constants.py
# GREEDY = 0
# 
# GENEROUS = 1
# 
# DEPENDENCY = 1
# 
# CONSTITUENCY = 2
# 
# Path: dsegmenter/treeseg/discourse_segment.py
# class DiscourseSegment(object):
#     """Class representing discourse segment.
# 
#     Attributes:
#       name (str): name of this segment
#       leaves (list): words or other segments inside of this unit
# 
#     """
# 
#     def __init__(self, a_name="", a_leaves=[]):
#         """Class constructor.
# 
#         Args:
#           a_name (str): name of discourse segment
#           a_leaves (list): segment's child nodes (either words or other
#             segments)
# 
#         """
#         self.name = a_name
#         self.leaves = a_leaves
#         self.leaves.sort(key=lambda el: el[0] if el else -1)
# 
#     def get_end(self):
#         """Obtain position of the last token in the list of leaves.
# 
#         Returns:
#           int: position of the last token in the list of leaves
# 
#         """
#         if not self.leaves:
#             return -1
#         else:
#             last_leaf = self.leaves[-1]
#             if isinstance(last_leaf[-1], DiscourseSegment):
#                 return last_leaf[-1].get_end()
#             else:
#                 return last_leaf[0]
# 
#     def insort(self, a_leaf):
#         """Insert leaf in the list of leaves according to its position.
# 
#         Args:
#           a_leaf (dict): leaf to be inserted
# 
#         Returns:
#           void:
# 
#         """
#         ipos = bisect_right(self.leaves, a_leaf)
#         inserted = False
#         prev_pos = ipos - 1
#         if prev_pos >= 0:
#             prev_leaf = self.leaves[prev_pos]
#             if isinstance(prev_leaf[-1], DiscourseSegment) and \
#                     prev_leaf[-1].get_end() > a_leaf[0]:
#                 prev_leaf[-1].insort(a_leaf)
#                 inserted = True
#         if not inserted:
#             self.leaves.insert(ipos, a_leaf)
# 
#     def __len__(self):
#         """Return number of elements in given segment.
# 
#         Returns:
#           int:number of elements in segment
# 
#         """
#         return len(self.leaves)
# 
#     def __nonzero__(self):
#         """Return True if the given segment is not empty.
# 
#         Returns:
#           bool: True if segment is not empty
# 
#         """
#         return bool(self.leaves)
# 
#     def __unicode__(self):
#         """Return unicode representation of given segment.
# 
#         Returns:
#           unicode: unicode string representing this object
# 
#         """
#         ret = u"(" + unicode(self.name)
#         for t in self.leaves:
#             ret += u' ' + unicode(t[-1])
#         ret += u" )"
#         return ret
# 
#     def __str__(self):
#         """Return utf-8 string representation of given segment.
# 
#         Returns:
#           str: utf-8 string representing this object
# 
#         """
#         return self.__unicode__().encode(ENCODING)
# 
#     def __repr__(self):
#         """Return internal representation of given segment.
# 
#         Returns:
#           str: internal representation of this segment
# 
#         """
#         ret = '<' + self.__class__.__name__ + " at " + str(hex(id(self)))
#         ret += u" name=" + repr(self.name)
#         ret += u" leaves=" + repr(self.leaves) + ">"
#         return ret


from dsegmenter.common import WORD, REL, DEPS, TAG
from dsegmenter.treeseg.constants import GREEDY, GENEROUS, DEPENDENCY, \
    CONSTITUENCY
from dsegmenter.treeseg.discourse_segment import DiscourseSegment
                dseg = DiscourseSegment(a_name=dec, a_leaves=leaves)

                if word is not None:
                    dseg.insort((ipos, word))
                # if strategy is GREEDY, only leave nodes which are adjacent to
                # the root
                if a_strategy == GREEDY:
                    outleaves = self._extract_nonadjacent(dseg, ipos)
                    a_ret += outleaves
                a_ret.append((dseg.leaves[0][0] if dseg.leaves else -1, dseg))
        # for GENEROUS strategy, do some post-processing
        a_ret.sort(key=lambda el: el[0])
        if a_strategy == GENEROUS:
            a_ret[:] = self._unite_nonadjacent(a_ret)
        return a_ret

    def _dg_decfunc(self, a_node, a_tree):
        """Make a prediction whether given node initiates a segment.

        Args:
          a_node (dict): parse node to be analyzed
          a_tree (nltk.parse.dependencygraph): tree of analyzed node

        Returns:
          str or None: name of discourse segment or None

        """
        chnode = None
        chtag = ""
        irel = a_node.get(REL, "UNK")

# Path: dsegmenter/common.py
# WORD = "word"
# 
# REL = "rel"
# 
# DEPS = "deps"
# 
# TAG = "tag"
# 
# Path: dsegmenter/treeseg/constants.py
# GREEDY = 0
# 
# GENEROUS = 1
# 
# DEPENDENCY = 1
# 
# CONSTITUENCY = 2
# 
# Path: dsegmenter/treeseg/discourse_segment.py
# class DiscourseSegment(object):
#     """Class representing discourse segment.
# 
#     Attributes:
#       name (str): name of this segment
#       leaves (list): words or other segments inside of this unit
# 
#     """
# 
#     def __init__(self, a_name="", a_leaves=[]):
#         """Class constructor.
# 
#         Args:
#           a_name (str): name of discourse segment
#           a_leaves (list): segment's child nodes (either words or other
#             segments)
# 
#         """
#         self.name = a_name
#         self.leaves = a_leaves
#         self.leaves.sort(key=lambda el: el[0] if el else -1)
# 
#     def get_end(self):
#         """Obtain position of the last token in the list of leaves.
# 
#         Returns:
#           int: position of the last token in the list of leaves
# 
#         """
#         if not self.leaves:
#             return -1
#         else:
#             last_leaf = self.leaves[-1]
#             if isinstance(last_leaf[-1], DiscourseSegment):
#                 return last_leaf[-1].get_end()
#             else:
#                 return last_leaf[0]
# 
#     def insort(self, a_leaf):
#         """Insert leaf in the list of leaves according to its position.
# 
#         Args:
#           a_leaf (dict): leaf to be inserted
# 
#         Returns:
#           void:
# 
#         """
#         ipos = bisect_right(self.leaves, a_leaf)
#         inserted = False
#         prev_pos = ipos - 1
#         if prev_pos >= 0:
#             prev_leaf = self.leaves[prev_pos]
#             if isinstance(prev_leaf[-1], DiscourseSegment) and \
#                     prev_leaf[-1].get_end() > a_leaf[0]:
#                 prev_leaf[-1].insort(a_leaf)
#                 inserted = True
#         if not inserted:
#             self.leaves.insert(ipos, a_leaf)
# 
#     def __len__(self):
#         """Return number of elements in given segment.
# 
#         Returns:
#           int:number of elements in segment
# 
#         """
#         return len(self.leaves)
# 
#     def __nonzero__(self):
#         """Return True if the given segment is not empty.
# 
#         Returns:
#           bool: True if segment is not empty
# 
#         """
#         return bool(self.leaves)
# 
#     def __unicode__(self):
#         """Return unicode representation of given segment.
# 
#         Returns:
#           unicode: unicode string representing this object
# 
#         """
#         ret = u"(" + unicode(self.name)
#         for t in self.leaves:
#             ret += u' ' + unicode(t[-1])
#         ret += u" )"
#         return ret
# 
#     def __str__(self):
#         """Return utf-8 string representation of given segment.
# 
#         Returns:
#           str: utf-8 string representing this object
# 
#         """
#         return self.__unicode__().encode(ENCODING)
# 
#     def __repr__(self):
#         """Return internal representation of given segment.
# 
#         Returns:
#           str: internal representation of this segment
# 
#         """
#         ret = '<' + self.__class__.__name__ + " at " + str(hex(id(self)))
#         ret += u" name=" + repr(self.name)
#         ret += u" leaves=" + repr(self.leaves) + ">"
#         return ret


# Path: dsegmenter/treeseg/treesegmenter.py
from dsegmenter.common import WORD, REL, DEPS, TAG
from dsegmenter.treeseg.constants import GREEDY, GENEROUS, DEPENDENCY, \
    CONSTITUENCY
from dsegmenter.treeseg.discourse_segment import DiscourseSegment

                dseg = DiscourseSegment(a_name=dec, a_leaves=leaves)

                if word is not None:
                    dseg.insort((ipos, word))
                # if strategy is GREEDY, only leave nodes which are adjacent to
                # the root
                if a_strategy == GREEDY:
                    outleaves = self._extract_nonadjacent(dseg, ipos)
                    a_ret += outleaves
                a_ret.append((dseg.leaves[0][0] if dseg.leaves else -1, dseg))
        # for GENEROUS strategy, do some post-processing
        a_ret.sort(key=lambda el: el[0])
        if a_strategy == GENEROUS:
            a_ret[:] = self._unite_nonadjacent(a_ret)
        return a_ret

    def _dg_decfunc(self, a_node, a_tree):
        """Make a prediction whether given node initiates a segment.

        Args:
          a_node (dict): parse node to be analyzed
          a_tree (nltk.parse.dependencygraph): tree of analyzed node

        Returns:
          str or None: name of discourse segment or None

        """
        chnode = None
        chtag = ""
        irel = a_node.get(REL, "UNK")

        ideps = a_node.get(DEPS, [])