library.py 3.11 KB
Newer Older
Anze Vavpetic's avatar
Anze Vavpetic committed
1 2 3 4 5 6
import re
from string import ascii_lowercase as chars
from random import choice

from aleph import Aleph
from rsd import RSD
Matic Perovšek's avatar
Matic Perovšek committed
7
from wordification import Wordification
8
from security import check_input
Anze Vavpetic's avatar
Anze Vavpetic committed
9

Anze Vavpetic's avatar
Anze Vavpetic committed
10 11
from services.webservice import WebService

Anze Vavpetic's avatar
Anze Vavpetic committed
12 13 14 15 16 17 18
def ilp_aleph(input_dict):
    aleph = Aleph()
    settings = input_dict['settings']
    mode = input_dict['mode']
    pos = input_dict['pos']
    neg = input_dict['neg']
    b = input_dict['b']
19
    # Parse settings provided via file
Anze Vavpetic's avatar
Anze Vavpetic committed
20 21
    if settings:
        aleph.settingsAsFacts(settings)
22 23 24
    # Parse settings provided as parameters (these have higher priority)
    for setting, def_val in Aleph.ESSENTIAL_PARAMS.items():
        aleph.set(setting, input_dict.get(setting, def_val))
25 26 27
    # Check for illegal predicates
    for pl_script in [b, pos, neg]:
        check_input(pl_script)
Anze Vavpetic's avatar
Anze Vavpetic committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    # Run aleph
    theory = aleph.induce(mode, pos, neg, b)
    return {'theory' : theory}

def ilp_rsd(input_dict):
    rsd = RSD()
    settings = input_dict.get('settings',None)
    pos = input_dict.get('pos', None)
    neg = input_dict.get('neg', None)
    examples = input_dict.get('examples', None)
    b = input_dict['b']
    subgroups = True if input_dict['subgroups'] == 'true' else False
    # Parse settings
    if settings:
        rsd.settingsAsFacts(settings)
43 44 45
    # Parse settings provided as parameters (these have higher priority)
    for setting, def_val in RSD.ESSENTIAL_PARAMS.items():
        rsd.set(setting, input_dict.get(setting, def_val))
46 47 48
    # Check for illegal predicates
    for pl_script in [b, pos, neg, examples]:
        check_input(pl_script)
Anze Vavpetic's avatar
Anze Vavpetic committed
49 50 51
    # Run rsd
    features, arff, rules = rsd.induce(b, examples=examples, pos=pos, neg=neg, cn2sd=subgroups)
    return {'features' : features, 'arff' : arff, 'rules' : rules}
52 53

def ilp_sdmsegs_rule_viewer(input_dict):
Anze Vavpetic's avatar
Anze Vavpetic committed
54 55 56
    return {}

def ilp_sdmaleph(input_dict):
57 58
    #ws = WebService('http://workflow.ijs.si:8081', 3600)
    ws = WebService('http://vihar.ijs.si:8097', 3600)
Anze Vavpetic's avatar
Anze Vavpetic committed
59 60 61 62 63 64 65 66 67 68 69 70
    response = ws.client.sdmaleph(
        examples=input_dict.get('examples'),
        mapping=input_dict.get('mapping'),
        ontologies=[{'ontology' : ontology} for ontology in input_dict.get('ontology')],
        relations=[{'relation' : relation} for relation in input_dict.get('relation')],
        posClassVal=input_dict.get('posClassVal') if input_dict.get('posClassVal') != '' else None,
        cutoff=input_dict.get('cutoff') if input_dict.get('cutoff') != '' else None,
        minPos=input_dict.get('minPos') if input_dict.get('minPos') != '' else None,
        noise=input_dict.get('noise') if input_dict.get('noise') != '' else None,
        clauseLen=input_dict.get('clauseLen') if input_dict.get('clauseLen') != '' else None,
        dataFormat=input_dict.get('dataFormat') if input_dict.get('dataFormat') != '' else None
    )
Matic Perovšek's avatar
Matic Perovšek committed
71 72 73 74 75 76 77 78 79
    return {'theory' : response['theory']}


def ilp_wordification(input_dict):
    target_table = input_dict.get('target_table',None)
    other_tables = input_dict.get('other_tables', None)
    context = input_dict.get('context', None)
    wordification = Wordification(target_table,other_tables,context)
    return {'corpus' : wordification.wordify()}