<commit_before>"""
Run IEPY core loop

Usage:
    iepy_runner.py <relation_name>
    iepy_runner.py -h | --help | --version

Options:
  -h --help             Show this screen
  --version             Version number
"""
from docopt import docopt
import logging
from sys import exit

from iepy.extraction.active_learning_core import ActiveLearningCore
from iepy.data.db import CandidateEvidenceManager
from iepy.data.models import Relation
from iepy.extraction.human_validation import TerminalInterviewer


def print_all_relations():
    print("All available relations:")
    for relation in Relation.objects.all():
        print("  {}".format(relation))


if __name__ == u'__main__':
    opts = docopt(__doc__, version=0.1)
    relation = opts['<relation_name>']

    logging.basicConfig(level=logging.DEBUG,
                        format=u"%(asctime)s - %(name)s - %(levelname)s - %(message)s")

    try:
        relation = Relation.objects.get(name=relation)
    except Relation.DoesNotExist:
        print("Relation {!r} non existent".format(relation))
        print_all_relations()
        exit(1)

    # Load evidences
    CEM = CandidateEvidenceManager  # shorcut
    labeled_evidences = CEM.labeled_candidates_for_relation(
        relation, CEM.conflict_resolution_newest_wins)

    p = ActiveLearningCore(relation, labeled_evidences)

    STOP = u'STOP'

    p.start()
    while p.questions:
        term = TerminalInterviewer(p.questions, p.add_answer, [(STOP, u'Stop execution ASAP')])
        result = term()
        if result == STOP:
            break
        p.process()
    predictions = p.predict()
    print("Predictions:")
    for prediction, value in predictions.items():
        print("({} -- {})".format(prediction, value))<commit_msg>added draft for the new iepy runner<commit_after>"""
Run IEPY core loop

Usage:
    iepy_runner.py <relation_name>
    iepy_runner.py -h | --help | --version

Options:
  -h --help             Show this screen
  --version             Version number
"""
from docopt import docopt
import logging
from sys import exit

from iepy.extraction.active_learning_core import ActiveLearningCore
from iepy.data.db import CandidateEvidenceManager
from iepy.data.models import Relation, SegmentToTag, TextSegment
from iepy.extraction.human_validation import TerminalInterviewer


def print_all_relations():
    print("All available relations:")
    for relation in Relation.objects.all():
        print("  {}".format(relation))


if __name__ == u'__main__':
    opts = docopt(__doc__, version=0.1)
    relation = opts['<relation_name>']

    logging.basicConfig(level=logging.DEBUG,
                        format=u"%(asctime)s - %(name)s - %(levelname)s - %(message)s")

    try:
        relation = Relation.objects.get(name=relation)
    except Relation.DoesNotExist:
        print("Relation {!r} non existent".format(relation))
        print_all_relations()
        exit(1)

    # Load evidences
    CEM = CandidateEvidenceManager  # shorcut
    labeled_evidences = CEM.labeled_candidates_for_relation(
        relation, CEM.conflict_resolution_newest_wins)

    pipeline = ActiveLearningCore(relation, labeled_evidences)
    pipeline.start()

    STOP = u'STOP'
    term = TerminalAdministration((STOP, u'Stop execution ASAP'))

    run_number = 0
    while pipeline.questions:
        segment_ids_to_add = []
        for evidence_candidate in pipeline.questions:
            segment = evidence_candidate.segment
            if segment.id not in segment_ids_to_add:
                segment_ids_to_add.append(evidence_candidate.segment.id)

        for segment_id in segment_ids_to_add:
            segment_to_tag = SegmentToTag(
                segment=TextSegment.object.get(id=segment_id),
                run_number=run_number,
            )
            segment_to_tag.save()

        result = term()
        if result == STOP:
            break

        # pipeline.add_answers(new_answers)
        pipeline.process()
        run_number += 1

    predictions = pipeline.predict()
    print("Predictions:")
    for prediction, value in predictions.items():
        print("({} -- {})".format(prediction, value))
{'fraction_non_alphanumeric': 0.10223719028145298, 'fraction_numerical': 0.0019244647582391148, 'mean_word_length': 2.900562851782364, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2605804', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1216, 'n_words': 280}