# Path: resources.py
# class Annotation(object):
#     def __init__(self, start, end, type):
#         self.start = start
#         self.end = end
#         self.type = type
# 
#     def __str__(self):
#         return 'Annotation(type={}, start={}, end={})'.format(
#                 self.type, self.start, self.end)
# 
# class Document(object):
#     def __init__(self, id, title, abstract, path):
#         self.id = id
#         self.title = title
#         self.abstract = abstract
#         self.path = path
# 
#     def __iter__(self):
#         return chain(self.title, self.abstract)
# 
#     def __len__(self):
#         return len(self.title) + len(self.abstract)
# 
# class Sentence(object):
#     def __init__(self, text, annotations):
#         self.text = text
#         self.annotations = annotations
# 
#     def __getitem__(self, val):
#         return self.text.__getitem__(val)
# 
#     def __iter__(self):
#         return iter(self.annotations)
# 
#     def __str__(self):
#         return self.text
# 
#     def add_annotation(self, annotation):
#         self.annotations.append(annotation)
# 
#     def annotation_text(self, annotation):
#         ret = self.text[annotation.start:annotation.end]
#         assert ret, '{0} len({1}) "{2}"'.format(annotation,
#                 len(self.text), self.text)
#         return ret


from collections import defaultdict, namedtuple
from itertools import chain, tee
from os import walk
from os.path import dirname, splitext
from os.path import join as join_path
from re import compile as _compile
from random import sample
from sys import path
from codecs import EncodedFile
from aligner import Aligner, MisalignedError
from resources import Annotation, Document, Sentence
from bisect import bisect_left
from random import seed as random_seed
    from collections import defaultdict
    from collections import defaultdict
        import matplotlib
        import matplotlib.pyplot as plt
        if a2_path is None:
            a2_path = '/dev/null'

        #with open(a1_path, 'r') as a1_file, open(a2_path, 'r') as a2_file:
        with open(a1_path, 'r') as a1_file:
            if ENCODE_WRAP:
                a1_file = _encode_wrap(a1_file)
            with open(a2_path, 'r') as a2_file:
                if ENCODE_WRAP:
                    a2_file = _encode_wrap(a2_file)
                for line in (l.rstrip('\n') for l in chain(a1_file, a2_file)):
                    # We ignore everything apart from the text-bound annotations
                    match = TB_SO_REGEX.match(line)
                    if match is not None:
                        g_dict = match.groupdict()
                        ann_start = int(g_dict['start'])
                        ann_end = int(g_dict['end'])

                        # Find the sentence and its index containing the annotation
                        s_idx, sentence = _find_containing_idx(ann_start,
                                s_starts_and_sentences)
                        
                        # XXX: There are cases where an annotation is cut-off
                        #       by a sentence break. If this is the case, merge
                        #       the sentences.
                        if ann_end > s_idx + len(sentence.text):
                            next_s_idx, next_sentence = _find_containing_idx(
                                    ann_end, s_starts_and_sentences)
                            # Merge the next sentence into this one
                            # XXX: Just assumes a space! May be wrong!

# Path: resources.py
# class Annotation(object):
#     def __init__(self, start, end, type):
#         self.start = start
#         self.end = end
#         self.type = type
# 
#     def __str__(self):
#         return 'Annotation(type={}, start={}, end={})'.format(
#                 self.type, self.start, self.end)
# 
# class Document(object):
#     def __init__(self, id, title, abstract, path):
#         self.id = id
#         self.title = title
#         self.abstract = abstract
#         self.path = path
# 
#     def __iter__(self):
#         return chain(self.title, self.abstract)
# 
#     def __len__(self):
#         return len(self.title) + len(self.abstract)
# 
# class Sentence(object):
#     def __init__(self, text, annotations):
#         self.text = text
#         self.annotations = annotations
# 
#     def __getitem__(self, val):
#         return self.text.__getitem__(val)
# 
#     def __iter__(self):
#         return iter(self.annotations)
# 
#     def __str__(self):
#         return self.text
# 
#     def add_annotation(self, annotation):
#         self.annotations.append(annotation)
# 
#     def annotation_text(self, annotation):
#         ret = self.text[annotation.start:annotation.end]
#         assert ret, '{0} len({1}) "{2}"'.format(annotation,
#                 len(self.text), self.text)
#         return ret


# Path: reader/bionlp.py
from collections import defaultdict, namedtuple
from itertools import chain, tee
from os import walk
from os.path import dirname, splitext
from os.path import join as join_path
from re import compile as _compile
from random import sample
from sys import path
from codecs import EncodedFile
from aligner import Aligner, MisalignedError
from resources import Annotation, Document, Sentence
from bisect import bisect_left
from random import seed as random_seed
    from collections import defaultdict
    from collections import defaultdict
        import matplotlib
        import matplotlib.pyplot as plt

        if a2_path is None:
            a2_path = '/dev/null'

        #with open(a1_path, 'r') as a1_file, open(a2_path, 'r') as a2_file:
        with open(a1_path, 'r') as a1_file:
            if ENCODE_WRAP:
                a1_file = _encode_wrap(a1_file)
            with open(a2_path, 'r') as a2_file:
                if ENCODE_WRAP:
                    a2_file = _encode_wrap(a2_file)
                for line in (l.rstrip('\n') for l in chain(a1_file, a2_file)):
                    # We ignore everything apart from the text-bound annotations
                    match = TB_SO_REGEX.match(line)
                    if match is not None:
                        g_dict = match.groupdict()
                        ann_start = int(g_dict['start'])
                        ann_end = int(g_dict['end'])

                        # Find the sentence and its index containing the annotation
                        s_idx, sentence = _find_containing_idx(ann_start,
                                s_starts_and_sentences)
                        
                        # XXX: There are cases where an annotation is cut-off
                        #       by a sentence break. If this is the case, merge
                        #       the sentences.
                        if ann_end > s_idx + len(sentence.text):
                            next_s_idx, next_sentence = _find_containing_idx(
                                    ann_end, s_starts_and_sentences)
                            # Merge the next sentence into this one
                            # XXX: Just assumes a space! May be wrong!

                            sentence = Sentence(sentence.text + ' ' + next_sentence.text,