Beispiel #1
0
def convert_coq_to_nltk_type(coq_type):
    """
    Given a coq_type specification such as:
      Parameter _love : Entity -> Entity -> Prop.
    return the equivalent NLTK type specification:
      {'_love' : read_type('<e, <e, t>>')}
    """
    assert isinstance(coq_type, str)
    coq_type_list = coq_type.split()
    assert len(coq_type_list) >= 4, 'Wrong coq_type format: %s' % coq_type
    parameter, surface, colon = coq_type_list[:3]
    assert parameter == 'Parameter' and colon == ':'
    # This list contains something like ['Entity', '->', 'Prop', '->', 'Prop'...]
    type_sig = coq_type_list[3:]
    nltk_type_str = ' '.join(type_sig).rstrip('.').replace('->', ' ').replace(
        'Entity', 'e').replace('Prop', 't').replace('Event', 'v')
    if not nltk_type_str.startswith('(') or not nltk_type_str.endswith('('):
        nltk_type_str = '(' + nltk_type_str + ')'
    # Add pre-terminals (necessary for NLTK, if we convert to CNF).
    nltk_type_str = re.sub(r'([evt])', r'(N \1)', nltk_type_str)
    nltk_type_tree = tree_or_string(nltk_type_str)
    nltk_type_tree.chomsky_normal_form(factor='right')
    nltk_type_str = remove_labels_and_unaries(nltk_type_tree).replace(
        '( ', '(').replace('(', '<').replace(')', '>').replace(' ', ',')
    if len(type_sig) == 1:
        nltk_type_str = nltk_type_str.strip('<>')
    return {surface: read_type(nltk_type_str)}
 def test_premise_case_var(self):
     coq_line = 'H2 : _woman (Acc x1)'
     expected_args = tree_or_string('(Acc x1)')
     args = get_tree_pred_args(coq_line)
     self.assertEqual(expected_args, args)
def parse_coq_line(coq_line):
    try:
        tree_args = tree_or_string('(' + coq_line + ')')
    except ValueError:
        tree_args = None
    return tree_args
 def test_premise_one_var(self):
     coq_line = 'H2 : _kiss x1'
     expected_args = tree_or_string('x1')
     args = get_tree_pred_args(coq_line)
     self.assertEqual(expected_args, args)