Class 22 — Friday October 15

It's a fact, look it up

Really need to know — What lies farther down the road — Need a future map


Look both ways


Agenda


Examples


To do


Huh?


Babelfish — a translation dictionary in CSV format

a, a

aab, water

aamadam, came

...

ziba, beautiful

zur, to

zu, to

zwei, two

...

 


Problem text translation using babelfish dictionary

Enter phrase to be translated: dónde está mi lápiz

where is my pencil

Enter phrase to be translated: est la baguette fraîche aujourd'hui

is the baguatte fresh today

Enter phrase to be translated: je %#@#&# ból svayam !

I _%#@#&#_ hurt myself !

Enter phrase to be translated: hvor er den skriget maleri af Munch

where is the scream painting by Münch

Enter phrase to be translated: hacer 뭔가 frais oggi

do something cool today

  • List of words making up the user text.
  • Dictionary loaded with the entries from the babelfish web file.
  • Translation accumulation.
  • For example, French word heureuse for happiness is not in the babelfish dictionary, so it would be translated as _heureuse_.
  • The translation needs to be stored as a string.
  • We need a string accumulator.
  • When accumulating the translation, need a space following each word to separate it from the next word.
  • If a word is known to the babelish dictionary, its translation is added to the accumulation.
  • If instead a word is unknown, the word surrounded by underscores is added to the accumulation.

Spell correction — program spell_rite.py

  • If a correction is known, the correction is offered within asterisks.
  • If instead no correction is known, the word is flagged with underscores.

Enter text: I am as happy as I can be

I am as happy as I can be

Enter text: I am a Newyorker who is livin in virginia

I am a *New Yorker* who is _livin_ in *Virginia*

Enter text:

 


Program wc.py — making a point about the importance of functions

Enter web file link: http://www.cs1112.org

nl = 241

nw = 1261

nc = 14104

Enter web file link: http://www.nytimes.com

nl = 583

nw = 12339

nc = 1166144

# import module for internet access

import url

# get the lines of text from a user-specified file

reply = input( 'Enter web file link: ' )

link = reply.strip()

contents = url.get_contents( link )

# count the number of lines, words, and characters in the contents

nbr_lines = contents.count( '\n' )

words = contents.split()

nbr_words = len( words )

nbc_chars = len( contents )

# display result

print( 'nl =', nbr_lines )

print( 'nw =', nbr_words )

print( 'nc =', nbc_chars )



Here we go

  • We will get to that likely in another three classes or so.

Babelfish — Hitchhiker's Guide to the Galaxy

Everaldo Coelho (YellowIcon); / LGPL (http://www.gnu.org/licenses/lgpl.html)

Ziggurat of Ur

Hardnfast / CC BY (https://creativecommons.org/licenses/by/3.0)