|
| 1 | +import logging |
| 2 | + |
| 3 | +from udapi.core.block import Block |
| 4 | + |
| 5 | +from udapi.block.zellig_harris.common import * |
| 6 | +from udapi.block.zellig_harris.queries import * |
| 7 | + |
| 8 | + |
| 9 | +class Configurations(Block): |
| 10 | + """ |
| 11 | + An abstract class for four extracting scenarios. |
| 12 | +
|
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, args=None): |
| 16 | + """ |
| 17 | + Initialization. |
| 18 | +
|
| 19 | + :param args: A dict of optional parameters. |
| 20 | +
|
| 21 | + """ |
| 22 | + if args is None: |
| 23 | + args = {} |
| 24 | + |
| 25 | + # Call the constructor of the parent object. |
| 26 | + super(Configurations, self).__init__(args) |
| 27 | + |
| 28 | + # Process the 'POS' argument. |
| 29 | + self.pos = [] |
| 30 | + if 'pos' in args: |
| 31 | + self.pos = args['pos'].split(',') |
| 32 | + |
| 33 | + # Process the 'print_lemmas' argument. |
| 34 | + self.print_lemmas = False |
| 35 | + if 'print_lemmas' in args and args['print_lemmas'] == '1': |
| 36 | + self.print_lemmas = True |
| 37 | + |
| 38 | + # Process the 'print_lemmas' argument. |
| 39 | + self.verbose = False |
| 40 | + if 'verbose' in args and args['verbose'] == '1': |
| 41 | + self.verbose = True |
| 42 | + |
| 43 | + def apply_query(self, query_id, node): |
| 44 | + """ |
| 45 | + A generic method for applying a specified query on a specified node. |
| 46 | +
|
| 47 | + :param query_id: A name of the query method to be called. |
| 48 | + :param node: An input node. |
| 49 | +
|
| 50 | + """ |
| 51 | + if self.verbose: |
| 52 | + logging.info(' - applying query %s', query_id) |
| 53 | + |
| 54 | + try: |
| 55 | + methods = globals() |
| 56 | + method = methods.get(query_id) |
| 57 | + except Exception as exception: |
| 58 | + logging.fatal(' - no such query %s', query_id) |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | + triples = [] |
| 62 | + try: |
| 63 | + triples = method(node) |
| 64 | + except ValueError as exception: |
| 65 | + if self.verbose: |
| 66 | + logging.info(' - no configurations: %s', exception) |
| 67 | + pass |
| 68 | + |
| 69 | + if len(triples) == 0: |
| 70 | + if self.verbose: |
| 71 | + logging.info(' - no configurations, but all conditions passed.') |
| 72 | + |
| 73 | + for (node_a, relation_name, node_b) in triples: |
| 74 | + print_triple(node_a, relation_name, node_b, |
| 75 | + print_lemma=self.print_lemmas) |
| 76 | + |
| 77 | + def process_tree(self, tree): |
| 78 | + """ |
| 79 | + If required, print detailed info about the processed sentence. |
| 80 | +
|
| 81 | + :param tree: A sentence to be processed. |
| 82 | +
|
| 83 | + """ |
| 84 | + if self.verbose: |
| 85 | + logging.info('') |
| 86 | + logging.info('---') |
| 87 | + logging.info('Sentence ID : %s', tree.sent_id) |
| 88 | + logging.info('Sentence : %s', ' '.join([node.form for node in tree.descendants()])) |
| 89 | + logging.info('---') |
| 90 | + |
| 91 | + for node in tree.descendants(): |
| 92 | + self.process_node(node) |
| 93 | + |
| 94 | + def process_node(self, node): |
| 95 | + """ |
| 96 | + Extract context configuration for verbs according to (Vulic et al., 2016). |
| 97 | +
|
| 98 | + :param node: A node to be process. |
| 99 | +
|
| 100 | + """ |
| 101 | + raise NotImplementedError('Cannot call this abstract method.') |
0 commit comments