# Path: pydmrs/pydelphin_interface.py
# 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
# 
# Path: pydmrs/matching/query.py
# def dmrs_query(dmrs_iter, search_dmrs_graphlang, results_as_dict=False, results_per_dmrs=False):
#     """
#     Queries DMRS graphs for an underspecified (sub)graph pattern and returns the values of named wildcards (of the form "?[Identifier]") as they are specified in the queried graph.
#     :param dmrs_iter An iterator of DMRS graphs to query.
#     :param search_dmrs_graphlang The query DMRS (sub)graph, given as a GraphLang string.
#     :param results_as_dict True if a query result should be a dictionary, mapping identifiers to values.
#     :param results_per_dmrs True if a (possibly empty) list per DMRS should be returned.
#     :return Iterator of dicts containing the matching node ids.
#     """
#     queries = {}
#     search_dmrs = parse_graphlang(search_dmrs_graphlang, queries=queries)
#     queries = [(key, queries[key]) for key in sorted(queries)]
#     for dmrs in dmrs_iter:
#         assert isinstance(dmrs, Dmrs), 'Object in dmrs_iter is not a Dmrs.'
#         # perform an exact matching of search_dmrs against dmrs
#         matchings = dmrs_exact_matching(search_dmrs, dmrs)
#         if results_per_dmrs:
#             results = []
#         for matching in matchings:
#             # extract matched values
#             if results_as_dict:
#                 result = {key: query(matching, dmrs) for key, query in queries}
#             else:
#                 result = tuple(query(matching, dmrs) for _, query in queries)
#             if results_per_dmrs:
#                 results.append(result)
#             else:
#                 yield result
#         if results_per_dmrs:
#             yield results


from pydmrs.pydelphin_interface import parse
from pydmrs.matching.query import dmrs_query


if __name__ == '__main__':

    # basic functionality
    dmrs_list = [parse('A mouse ate the whole cheese.')[0],
                 parse('Lions eat around 15 zebras per year.')[0],
                 parse('Their children eat so many sweets.')[0],
                 parse('Potatoes are mostly eaten by humans.')[0]]
    search_dmrs = '_?1_?_?_rel i <-1- _eat_v_1_rel e? -2-> _?2_?_?_rel i'

    # not dict, not per dmrs
    results = list(dmrs_query(dmrs_list, search_dmrs, results_as_dict=False, results_per_dmrs=False))
    assert len(results) == 4
    assert ('mouse', 'cheese') in results
    assert ('lion', 'zebra') in results
    assert ('child', 'sweet') in results
    assert ('human', 'potato') in results
    # dict, not per dmrs

# Path: pydmrs/pydelphin_interface.py
# 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
# 
# Path: pydmrs/matching/query.py
# def dmrs_query(dmrs_iter, search_dmrs_graphlang, results_as_dict=False, results_per_dmrs=False):
#     """
#     Queries DMRS graphs for an underspecified (sub)graph pattern and returns the values of named wildcards (of the form "?[Identifier]") as they are specified in the queried graph.
#     :param dmrs_iter An iterator of DMRS graphs to query.
#     :param search_dmrs_graphlang The query DMRS (sub)graph, given as a GraphLang string.
#     :param results_as_dict True if a query result should be a dictionary, mapping identifiers to values.
#     :param results_per_dmrs True if a (possibly empty) list per DMRS should be returned.
#     :return Iterator of dicts containing the matching node ids.
#     """
#     queries = {}
#     search_dmrs = parse_graphlang(search_dmrs_graphlang, queries=queries)
#     queries = [(key, queries[key]) for key in sorted(queries)]
#     for dmrs in dmrs_iter:
#         assert isinstance(dmrs, Dmrs), 'Object in dmrs_iter is not a Dmrs.'
#         # perform an exact matching of search_dmrs against dmrs
#         matchings = dmrs_exact_matching(search_dmrs, dmrs)
#         if results_per_dmrs:
#             results = []
#         for matching in matchings:
#             # extract matched values
#             if results_as_dict:
#                 result = {key: query(matching, dmrs) for key, query in queries}
#             else:
#                 result = tuple(query(matching, dmrs) for _, query in queries)
#             if results_per_dmrs:
#                 results.append(result)
#             else:
#                 yield result
#         if results_per_dmrs:
#             yield results


# Path: examples/examples_query.py
from pydmrs.pydelphin_interface import parse
from pydmrs.matching.query import dmrs_query



if __name__ == '__main__':

    # basic functionality
    dmrs_list = [parse('A mouse ate the whole cheese.')[0],
                 parse('Lions eat around 15 zebras per year.')[0],
                 parse('Their children eat so many sweets.')[0],
                 parse('Potatoes are mostly eaten by humans.')[0]]
    search_dmrs = '_?1_?_?_rel i <-1- _eat_v_1_rel e? -2-> _?2_?_?_rel i'

    # not dict, not per dmrs
    results = list(dmrs_query(dmrs_list, search_dmrs, results_as_dict=False, results_per_dmrs=False))
    assert len(results) == 4
    assert ('mouse', 'cheese') in results
    assert ('lion', 'zebra') in results
    assert ('child', 'sweet') in results
    assert ('human', 'potato') in results
    # dict, not per dmrs

    results = list(dmrs_query(dmrs_list, search_dmrs, results_as_dict=True, results_per_dmrs=False))