library.py 3.62 KB
Newer Older
1
import re
vpodpecan's avatar
vpodpecan committed
2 3 4 5 6 7 8
import subprocess
import logging
try:
    from ladon.clients.jsonwsp import JSONWSPClient
except ImportError:
    logging.warning('Ladon package not available, most of MUSE stuff will not work.')

9 10 11 12 13 14 15 16 17 18 19 20 21


def MUSE_annotate(input_dict):
    from NLP_interface import invoke_terence_service
    text = input_dict['raw_text']
    xml = invoke_terence_service(text)
    #xml = xml.replace('\n', ' ')
    #xml = xml.replace('\t', ' ')
    return {'annotations_xml': xml}


def MUSE_view_xml(input_dict):
    return {'xml_data': input_dict.get('xml_data', None)}
vpodpecan's avatar
vpodpecan committed
22 23 24 25 26 27


def MUSE_mapping_to_KR(input_dict):
    url = input_dict['url']
    data = input_dict['input_data']

28
    import socket
vpodpecan's avatar
vpodpecan committed
29
    cli = JSONWSPClient(url)
30
    socket.setdefaulttimeout(None)
vpodpecan's avatar
vpodpecan committed
31 32 33 34 35
    result = cli.mapping_to_KR(SRL=data)

    return {'mapping': result.response_dict['result']}


vpodpecan's avatar
vpodpecan committed
36 37 38 39
def MUSE_mapping_to_KR_latest(input_dict):
    url = input_dict['url']
    data = input_dict['input_data']

40
    import socket
vpodpecan's avatar
vpodpecan committed
41
    cli = JSONWSPClient(url)
42
    socket.setdefaulttimeout(None)
vpodpecan's avatar
vpodpecan committed
43 44 45 46 47
    result, xml = cli.mapping_to_KR_latest(SRL=data)
    mapping, xml = result.response_dict['result']

    return {'mapping': mapping, 'xml': xml}

vpodpecan's avatar
vpodpecan committed
48 49 50 51 52
def MUSE_mapping_to_KR_precomputed(input_dict):
    url = input_dict['url']
    data = input_dict['input_data']

    cli = JSONWSPClient(url)
vpodpecan's avatar
vpodpecan committed
53
    result = cli.mapping_to_KR_precomputed(SRL=data)
vpodpecan's avatar
vpodpecan committed
54 55 56 57

    return {'mapping': result.response_dict['result']}


vpodpecan's avatar
vpodpecan committed
58 59 60 61 62 63 64 65 66 67 68
def MUSE_mapping_to_KR_precomputed_latest(input_dict):
    url = input_dict['url']
    data = input_dict['input_data']

    cli = JSONWSPClient(url)
    result = cli.mapping_to_KR_precomputed_latest(SRL=data)
    mapping, xml = result.response_dict['result']

    return {'mapping': mapping, 'xml': xml}


vpodpecan's avatar
vpodpecan committed
69 70 71 72
def MUSE_mapping_to_KR_golden_standard(input_dict):
    return {'goldenLink': 'https://ive.scm.tees.ac.uk/muse/tuk-text/gold_standard.xml'}


vpodpecan's avatar
vpodpecan committed
73 74 75 76
def MUSE_semantic_role_labeling(input_dict):
    url = input_dict['url']
    data = input_dict['xml']

vpodpecan's avatar
vpodpecan committed
77 78 79
    import socket
    socket.setdefaulttimeout(None)

vpodpecan's avatar
vpodpecan committed
80 81 82 83 84
    cli = JSONWSPClient(url)
    result = cli.semantic_role_labeling(xml=data)
    srl, txt = result.response_dict['result']

    return {'xml': srl, 'txt': txt}
vpodpecan's avatar
vpodpecan committed
85 86


vpodpecan's avatar
vpodpecan committed
87 88 89 90
def MUSE_semantic_role_labeling_tuk(input_dict):
    url = input_dict['url']
    data = input_dict['xml']

vpodpecan's avatar
vpodpecan committed
91 92 93
    import socket
    socket.setdefaulttimeout(None)

vpodpecan's avatar
vpodpecan committed
94 95 96 97 98 99 100
    cli = JSONWSPClient(url)
    result = cli.semantic_role_labeling_tuk(xml=data)
    srl, txt = result.response_dict['result']

    return {'xml': srl, 'txt': txt}


vpodpecan's avatar
vpodpecan committed
101 102 103 104 105
def MUSE_string_to_file(input_dict):
    return {}


def MUSE_string_to_file_finished(postdata, input_dict, output_dict):
vpodpecan's avatar
vpodpecan committed
106 107 108
    from socket import getfqdn
    import sys

vpodpecan's avatar
vpodpecan committed
109
    if len(sys.argv) > 1:
vpodpecan's avatar
vpodpecan committed
110 111 112 113
        if len(sys.argv > 2) and '.' in sys.argv[2] and ':' in sys.argv[2]:
            port = sys.argv[2].split(':')[1]
        else:
            port = 8000
114 115 116
        DEVSERVER = sys.argv[1].startswith('runserver')
    else:
        DEVSERVER = False
vpodpecan's avatar
vpodpecan committed
117
    fqdn = '127.0.0.1:' + port if DEVSERVER else getfqdn()
vpodpecan's avatar
vpodpecan committed
118 119

    fileURL = fqdn + postdata.get('fileURL')[0]
vpodpecan's avatar
vpodpecan committed
120 121
    return {'fileURL': fileURL}

vpodpecan's avatar
vpodpecan committed
122 123 124 125 126 127 128 129 130 131 132

def MUSE_virtual_environment_visualization(input_dict):
    NLP_data = input_dict.get('NLP_data', None)
    unitylink = input_dict.get('unitylink', 'https://ive.scm.tees.ac.uk/muse/tukdemo/')
    return {'NLP_data': NLP_data,
            'unitylink': unitylink}


def MUSE_virtual_environment_demonstrator_tuk(input_dict):
    return {'mappingLink': str(input_dict['mappingLink']), 'unityLink': input_dict['unityLink']}

133 134 135

def MUSE_virtual_environment_demonstrator_tuk_local(input_dict):
    return {'mappingLink': str(input_dict['mappingLink']), 'unityLink': input_dict['unityLink']}