Esempio n. 1
0
from copy import copy
from operator import attrgetter
from collections import deque

from pydmrs._exceptions import PydmrsError, PydmrsValueError
from pydmrs.core import Link, Pred, Dmrs, ListDmrs, DictDmrs
from pydmrs.utils import load_config, get_config_option

DEFAULT_CONFIG_FILE = 'default_simplification.conf'

config = load_config(DEFAULT_CONFIG_FILE)
REVERSE_ARG1 = frozenset(Pred.from_string(x) for x in get_config_option(config, 'Rooted Conversion', 'reverse_arg1', opt_type=list))


def reverse_link(dmrs, link):
    """
    Reverse a Link in a Dmrs graph.
    The start and end nodeids are switched,
    and "_REV" is appended to the rargname (or removed if already present)
    """
    if link.rargname[-4:] == "_REV":
        new_rargname = link.rargname[:-4]
    else:
        new_rargname = link.rargname + "_REV"
    new_link = Link(link.end, link.start, new_rargname, link.post)
    dmrs.remove_link(link)
    dmrs.add_link(new_link)
    return new_link

def is_root(dmrs, nodeid):
    """
Esempio n. 2
0
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='DMRS simplification tool')
    parser.add_argument('-c', '--config', default=None,
                        help='Path to simplifaction configuration file. By default, configuration in __config__/default_simplification.conf is used.')
    parser.add_argument('input_dmrs', help='Specify input DMRS file')
    parser.add_argument('output_dmrs', help='Specify output dmrs file.')
    args = parser.parse_args()
    if args.config is not None:  # Load the given file
        config = load_config(args.config, default=False)
    else:
        config = load_config(DEFAULT_CONFIG_FILE)
else:
    config = load_config(DEFAULT_CONFIG_FILE)

DEFAULT_FILTER = frozenset(GPred.from_string(x) for x in get_config_option(config, 'General Predicate Filtering', 'filter', opt_type=list))
DEFAULT_ALLOW_DISC = get_config_option(config, 'General Predicate Filtering', 'allow_disconnected_dmrs') 

def gpred_filtering(dmrs, gpred_filter=DEFAULT_FILTER, allow_disconnected_dmrs=DEFAULT_ALLOW_DISC):
    """
    Remove general predicate nodes on the filter list from the DMRS.
    :param dmrs_xml: Input DMRS object
    :param gpred_filter: A list of general predicates to filter (as strings)
    :param allow_disconnected_dmrs: Remove gpred nodes even if their removal would result in a disconnected DMRS.
     If DMRS was already disconnected, gpred nodes are removed regardless.
    :return: Output DMRS object
    """

    filterable_nodeids = set()

    # Find general predicate nodes to filter
Esempio n. 3
0
from copy import copy
from operator import attrgetter
from collections import deque

from pydmrs._exceptions import PydmrsError, PydmrsValueError
from pydmrs.core import Link, Pred, Dmrs, ListDmrs, DictDmrs
from pydmrs.utils import load_config, get_config_option

DEFAULT_CONFIG_FILE = 'default_simplification.conf'

config = load_config(DEFAULT_CONFIG_FILE)
REVERSE_ARG1 = frozenset(
    Pred.from_string(x) for x in get_config_option(
        config, 'Rooted Conversion', 'reverse_arg1', opt_type=list))


def reverse_link(dmrs, link):
    """
    Reverse a Link in a Dmrs graph.
    The start and end nodeids are switched,
    and "_REV" is appended to the rargname (or removed if already present)
    """
    if link.rargname[-4:] == "_REV":
        new_rargname = link.rargname[:-4]
    else:
        new_rargname = link.rargname + "_REV"
    new_link = Link(link.end, link.start, new_rargname, link.post)
    dmrs.remove_link(link)
    dmrs.add_link(new_link)
    return new_link
Esempio n. 4
0
from delphin import ace
from delphin.mrs import from_dmrs
from delphin.dmrs import from_mrs
from delphin.codecs import simplemrs, dmrx

from pydmrs.core import ListDmrs
from pydmrs.utils import load_config, get_config_option

DEFAULT_CONFIG_FILE = 'default_interface.conf'

config = load_config(DEFAULT_CONFIG_FILE)
DEFAULT_ERG_FILE = get_config_option(config, 'Grammar', 'ERG')


def parse(sentence, cls=ListDmrs, erg_file=DEFAULT_ERG_FILE):
    results = []
    for result in ace.parse(
            erg_file, sentence).results():  # cmdargs=['-r', 'root_informal']
        mrs = result.mrs()
        _dmrs = from_mrs(mrs)
        dmrs_xml = dmrx.encode(_dmrs)
        dmrs = cls.loads_xml(dmrs_xml)
        results.append(dmrs)
    return results


def generate(dmrs, erg_file=DEFAULT_ERG_FILE):
    dmrs_xml = dmrs.dumps_xml(encoding='utf-8')
    _dmrs = dmrx.decode(dmrs_xml)
    _mrs = from_dmrs(_dmrs)
    mrs = simplemrs.encode(_mrs)
Esempio n. 5
0
        default=None,
        help=
        'Path to simplifaction configuration file. By default, configuration in __config__/default_simplification.conf is used.'
    )
    parser.add_argument('input_dmrs', help='Specify input DMRS file')
    parser.add_argument('output_dmrs', help='Specify output dmrs file.')
    args = parser.parse_args()
    if args.config is not None:  # Load the given file
        config = load_config(args.config, default=False)
    else:
        config = load_config(DEFAULT_CONFIG_FILE)
else:
    config = load_config(DEFAULT_CONFIG_FILE)

DEFAULT_FILTER = frozenset(
    GPred.from_string(x) for x in get_config_option(
        config, 'General Predicate Filtering', 'filter', opt_type=list))
DEFAULT_ALLOW_DISC = get_config_option(config, 'General Predicate Filtering',
                                       'allow_disconnected_dmrs')


def gpred_filtering(dmrs,
                    gpred_filter=DEFAULT_FILTER,
                    allow_disconnected_dmrs=DEFAULT_ALLOW_DISC):
    """
    Remove general predicate nodes on the filter list from the DMRS.
    :param dmrs_xml: Input DMRS object
    :param gpred_filter: A list of general predicates to filter (as strings)
    :param allow_disconnected_dmrs: Remove gpred nodes even if their removal would result in a disconnected DMRS.
     If DMRS was already disconnected, gpred nodes are removed regardless.
    :return: Output DMRS object
    """
Esempio n. 6
0
from delphin.interfaces import ace
from delphin.mrs import simplemrs, dmrx

from pydmrs.core import ListDmrs
from pydmrs.utils import load_config, get_config_option

DEFAULT_CONFIG_FILE = 'default_interface.conf'

config = load_config(DEFAULT_CONFIG_FILE)
DEFAULT_ERG_FILE = get_config_option(config, 'Grammar', 'ERG')


def parse(sentence, cls=ListDmrs, erg_file=DEFAULT_ERG_FILE):
    results = []
    for result in ace.parse(erg_file, sentence)['RESULTS']:  # cmdargs=['-r', 'root_informal']
        mrs = result['MRS']
        xmrs = simplemrs.loads_one(mrs)
        dmrs_xml = dmrx.dumps_one(xmrs)[11:-12]
        dmrs = cls.loads_xml(dmrs_xml)
        results.append(dmrs)
    return results


def generate(dmrs, erg_file=DEFAULT_ERG_FILE):
    dmrs_xml = '<dmrs-list>' + dmrs.dumps_xml(encoding='utf-8') + '</dmrs-list>'
    xmrs = dmrx.loads_one(dmrs_xml)
    mrs = simplemrs.dumps_one(xmrs)
    results = []
    for result in ace.generate(erg_file, mrs)['RESULTS']:
        sentence = result['SENT']
        results.append(sentence)