Skip to content

Commit

Permalink
Make Python scripts work with older versions of Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
pebbe committed Feb 4, 2024
1 parent 4f9c123 commit 6887468
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
8 changes: 5 additions & 3 deletions Generation/fluency/CreateLM/sfr2frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
# adapted by GvN: now undoes MWU, because that is how the model is applied
# in the generator
import sys
import io, sys

def is_mwu(tag):
result=False
Expand All @@ -27,8 +27,10 @@ def is_mwu(tag):
curId = ''
frames = []

sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')
# sys.stdin.reconfigure(encoding='utf-8')
# sys.stdout.reconfigure(encoding='utf-8')
sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf-8', newline=None)
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8', newline=None, line_buffering=True)

for line in sys.stdin:
lineParts = line.split('|')
Expand Down
8 changes: 5 additions & 3 deletions Generation/fluency/CreateLM/sfr2words.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#
#

import sys
import io, sys

if __name__ == '__main__':
curId = ''
frames = []

sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')
# sys.stdin.reconfigure(encoding='utf-8')
# sys.stdout.reconfigure(encoding='utf-8')
sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf-8', newline=None)
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8', newline=None, line_buffering=True)

for line in sys.stdin:
lineParts = line.split('|')
Expand Down
8 changes: 6 additions & 2 deletions Generation/fluency/CreateMaxent/evaluate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import argparse
import io
import random
import re
import sys
Expand Down Expand Up @@ -165,8 +166,11 @@ def printModelScores(scores, bestScores):
# Make results reproducable.
random.seed(13)

sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')
# sys.stdin.reconfigure(encoding='utf-8')
# sys.stdout.reconfigure(encoding='utf-8')
sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf-8', newline=None)
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8', newline=None, line_buffering=True)


with open(options.features, encoding='utf-8') as featureFh:
featureWeights = readFeatureWeights(featureFh)
Expand Down
7 changes: 5 additions & 2 deletions Generation/fluency/CreateMaxent/filter_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#

import argparse
import io
import sys

VELLDAL_FEATURES = ['lds', 'ldsb', 'lds_dl', 'lds_skew', 'tngram', 'tngramw']
Expand Down Expand Up @@ -38,8 +39,10 @@
tagNgramFeatures = options.tagNgramFeatures
velldalFeatures = options.velldalFeatures

sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')
# sys.stdin.reconfigure(encoding='utf-8')
# sys.stdout.reconfigure(encoding='utf-8')
sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf-8', newline=None)
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8', newline=None, line_buffering=True)

for line in sys.stdin:
line = line.strip()
Expand Down
14 changes: 6 additions & 8 deletions TreebankTools/bin/dtcanonicalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

from xml.dom import minidom
from xml.parsers.expat import ExpatError
import sys, os
import io, os, sys
import tempfile
import html

Expand Down Expand Up @@ -204,13 +204,11 @@ def dump_content(self):
print("comment:", comment)


def write_xml(self, outfile, encoding='UTF-8', version='1.5'):
"""write the dependency structure to xml in encoding"""

outfile.reconfigure(encoding=encoding)
def write_xml(self, outfile, version='1.5'):
"""write the dependency structure to xml"""

# de xml-header
outfile.write('<?xml version="1.0" encoding="%s"?>\n' % encoding)
outfile.write('<?xml version="1.0" encoding="UTF-8"?>\n')

# de root node
if version == "":
Expand Down Expand Up @@ -766,12 +764,12 @@ def write_xml_data(outfile, data):
parser.print_help(sys.stderr)
sys.exit(1)


dtparser = DTParser()

# de bestanden canoniek maken
error_occurred = False
if options.stdin:
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8', newline=None, line_buffering=True)
try:
cn = Canonicalizer(dtparser, sys.stdin)
cn.canonicalize()
Expand All @@ -796,7 +794,7 @@ def write_xml_data(outfile, data):
os.path.dirname(file))

# - we schrijven de nieuwe file naar die tempfile
tempfileobj = os.fdopen(tempfd, "w")
tempfileobj = os.fdopen(tempfd, "w", encoding="utf-8")
cn.write_xml(tempfileobj)
tempfileobj.close()

Expand Down
2 changes: 1 addition & 1 deletion webdemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from urllib import parse

#sys.stdin.reconfigure(encoding='utf-8')
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8', newline=None, line_buffering=True)
form = {}
for line in sys.stdin:
for key, val in parse.parse_qsl(line,
Expand Down

0 comments on commit 6887468

Please sign in to comment.