Assignment 8
Due: 11:00 AM on Friday, November 25, 2011
Question 1
Given that Imperial and Metric units are still used in different
countries, it is often necessary to convert from a measure in one unit
to a measure in another, e.g.,
- Given x feet, to convert to metres, multiply x by 0.305;
- Given x metres, to convert to feet, multiply x by 3.28;
- Given x pounds, to convert to kilos, multiply x by 0.454; and
- Given x kilos, to convert to pounds, multiply x by 2.202.
Write and document a Python script convert2.py which takes as
command-line arguments a given-unit type in {Metric, Imperial}
and an input textfile, and outputs the version of the input textfile in
which all units that are the opposite unit-type of that specified are
translated into the units of the specified type as described above.
Note that unit-quantities may be specified
in the given text using either integers or floating-point numbers.
The conversion mechanism must be implemented using functions
from the re module.
Your script must work on datafile
convert2.txt
to produce the output given in typescript-file
convert2.script.
Question 2
A reversible translator between two dialects of the same language can
be specified by i) a set of mapping rules of the form "X" <=> "Y"
indicating that text X in dialect #1 maps onto text Y in
dialect #2 and vice versa, and ii) a mechanism that applies (i) to a piece
of text from dialect #1 (#2) to create the corresponding piece of text in
dialect #2 (#1). The rules in (i) are stored in a dialect translation file
which may contain embedded (and ignored) comments. The mechanism in (ii)
applies the rules in (i) in the order in which they are specified to a
sentence of the given dialect. As text in one dialect may be
longer or shorter than the corresponding text in the other
dialect and we want to maintain the approximate line-length of the original
text, the mechanism in (ii) should also make sure that it adjusts the
translated text such that no line is longer than the longest line in the
given text.
Write and document a Python script rtrans.py which, given a
dialect translation file, a translation-direction string "to"
or "from", and a dialect textfile as command-line
arguments, prints a line-length adjusted version of the given text as
translated relative to the given translation file in either the forward
or backward direction, respectively.
The translation mechanism must be implemented using functions
from the re module.
Your script must work on dialect-translation files
e2g1.txt and
e2g2.txt and textfile
rtrans.txt
to produce the output given in typescript-file
rtrans.script.
You may assume that all dialect translation files are valid.
Hints
In Question 1, you may find it useful to process recognized quantity-unit
pairs using lambda functions for the replacement patterns in the
sub() function.
In Question 2, you may find it useful to recognize and parse the
rules in a dialect translation file using the regular-expression matching
functions in re before applying this rule-set (again using
re functions) to the provided text.
Submission
Please hand in printed copies of all of your Python script files.
You must also submit these files electronically using the
submit-assignment command.
Note that each script file must have the following comment
block at the top, where the X's are replaced with the appropriate
information, followed by a docstring briefly describing the program in that
script. For instance, my script for this assignment would
begin with the following comment block:
#########################################################
## CS 2500 (Fall 2011), Assignment #8, Question #1 ##
## Script File Name: convert2.py ##
## Student Name: Todd Wareham ##
## Login Name: harold ##
## MUN #: 8008765 ##
#########################################################
You do not have to develop your code on our CS departmental systems.
However, as your code will be compiled and tested on our CS departmental
systems as part of the assignment marking process,
you should ensure that your code compiles and runs correctly on at
least one of these systems.
- Nov 21, 2:15pm
Fixed error in both answer code and supplied script file
"rtrans.script" (wrt the length of lines in outputs) for
Question #2 in Assignment #8.
- Nov 9, 8:55am
Assignment #8 posted.
Created: November 9, 2011
Last Modified: November 21, 2011