1#! /usr/bin/env python
2# -*- coding: utf8 -*-
3"""Link Grammar example usage"""
4
5from __future__ import print_function, division  # We require Python 2.6 or later
6
7from linkgrammar import Sentence, ParseOptions, Dictionary, Clinkgrammar as clg
8
9print ("Version:", clg.linkgrammar_get_version())
10po = ParseOptions(verbosity=1)
11
12def desc(lkg):
13    print (lkg.diagram())
14    print ('Postscript:')
15    print (lkg.postscript())
16    print ('---')
17
18def s(q):
19    return '' if q == 1 else 's'
20
21def linkage_stat(psent, lang, lkgs, sent_po):
22    """
23    This function mimics the linkage status report style of link-parser
24    """
25    random = ' of {} random linkages'. \
26             format(clg.sentence_num_linkages_post_processed((psent._obj))) \
27             if clg.sentence_num_linkages_found(psent._obj) > sent_po.linkage_limit else ''
28
29    print ('{}: Found {} linkage{} ({}{} had no P.P. violations)'. \
30          format(lang, clg.sentence_num_linkages_found(psent._obj),
31                 s(clg.sentence_num_linkages_found(psent._obj)), len(lkgs), random))
32
33
34en_lines = [
35    'This is a test.',
36    'I feel is the exciter than other things', # from issue #303 (10 linkages)
37]
38
39po = ParseOptions(min_null_count=0, max_null_count=999)
40#po.linkage_limit = 3
41
42# English is the default language
43en_dir = Dictionary() # open the dictionary only once
44for text in en_lines:
45    sent = Sentence(text, en_dir, po)
46    linkages = sent.parse()
47    linkage_stat(sent, 'English', linkages, po)
48    for linkage in linkages:
49        desc(linkage)
50
51# Russian
52sent = Sentence("Целью курса является обучение магистрантов основам построения и функционирования программного обеспечения сетей ЭВМ.", Dictionary('ru'), po)
53linkages = sent.parse()
54linkage_stat(sent, 'Russian', linkages, po)
55for linkage in linkages:
56    desc(linkage)
57
58# Turkish
59po = ParseOptions(islands_ok=True, max_null_count=1, display_morphology=True, verbosity=1)
60sent = Sentence("Senin ne istediğini bilmiyorum", Dictionary('tr'), po)
61linkages = sent.parse()
62linkage_stat(sent, 'Turkish', linkages, po)
63for linkage in linkages:
64    desc(linkage)
65
66# Prevent interleaving "Dictionary close" messages
67po = ParseOptions(verbosity=0)
68