Skip to content

Commit

Permalink
replace print statements with logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bbonf committed May 22, 2024
1 parent 8d89be8 commit 8bffd0b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
34 changes: 16 additions & 18 deletions mwe_query/canonicalform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand All @@ -16,6 +17,8 @@
from sastadev.alpinoparsing import parse
from mwe_query.lcat import expandnonheadwords

log = logging.getLogger()

Xpathexpression = str

space = ' '
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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


Expand Down
10 changes: 6 additions & 4 deletions mwe_query/lcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand Down Expand Up @@ -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

0 comments on commit 8bffd0b

Please sign in to comment.