Exemplo n.º 1
0
    def from_text(cls, text):
        input_lines = [l.strip() for l in text.splitlines() if l.strip() != '']

        parse_entity = False
        parse_event = False

        script = cls(input_lines[0])
        for line in input_lines[1:]:
            if line == 'Entities:':
                assert (not parse_entity) and (not parse_event)
                parse_entity = True
                continue
            elif line == 'Events:':
                assert parse_entity and (not parse_event)
                parse_event = True
                parse_entity = False
                continue
            if parse_entity:
                entity = Entity.from_text(line.partition('\t')[2])
                script.add_entity(entity)
            elif parse_event:
                event = Event.from_text(line.partition('\t')[2])
                script.add_event(event)
            else:
                raise RuntimeError(
                    'cannot parse EventScript from: {}'.format(text))

        return script
Exemplo n.º 2
0
    def from_text(cls, text):
        if len(text.splitlines()) == 1:
            # Empty document: error in event extraction
            return cls(text.strip(), [], [])

        sections = split_sections((l.strip() for l in text.splitlines()),
                                  ["Entities:", "Events:"])
        # First comes the doc name
        doc_name = sections[0][0].strip()
        # Then a whole section giving the entities, matched by entity_line_re
        entity_lines = [
            line.partition('\t')[2] for line in sections[1] if line
        ]
        entities = [Entity.from_text(line) for line in entity_lines]
        # Then another section giving the events, matched by event_line_re
        event_lines = [line.partition('\t')[2] for line in sections[2] if line]
        events = [Event.from_text(line) for line in event_lines]

        return cls(doc_name, entities, events)