Пример #1
0
 def test_setting_all_short_connectors(self):
     po = ParseOptions()
     po.all_short_connectors = True
     self.assertEqual(po.all_short_connectors, True)
     self.assertEqual(clg.parse_options_get_all_short_connectors(po._obj), 1)
     po.all_short_connectors = False
     self.assertEqual(po.all_short_connectors, False)
     self.assertEqual(clg.parse_options_get_all_short_connectors(po._obj), 0)
Пример #2
0
 def test_setting_islands_ok(self):
     po = ParseOptions()
     po.islands_ok = True
     self.assertEqual(po.islands_ok, True)
     self.assertEqual(clg.parse_options_get_islands_ok(po._obj), 1)
     po.islands_ok = False
     self.assertEqual(po.islands_ok, False)
     self.assertEqual(clg.parse_options_get_islands_ok(po._obj), 0)
Пример #3
0
 def test_setting_display_morphology(self):
     po = ParseOptions()
     po.display_morphology = True
     self.assertEqual(po.display_morphology, True)
     self.assertEqual(clg.parse_options_get_display_morphology(po._obj), 1)
     po.display_morphology = False
     self.assertEqual(po.display_morphology, False)
     self.assertEqual(clg.parse_options_get_display_morphology(po._obj), 0)
Пример #4
0
def setUpModule():
    datadir = os.getenv("LINK_GRAMMAR_DATA", "")
    if datadir:
        clg.dictionary_set_data_dir(datadir)

    clg.test_data_srcdir = os.getenv("srcdir")
    if clg.test_data_srcdir:
        clg.test_data_srcdir += "/"
    else:
        clg.test_data_srcdir = ""
Пример #5
0
def linkage_stat(psent, lang, lkgs, sent_po):
    """
    This function mimics the linkage status report style of link-parser
    """
    random = ' of {} random linkages'. \
             format(clg.sentence_num_linkages_post_processed((psent._obj))) \
             if clg.sentence_num_linkages_found(psent._obj) > sent_po.linkage_limit else ''

    print ('{}: Found {} linkage{} ({}{} had no P.P. violations)'. \
          format(lang, clg.sentence_num_linkages_found(psent._obj),
                 s(clg.sentence_num_linkages_found(psent._obj)), len(lkgs), random))
Пример #6
0
 def test_setting_verbosity(self):
     po = ParseOptions()
     po.verbosity = 2
     #Ensure that the PO object reports the value correctly
     self.assertEqual(po.verbosity, 2)
     #Ensure that it's actually setting it.
     self.assertEqual(clg.parse_options_get_verbosity(po._obj), 2)
Пример #7
0
def linkage_testfile(self, lgdict, popt, desc = ''):
    """
    Reads sentences and their corresponding
    linkage diagrams / constituent printings.
    """
    if '' != desc:
        desc = desc + '-'
    testfile = clg.test_data_srcdir + "parses-" + desc + clg.dictionary_get_lang(lgdict._obj) + ".txt"
    parses = open(testfile)
    diagram = None
    sent = None
    lineno = 0
    for line in parses:
        lineno += 1
        # Lines starting with I are the input sentences
        if 'I' == line[0]:
            sent = line[1:]
            diagram = ""
            constituents = ""
            linkages = Sentence(sent, lgdict, popt).parse()
            linkage = linkages.next()

        # Generate the next linkage of the last input sentence
        if 'N' == line[0]:
            diagram = ""
            constituents = ""
            linkage = next(linkages, None)
            if not linkage:
                self.assertTrue(linkage, "{}:{}: Sentence has too few linkages".format(testfile, lineno))

        # Lines starting with O are the parse diagram
        # It ends with an empty line
        if 'O' == line[0]:
            diagram += line[1:]
            if '\n' == line[1] and 1 < len(diagram):
                self.assertEqual(linkage.diagram(), diagram)

        # Lines starting with C are the constituent output (type 1)
        # It ends with an empty line
        if 'C' == line[0]:
            if '\n' == line[1] and 1 < len(constituents):
                self.assertEqual(linkage.constituent_tree(), constituents)
            constituents += line[1:]
    parses.close()
Пример #8
0
 def test_setting_min_null_count(self):
     po = ParseOptions()
     po.min_null_count = 3
     self.assertEqual(clg.parse_options_get_min_null_count(po._obj), 3)
Пример #9
0
 def test_setting_disjunct_cost(self):
     po = ParseOptions()
     po.disjunct_cost = 3.0
     self.assertEqual(clg.parse_options_get_disjunct_cost(po._obj), 3.0)
Пример #10
0
 def test_setting_linkage_limit(self):
     po = ParseOptions()
     po.linkage_limit = 3
     self.assertEqual(clg.parse_options_get_linkage_limit(po._obj), 3)
Пример #11
0
def linkage_testfile(self, lgdict, popt, desc = ''):
    """
    Reads sentences and their corresponding
    linkage diagrams / constituent printings.
    """
    self.__class__.longMessage = True
    if desc != '':
        desc = desc + '-'
    testfile = clg.test_data_srcdir + "parses-" + desc + clg.dictionary_get_lang(lgdict._obj) + ".txt"
    parses = open(testfile, "rb")
    diagram = None
    constituents = None
    sent = None
    lineno = 0

    # Function code and file format sanity check
    self.opcode_detected = 0
    def validate_opcode(ctxt=self, O=False, C=False):
        ctxt.opcode_detected += 1
        if O:
            self.assertFalse(diagram, "at {}:{}: Unfinished diagram entry".format(testfile, lineno))
        if C:
            self.assertFalse(constituents, "at {}:{}: Unfinished constituents entry".format(testfile, lineno))

    for line in parses:
        lineno += 1
        if sys.version_info > (3, 0):
            line = line.decode('utf-8')
        # Lines starting with I are the input sentences
        if line[0] == 'I':
            validate_opcode(O=True, C=True)
            sent = line[1:]
            diagram = ""
            constituents = ""
            linkages = Sentence(sent, lgdict, popt).parse()
            linkage = next(linkages, None)
            self.assertTrue(linkage, "at {}:{}: Sentence has no linkages".format(testfile, lineno))

        # Generate the next linkage of the last input sentence
        if line[0] == 'N' :
            validate_opcode(O=True, C=True)
            diagram = ""
            constituents = ""
            linkage = next(linkages, None)
            self.assertTrue(linkage, "at {}:{}: Sentence has too few linkages".format(testfile, lineno))

        # Lines starting with O are the parse diagram
        # It ends with an empty line
        if line[0] == 'O' :
            validate_opcode(C=True)
            diagram += line[1:]
            if line[1] == '\n' and len(diagram) > 1:
                self.assertEqual(linkage.diagram(), diagram, "at {}:{}".format(testfile, lineno))
                diagram = None

        # Lines starting with C are the constituent output (type 1)
        # It ends with an empty line
        if line[0] == 'C':
            validate_opcode(O=True)
            if line[1] == '\n' and len(constituents) > 1:
                self.assertEqual(linkage.constituent_tree(), constituents, "at {}:{}".format(testfile, lineno))
                constituents = None
            else:
                constituents += line[1:]
    parses.close()

    validate_opcode(O=True, C=True)
    self.assertGreaterEqual(self.opcode_detected, 2, "Nothing has been done for " + testfile)
Пример #12
0
    unittest.TestCase.assertRegexpMatches = unittest.TestCase.assertRegex

import lg_testutils  # Found in the same directory of this test script

# Show information on this program run
print('Running by:', sys.executable)
print('Running {} in:'.format(sys.argv[0]), os.getcwd())
for v in 'PYTHONPATH', 'srcdir', 'LINK_GRAMMAR_DATA':
    print('{}={}'.format(v, os.environ.get(v)))
#===

from linkgrammar import (Sentence, Linkage, ParseOptions, Link, Dictionary,
                         LG_Error, LG_DictionaryError, LG_TimerExhausted,
                         Clinkgrammar as clg)

print(clg.linkgrammar_get_configuration())

# Show the location and version of the bindings modules
for imported_module in 'linkgrammar$', 'clinkgrammar', '_clinkgrammar', 'lg_testutils':
    module_found = False
    for module in sys.modules:
        if re.search(r'^(linkgrammar\.)?' + imported_module, module):
            print("Using", sys.modules[module], end='')
            if hasattr(sys.modules[module], '__version__'):
                print(' version', sys.modules[module].__version__, end='')
            print()
            module_found = True
    if not module_found:
        print("Warning: Module", imported_module, "not loaded.")

sys.stdout.flush()
Пример #13
0
 def test_setting_max_null_count(self):
     po = ParseOptions()
     po.max_null_count = 3
     self.assertEqual(clg.parse_options_get_max_null_count(po._obj), 3)
Пример #14
0
 def test_setting_max_parse_time(self):
     po = ParseOptions()
     po.max_parse_time = 3
     self.assertEqual(clg.parse_options_get_max_parse_time(po._obj), 3)
Пример #15
0
 def test_specifying_parse_options(self):
     po = ParseOptions(linkage_limit=99)
     self.assertEqual(clg.parse_options_get_linkage_limit(po._obj), 99)
Пример #16
0
 def test_setting_linkage_limit(self):
     po = ParseOptions()
     po.linkage_limit = 3
     self.assertEqual(clg.parse_options_get_linkage_limit(po._obj), 3)
Пример #17
0
 def test_setting_short_length(self):
     po = ParseOptions()
     po.short_length = 3
     self.assertEqual(clg.parse_options_get_short_length(po._obj), 3)
Пример #18
0
 def test_setting_short_length(self):
     po = ParseOptions()
     po.short_length = 3
     self.assertEqual(clg.parse_options_get_short_length(po._obj), 3)
Пример #19
0
#! /usr/bin/env python
# -*- coding: utf8 -*-
"""Link Grammar example usage"""

from __future__ import print_function, division  # We require Python 2.6 or later

from linkgrammar import Sentence, ParseOptions, Dictionary, Clinkgrammar as clg

print ("Version:", clg.linkgrammar_get_version())
po = ParseOptions(verbosity=1)

def desc(lkg):
    print (lkg.diagram())
    print ('Postscript:')
    print (lkg.postscript())
    print ('---')

def s(q):
    return '' if q == 1 else 's'

def linkage_stat(psent, lang, lkgs, sent_po):
    """
    This function mimics the linkage status report style of link-parser
    """
    random = ' of {} random linkages'. \
             format(clg.sentence_num_linkages_post_processed((psent._obj))) \
             if clg.sentence_num_linkages_found(psent._obj) > sent_po.linkage_limit else ''

    print ('{}: Found {} linkage{} ({}{} had no P.P. violations)'. \
          format(lang, clg.sentence_num_linkages_found(psent._obj),
                 s(clg.sentence_num_linkages_found(psent._obj)), len(lkgs), random))
Пример #20
0
 def test_specifying_parse_options(self):
     po = ParseOptions(linkage_limit=99)
     self.assertEqual(clg.parse_options_get_linkage_limit(po._obj), 99)
Пример #21
0
 def test_setting_max_parse_time(self):
     po = ParseOptions()
     po.max_parse_time = 3
     self.assertEqual(clg.parse_options_get_max_parse_time(po._obj), 3)
Пример #22
0
 def test_setting_disjunct_cost(self):
     po = ParseOptions()
     po.disjunct_cost = 3.0
     self.assertEqual(clg.parse_options_get_disjunct_cost(po._obj), 3.0)