title: Python to sort individual words into alphabetical + corresponding translations in columns
body: <p>So, this is the script that has very kindly been given to me as a starter:</p>
  <pre><code>#!/usr/bin/python
  # -*- coding: utf-8 -*-
  from __future__ import with_statement    # needed for Python 2.5
  from itertools import chain
  
  def chunk(s):
      &quot;&quot;&quot;Split a string on whitespace or hyphens&quot;&quot;&quot;
      return chain(*(c.split(&quot;-&quot;) for c in s.split()))
  
  def process(latin, gloss, trans):
      chunks = zip(chunk(latin), chunk(gloss))
      # now you have to DO SOMETHING with the chunks!
  
  def main():
      with open(&quot;examples.txt&quot;) as inf:
          try:
              while True:
                  latin = inf.next().strip()
                  gloss = inf.next().strip()
                  trans = inf.next().strip()
                  process(latin, gloss, trans)
                  inf.next()    # skip blank line
          except StopIteration:
              # reached end of file
              pass
  
  if __name__==&quot;__main__&quot;:
      main()
  </code></pre>
  <p>However,</p>
  <p>I've just spoken to my lecturer, who has let me know that he doesn't want us using the</p>
  <pre><code>__ x __ 
  </code></pre>
  <p>function, as it is &quot;too advanced for the students' needs at this point in the course&quot;.
  I'm absolutely stumped as to what I need to put into the &quot;chunks&quot; or &quot;process&quot; fields, up until now I've been able to figure most of the other exercises out (with a few hints) but this one is just way beyond me. This particular part is worth 15 points out of 20, and it's making me feel just a little bit sick!
  Any further help would be greatly appreciated.</p>
  <hr />
  <p><strong>Original post</strong> (sorry it's so long!)</p>
  <p>I'm trying to do the following: I have a text with a language other than english, broken up into morphemes (parts of each word) using hyphens, with the English gloss (linguistic translation of each morpheme) and a direct translation below. eg.</p>
  <blockquote>
  <p>Itali-am fat-o profug-us Lavini-a-que ven-it</p>
  <p>Italy-Fem:Sg:Acc fate-Neut:Sg:Abl fleeing-Masc:Sg:Nom Lavinian-Neut:Pl:Acc come:Perf-3-Sg:Indic:Act</p>
  <p>'in flight [driven] by fate came to Italy and the Lavinian [shores]'</p>
  </blockquote>
  <p>I'll have several texts such as the above in one file - i.e.</p>
  <pre><code>blank line
  
  a line of latin broken up with hyphens
  
  a line of gloss broken up with corresponding hyphens, using colons to join elements
  
  a line of translation
  
  blank line
  
  latin
  
  gloss
  
  translation
  
  ad infinitum.
  </code></pre>
  <p>What I need to do is write a file that gives me the following output:</p>
  <pre><code>Itali:    1    Italy
  am:    1    Fem:Sg:Acc
  fat:    1    fate
  o:    1    Neut:Sg:Abl
  profug:   1    fleeing
  us:    1    Masc:Sg:Nom
  Lavini:    1    Lavinian
  a:    1    Neug:Pl:Acc
  que:    1    come:Perf
  ven:    1   3
  it:     1   Sg:Indic:Act
  </code></pre>
  <p>where the first column represents the first line of text without hyphens; the second column indicates the number of occurrences (it's only 1 each in this example), and the third column is the English translation of the first column, as written in the text.</p>
  <p>If there's a latin morpheme with no corresponding English gloss/translation, the Latin column will be as normal but the English column will print [unknown], like:</p>
  <pre><code>a:  1   [unknown]
  </code></pre>
  <p>And if the opposite, i.e. an English morpheme with no corresponding Latin, it should print</p>
  <pre><code>[unknown]:  1   kitten
  </code></pre>
  <p>Finally, the prog needs to be able to deal with homophonous morphemes (i.e. two identically spelled latin morphemes with different meanings). e.g.</p>
  <pre><code>a:  16  Neuter:Plural
  a:  28  Feminine:Singular
  </code></pre>