From 8bffd0b590a9eed9ec23497c595049d648558802 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 22 May 2024 15:47:42 +0200 Subject: [PATCH] replace print statements with logging --- mwe_query/canonicalform.py | 34 ++++++++++++++++------------------ mwe_query/lcat.py | 10 ++++++---- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/mwe_query/canonicalform.py b/mwe_query/canonicalform.py index 3612cea..07c2ec8 100644 --- a/mwe_query/canonicalform.py +++ b/mwe_query/canonicalform.py @@ -5,6 +5,7 @@ from typing import Dict, List, Optional, Set, Tuple from sastadev.sastatypes import SynTree +import logging import re import sys from sastadev.treebankfunctions import getattval as gav, terminal, getnodeyield, find1, bareindexnode, indextransform, \ @@ -16,6 +17,8 @@ from sastadev.alpinoparsing import parse from mwe_query.lcat import expandnonheadwords +log = logging.getLogger() + Xpathexpression = str space = ' ' @@ -247,9 +250,8 @@ def preprocess_MWE(rawmwe: str) -> List[Tuple[str, int]]: # noqa: C901 newann = noann newword = word else: - print(f'illegal state: {state} for {rawmwe}', file=sys.stderr) - print(f'mwe={mwe}', file=sys.stderr) - exit(-1) + log.debug('illegal state: %s for %s: mwe=%s', state, rawmwe, mwe) + raise RuntimeError(f'illegal state: {state} for {rawmwe}') ann_list.append((newword, newann)) return ann_list @@ -330,11 +332,10 @@ def headmodifiable(stree, mwetop, annotations): elif mwetop in {itop, parenttop}: result = annotations[beginint] not in nomodanns else: - print(f'Illegal value for mwetop={mwetop}', file=sys.stderr) + log.warning('Illegal value for mwetop=%s', mwetop) result = False else: - print( - f'Index out of range: {beginint} in {annotations}', file=sys.stderr) + log.warning(f'Index out of range: %s in %s', beginint, annotations) result = False else: # can now only be node with cat=mwu mwps = getnodeyield(head) @@ -345,7 +346,7 @@ def headmodifiable(stree, mwetop, annotations): result = any([annotations[int(gav(mwp, 'begin'))] not in nomodanns for mwp in mwps]) else: - print(f'Illegal value for mwetop={mwetop}', file=sys.stderr) + log.warning('Illegal value for mwetop=%s', mwetop) result = False return result @@ -372,8 +373,7 @@ def zerochildrencount(stree, annotations): if annotations[intbegin] == zero: result += 1 else: - print( - f'Index out of range: {intbegin} in {annotations}', file=sys.stderr) + log.warning('Index out of range: %d in %s', intbegin, annotations) return result @@ -551,13 +551,13 @@ def transformtree(stree: SynTree, annotations: List[Annotation], mwetop=notop, a for newchild in newchildlist: if newchild is not None: if DEBUG: - print('\nnewchild:') + log.debug('\nnewchild:') ET.dump(newchild) # we must make a copy of the child because each Element has only one parent newchildcopy = copy.copy(newchild) newnodecopy.append(newchildcopy) if DEBUG: - print('\n\nnewnodecopy:') + log.debug('\n\nnewnodecopy:') ET.dump(newnodecopy) results.append(newnodecopy) else: @@ -573,8 +573,7 @@ def transformtree(stree: SynTree, annotations: List[Annotation], mwetop=notop, a pt = gav(stree, 'pt') rel = gav(stree, 'rel') if not (0 <= beginint < len(annotations)): - print( - f'Index out of range: {beginint} in {annotations}', file=sys.stderr) + log.warning('Index out of range: %d in %s', beginint, annotations) # we simply skip this node # newnode = None else: @@ -649,19 +648,18 @@ def transformtree(stree: SynTree, annotations: List[Annotation], mwetop=notop, a newnode.attrib['pt'] = 'vnw' results.append(newnode) else: - print( - f'Unrecognized annotation: {annotations[beginint]}', file=sys.stderr) + log.warning('Unrecognized annotation: %s', annotations[beginint]) newnode = attcopy( stree, ['lemma', 'rel', 'pt'] + subcatproperties + inflproperties) results.append(newnode) if DEBUG: - print('results:') + log.debug('results:') for result in results: if result is None: - print('None') + log.debug('None') else: - ET.dump(result) + log.debug(ET.tostring(result)) return results diff --git a/mwe_query/lcat.py b/mwe_query/lcat.py index 76117f5..c0c33e7 100644 --- a/mwe_query/lcat.py +++ b/mwe_query/lcat.py @@ -6,8 +6,12 @@ from sastadev.sastatypes import SynTree from sastadev.treebankfunctions import getattval as gav, terminal, allcats as validcats, find1 import copy +import logging import lxml.etree as ET + +log = logging.getLogger() + dummy = 'dummy' @@ -182,11 +186,9 @@ def getlcat(node: SynTree, prel=None) -> str: # noqa: C901 elif pt == dummy: result = None else: - print('Unknown att value (pt) encountered in:') - ET.dump(node) + log.warning('Unknown att value (pt) encountered in: %s', ET.tostring(node)) result = None if result == 'xp': - print('Unexpected att value encountered in:') - ET.dump(node) + log.warning('Unexpected att value encountered in: %s', ET.tostring(node)) return result