Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7888327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:48:15+00:00 2026-06-03T05:48:15+00:00

This is the script that has very kindly been given to me as a

  • 0

This is the script that has very kindly been given to me as a starter:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import with_statement    # needed for Python 2.5
from itertools import chain

def chunk(s):
    """Split a string on whitespace or hyphens"""
    return chain(*(c.split("-") 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("examples.txt") 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__=="__main__":
    main()

I’m not sure if I’m missing anything, but the output is simply blank, taking me back to $.

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.

Itali-am fat-o profug-us Lavini-a-que ven-it

Italy-Fem:Sg:Acc fate-Neut:Sg:Abl fleeing-Masc:Sg:Nom Lavinian-Neut:Pl:Acc come:Perf-3-Sg:Indic:Act

‘in flight [driven] by fate came to Italy and the Lavinian [shores]’

I’ll have several texts such as the above in one file – i.e.

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.

What I need to do is write a file that gives me the following output:

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

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.

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:

a:  1   [unknown]

And if the opposite, i.e. an English morpheme with no corresponding Latin, it should print

[unknown]:  1   kitten

Finally, the prog needs to be able to deal with homophonous morphemes (i.e. two identically spelled latin morphemes with different meanings).
e.g.

a:  16  Neuter:Plural
a:  28  Feminine:Singular

Again, it’s homework, and any pointers would be wonderful. Working on putting together some script now to upload here for critique!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:48:16+00:00Added an answer on June 3, 2026 at 5:48 am

    Processing the file is a bit tricky because of the multiline structure; rather than the usual line-by-line iteration, I suggest something like this (I presume the file does not actually begin with a blank line, per your example, but that it uses blank lines as separators):

    with open("input.txt") 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
    

    process must then split latin and gloss into chunks and pair them appropriately:

    from itertools import chain
    
    def chunk(s):
        """Split a string on whitespace or hyphens"""
        return chain(*(c.split("-") for c in s.split()))
    
    def process(latin, gloss, trans):
        chunks = zip(chunk(latin), chunk(gloss))
    

    Calling this like

    process(
        "Itali-am fat-o profug-us Lavini-a-que ven-it",
        "Italy-Fem:Sg:Acc fate-Neut:Sg:Abl fleeing-Masc:Sg:Nom Lavinian-Neut:Pl:Acc come:Perf-3-Sg:Indic:Act",
        "in flight [driven] by fate came to Italy and the Lavinian [shores]")
    

    leaves chunks containing

    [('Itali', 'Italy'),
     ('am', 'Fem:Sg:Acc'),
     ('fat', 'fate'),
     ('o', 'Neut:Sg:Abl'),
     ('profug', 'fleeing'),
     ('us', 'Masc:Sg:Nom'),
     ('Lavini', 'Lavinian'),
     ('a', 'Neut:Pl:Acc'),
     ('que', 'come:Perf'),
     ('ven', '3'),
     ('it', 'Sg:Indic:Act')]
    

    The rest is an exercise for the student – keeping a running count of the chunks, then sorting and displaying it appropriately. Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, this is the script that has very kindly been given to me as
I have a bash script that has this function in it: function start_vi() {
I have this script that collects data from users and I want to check
I have a Python script that is running a few ls commands. This script
I'm trying to write a python script that packages our software. This script needs
I've been working on a very simple Jquery script that displays a few mini-dashboards
I do not think that this has been posted before - as this is
Well basically I have this script that takes a long time to execute and
I have this script that works, but I need to change the format of
I have a JS script that changes content of webpages. I use this script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.