library.py 3.52 KB
Newer Older
Anze Vavpetic's avatar
Anze Vavpetic committed
1
import re
2 3
import json
import tempfile
Anze Vavpetic's avatar
Anze Vavpetic committed
4 5 6 7 8
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
9
from wordification import Wordification
10
from security import check_input
Anze Vavpetic's avatar
Anze Vavpetic committed
11

Anze Vavpetic's avatar
Anze Vavpetic committed
12 13
from services.webservice import WebService

Anze Vavpetic's avatar
Anze Vavpetic committed
14 15 16 17 18 19 20
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']
21
    # Parse settings provided via file
Anze Vavpetic's avatar
Anze Vavpetic committed
22 23
    if settings:
        aleph.settingsAsFacts(settings)
24 25 26
    # 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))
27 28 29
    # Check for illegal predicates
    for pl_script in [b, pos, neg]:
        check_input(pl_script)
Anze Vavpetic's avatar
Anze Vavpetic committed
30 31 32 33 34 35 36 37 38 39 40
    # 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']
41
    subgroups = input_dict['subgroups'] == 'true'
Anze Vavpetic's avatar
Anze Vavpetic committed
42 43 44
    # Parse settings
    if settings:
        rsd.settingsAsFacts(settings)
45 46 47
    # 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))
48 49 50
    # Check for illegal predicates
    for pl_script in [b, pos, neg, examples]:
        check_input(pl_script)
Anze Vavpetic's avatar
Anze Vavpetic committed
51 52 53
    # Run rsd
    features, arff, rules = rsd.induce(b, examples=examples, pos=pos, neg=neg, cn2sd=subgroups)
    return {'features' : features, 'arff' : arff, 'rules' : rules}
54 55

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

def ilp_sdmaleph(input_dict):
59
    import orange
60
    ws = WebService('http://vihar.ijs.si:8097', 3600)
61 62 63 64 65 66 67 68 69 70 71 72
    data = input_dict.get('examples')
    if isinstance(data, orange.ExampleTable):
        with tempfile.NamedTemporaryFile(suffix='.tab', delete=True) as f:
            data.save(f.name)
            examples = f.read()
    elif isinstance(data, list):
        examples = json.dumps(data)
    elif isinstance(data, str):
        examples = data
    else:
        raise Exception('Illegal examples format. \
                         Supported formats: str, list or Orange')
Anze Vavpetic's avatar
Anze Vavpetic committed
73
    response = ws.client.sdmaleph(
74
        examples=examples,
Anze Vavpetic's avatar
Anze Vavpetic committed
75 76 77 78 79 80 81 82 83 84
        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
85 86 87 88 89 90 91 92
    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)
Janez K's avatar
merge  
Janez K committed
93
    return {'corpus' : wordification.wordify()}