Skip to content

Commit

Permalink
Updating built-in functions (issue #79).
Browse files Browse the repository at this point in the history
  • Loading branch information
marcverhagen committed Dec 30, 2020
1 parent aad98ac commit a8bc653
Show file tree
Hide file tree
Showing 55 changed files with 431 additions and 356 deletions.
47 changes: 24 additions & 23 deletions components/blinker/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

from __future__ import absolute_import
from __future__ import print_function
import re

from utilities import logger
Expand Down Expand Up @@ -138,7 +139,7 @@ def _run_blinker(self):
sentence = self.doctree[si]
r3_main_event = None
if _DEBUG:
print "processing sentence", si
print("processing sentence", si)

# iterate over elements within a sentence
for i in range(len(sentence)):
Expand Down Expand Up @@ -168,7 +169,7 @@ def _run_blinker(self):
and element.isVerbChunk() \
and event.attrs['class'] == 'REPORTING':
if _DEBUG:
print "applying type 5 rules"
print("applying type 5 rules")
self._apply_type5_rules(sentence, event, i)

# R3: if no main event in sentence
Expand All @@ -195,7 +196,7 @@ def _apply_type3_rules(self, event1, event2):
event2.attrs[EIID],
"%s-Rule-%s" % (BLINKER, rule.id))
if _DEBUG:
print "RULE %s fired!" % rule.rule_number
print("RULE %s fired!" % rule.rule_number)
return

def _apply_type5_rules(self, sentence, event1, position):
Expand All @@ -219,11 +220,11 @@ def _apply_type5_rules(self, sentence, event1, position):
# forward

if _DEBUG:
print "inside rule application function"
print("inside rule application function")
sentence.pretty_print()
for i in range(position+1, len(sentence)):
if _DEBUG:
print "processing element", i
print("processing element", i)
element = sentence[i]

# quote
Expand Down Expand Up @@ -257,7 +258,7 @@ def _apply_type5_rules(self, sentence, event1, position):
event2.attrs[EIID],
"%s-Rule-%s" % (BLINKER, rule.id))
if _DEBUG:
print "RULE %s fired!" % rule.rule_number
print("RULE %s fired!" % rule.rule_number)
# apply the first matching rule
return

Expand All @@ -268,7 +269,7 @@ def _apply_type5_rules(self, sentence, event1, position):
direct = 'INDIRECT'
for i in range(position-1, -1, -1): # ..,3,2,1,0
if _DEBUG:
print "processing element", i
print("processing element", i)
element = sentence[i]

# quote
Expand All @@ -286,20 +287,20 @@ def _apply_type5_rules(self, sentence, event1, position):
if direct in rule.attrs['sentType']]
if _DEBUG:
_pp_events(event1, event2)
print "Applying rules for sentence type:", \
direct, len(current_rules), "rules"
print("Applying rules for sentence type:", \
direct, len(current_rules), "rules")
for rule in current_rules:
# if attribute not set in the rule, accept any value
for att in ['class', 'tense', 'aspect']:
if 'arg2.'+att not in rule.attrs:
rule.attrs['arg2.'+att] = [event2.attrs[att]]
if _DEBUG:
print "RULE %s (%s):" % (rule.rule_number,
rule.attrs['sentType'][0])
print rule.attrs['arg1.class'], rule.attrs['arg1.tense'], \
rule.attrs['arg1.aspect']
print rule.attrs['arg2.class'], rule.attrs['arg2.tense'], \
rule.attrs['arg2.aspect']
print("RULE %s (%s):" % (rule.rule_number,
rule.attrs['sentType'][0]))
print(rule.attrs['arg1.class'], rule.attrs['arg1.tense'], \
rule.attrs['arg1.aspect'])
print(rule.attrs['arg2.class'], rule.attrs['arg2.tense'], \
rule.attrs['arg2.aspect'])
# check that specified values match
if event2.attrs['class'] in rule.attrs['arg2.class'] and \
event2.attrs['tense'] in rule.attrs['arg2.tense'] and \
Expand All @@ -311,7 +312,7 @@ def _apply_type5_rules(self, sentence, event1, position):
event2.attrs['eiid'],
origin)
if _DEBUG:
print "RULE %s fired!" % rule.rule_number
print("RULE %s fired!" % rule.rule_number)
# apply the first matching rule
return

Expand Down Expand Up @@ -346,8 +347,8 @@ def _apply_event_anchoring_rules(self, sentence, timex, i):
eiid = event.attrs[EIID]
tid = timex.attrs[TID]
if _DEBUG:
print "FOUND: [%s] %s [%s] --> %s" % \
(event.dtrs[0].getText(), signal, timex.getText(), rel)
print("FOUND: [%s] %s [%s] --> %s" % \
(event.dtrs[0].getText(), signal, timex.getText(), rel))
self._add_tlink(rel, eiid, tid, "%s-Type-2-%s" % (BLINKER, signal))
return

Expand Down Expand Up @@ -439,8 +440,8 @@ def _fix_timex_val(date):


def _pp_events(event1, event2):
print(event1.dtrs[0].getText(), event2.dtrs[0].getText())
print(' e1', event1.dtrs[0].getText(), event1.attrs['class'],
event1.attrs['tense'], event1.attrs['aspect'])
print(' e2', event2.dtrs[0].getText(), event2.attrs['class'],
event2.attrs['tense'], event2.attrs['aspect'])
print((event1.dtrs[0].getText(), event2.dtrs[0].getText()))
print((' e1', event1.dtrs[0].getText(), event1.attrs['class'],
event1.attrs['tense'], event1.attrs['aspect']))
print((' e2', event2.dtrs[0].getText(), event2.attrs['class'],
event2.attrs['tense'], event2.attrs['aspect']))
3 changes: 2 additions & 1 deletion components/btime/btime-wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class will at least have methods text(), tag(), begin(), end() and __unicode__()


from __future__ import absolute_import
from __future__ import print_function
import sys, codecs


Expand All @@ -50,7 +51,7 @@ def parse_file(filename):

def parse_sentence(sentence):
"""This is where BTime gets folded in."""
print "processing \"%s\"" % ' '.join([t.text for t in sentence])
print("processing \"%s\"" % ' '.join([t.text for t in sentence]))



Expand Down
9 changes: 5 additions & 4 deletions components/classifier/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

from __future__ import absolute_import
from __future__ import print_function
import os, codecs, textwrap

from components.common_modules.tree import create_tarsqi_tree
Expand Down Expand Up @@ -411,18 +412,18 @@ def _set_feature_SIGNAL(self):

def _debug(text):
if DEBUG:
print text
print(text)


def _debug_vector(vector_type, vector, indent=''):
if DEBUG:
text = "%s %s%s" % (vector_type, vector, indent)
for line in textwrap.wrap(text, 100):
print "%s%s" % (indent, line)
print("%s%s" % (indent, line))


def _debug_leafnodes(element, s):
if DEBUG:
print("\n%s %s\n" % (element.begin, s))
print(("\n%s %s\n" % (element.begin, s)))
for n in s.leaf_nodes():
print ' ', n
print(' ', n)
9 changes: 5 additions & 4 deletions components/common_modules/chunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

from __future__ import absolute_import
from __future__ import print_function
import types
from xml.sax.saxutils import quoteattr

Expand Down Expand Up @@ -198,9 +199,9 @@ def isChunk(self):
return True

def pretty_print(self, indent=0):
print "%s<%s position=%s %d-%d checkedEvents=%s event=%s eid=%s>" % \
print("%s<%s position=%s %d-%d checkedEvents=%s event=%s eid=%s>" % \
(indent * ' ', self.__class__.__name__, self.position,
self.begin, self.end, self.checkedEvents, self.event, self.eid)
self.begin, self.end, self.checkedEvents, self.event, self.eid))
for tok in self.dtrs:
tok.pretty_print(indent + 2)

Expand Down Expand Up @@ -598,7 +599,7 @@ def _identify_substring(self, sentence_slice, fsa_list):
lenSubstring = fsa.acceptsSubstringOf(sentence_slice)
if lenSubstring:
if DEBUG:
print "Succesful application of %s" % fsa.fsaname
print("Succesful application of %s" % fsa.fsaname)
return (lenSubstring, fsaCounter)
else:
return (0, fsaCounter)
Expand Down Expand Up @@ -635,7 +636,7 @@ def _debug_vcf(vcf_list):
if len(vcf_list) > 0 and DEBUG:
for vcf in vcf_list:
if DEBUG:
print ' ',
print(' ', end=' ')
vcf.pp()
for vcf in vcf_list:
logger.debug(vcf.as_verbose_string())
3 changes: 2 additions & 1 deletion components/common_modules/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


from __future__ import absolute_import
from __future__ import print_function
from utilities import logger


Expand All @@ -22,7 +23,7 @@ def process(self, infile, outfile):
def pp_doctree(self, componentName):
"""Print the document tree. Assumes there is a doctree instance variable that
contains a TarsqiTree object."""
print "\n--------- DOCUMENT TREE for %s ----------" % componentName
print("\n--------- DOCUMENT TREE for %s ----------" % componentName)
self.doctree.pretty_print()


3 changes: 2 additions & 1 deletion components/common_modules/constituent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import
from __future__ import print_function
from types import ListType, TupleType
from pprint import pprint

Expand Down Expand Up @@ -274,7 +275,7 @@ def print_vars(self):
pprint(vars(self))

def pretty_print(self):
print "<<pretty_print() not defined for this object>>"
print("<<pretty_print() not defined for this object>>")

# SLINKET METHODS
# There is some serious redundancy here, refactor these methods.
Expand Down
9 changes: 5 additions & 4 deletions components/common_modules/sentence.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Contains functionality specific to sentences in a tree."""

from __future__ import absolute_import
from __future__ import print_function
from utilities import logger
from components.common_modules.constituent import Constituent

Expand Down Expand Up @@ -51,11 +52,11 @@ def set_event_list(self):
def pretty_print(self, tree=True, verbose=False, indent=0):
"""Pretty print the sentence by pretty printing all daughters"""
if verbose:
print "SENTENCE %s\n" % self.position
print " parent = %s" % self.parent
print " eventList = %s\n" % self.eventList
print("SENTENCE %s\n" % self.position)
print(" parent = %s" % self.parent)
print(" eventList = %s\n" % self.eventList)
else:
print "%s<Sentence position=%s %d-%d>" % (indent*' ', self.position, self.begin, self.end)
print("%s<Sentence position=%s %d-%d>" % (indent*' ', self.position, self.begin, self.end))
if tree or verbose:
for dtr in self.dtrs:
dtr.pretty_print(indent=indent+2)
Expand Down
9 changes: 5 additions & 4 deletions components/common_modules/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


from __future__ import absolute_import
from __future__ import print_function
from library.main import LIBRARY
from components.common_modules.constituent import Constituent
from utilities import logger
Expand Down Expand Up @@ -100,9 +101,9 @@ def isEvent(self):
def pretty_print(self, indent=0):
(eid, eiid, cl) = (self.attrs.get('eid'), self.attrs.get('eiid'),
self.attrs.get('class'))
print "%s<%s position=%s %d-%d eid=%s eiid=%s class=%s>" % \
print("%s<%s position=%s %d-%d eid=%s eiid=%s class=%s>" % \
(indent * ' ', self.__class__.__name__, self.position,
self.begin, self.end, eid, eiid, cl)
self.begin, self.end, eid, eiid, cl))
for dtr in self.dtrs:
dtr.pretty_print(indent+2)

Expand Down Expand Up @@ -133,9 +134,9 @@ def isTimex(self):
return True

def pretty_print(self, indent=0):
print "%s<%s tid=%s type=%s value=%s>" % \
print("%s<%s tid=%s type=%s value=%s>" % \
(indent * ' ', self.__class__.__name__, self.attrs.get('tid'),
self.attrs.get('type'), self.attrs.get('value'))
self.attrs.get('type'), self.attrs.get('value')))
for dtr in self.dtrs:
dtr.pretty_print(indent+2)

Expand Down
5 changes: 3 additions & 2 deletions components/common_modules/tokens.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

from __future__ import absolute_import
from __future__ import print_function
from library import forms
from library.main import LIBRARY
from components.evita.event import Event
Expand Down Expand Up @@ -120,9 +121,9 @@ def pretty_print(self, indent=0):
eid = self.event_tag.attrs.get('eid')
eiid = self.event_tag.attrs.get('eiid')
event_string = " eid=%s eiid=%s" % (eid, eiid)
print "%s<%s position=%d %d-%d pos=%s text=%s%s>" % \
print("%s<%s position=%d %d-%d pos=%s text=%s%s>" % \
(indent * ' ', self.__class__.__name__, self.position,
self.begin, self.end, self.pos, self.getText(), event_string)
self.begin, self.end, self.pos, self.getText(), event_string))


class AdjectiveToken(Token):
Expand Down
23 changes: 12 additions & 11 deletions components/common_modules/tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Contains the TarsqiTree class."""

from __future__ import absolute_import
from __future__ import print_function
import sys
import re
from xml.sax.saxutils import escape, quoteattr
Expand Down Expand Up @@ -295,7 +296,7 @@ def as_tree_element(self):
return tree_element

def pp(self, indent=0):
print "%s%s" % (indent * ' ', self)
print("%s%s" % (indent * ' ', self))
for dtr in self.dtrs:
dtr.pp(indent + 1)

Expand Down Expand Up @@ -437,32 +438,32 @@ def pp(self):
def pretty_print(self):
"""Pretty printer that prints all instance variables and a neat representation
of the sentences."""
print "\n<TarsqiTree filename=%s>\n" % self.tarsqidoc.sourcedoc.filename
print "len(dtrs) = %s" % (len(self.dtrs))
print("\n<TarsqiTree filename=%s>\n" % self.tarsqidoc.sourcedoc.filename)
print("len(dtrs) = %s" % (len(self.dtrs)))
self.pretty_print_tagged_events_dict()
self.pretty_print_sentences()
self.pretty_print_links(self.alinks)
self.pretty_print_links(self.slinks)
self.pretty_print_links(self.tlinks)

def pretty_print_tagged_events_dict(self):
print 'events = {',
print('events = {', end=' ')
eids = sorted(self.events.keys())
for eid in eids:
print "\n ", eid, '=> {',
print("\n ", eid, '=> {', end=' ')
attrs = list(self.events[eid].keys())
attrs.sort()
for attr in attrs:
print "%s=%s" % (attr, str(self.events[eid][attr])),
print '}'
print '}'
print("%s=%s" % (attr, str(self.events[eid][attr])), end=' ')
print('}')
print('}')

def pretty_print_sentences(self):
for sentence in self:
print
print()
sentence.pretty_print(verbose=False)
print
print()

def pretty_print_links(self, links):
for link in links:
print ' ', link
print(' ', link)
Loading

0 comments on commit a8bc653

Please sign in to comment.