Chapter 2
Encoding/File/Strings snippets from listings 2.2 to 2.4
Click here to open in new a page/tab.
Listing 2.5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Small game program asking a user to find a random number (Listing 2.5) | |
#Tell program to import the "random" module | |
import random | |
#Generate a random number between 0 and 5 | |
secret_number = random.randint(0,5) | |
#Question to the user | |
question = "Guess the number between 0 and 5 and press Enter." | |
while int(raw_input(question).strip()) != secret_number: | |
pass | |
#Tell user that they have won the game | |
print "You've found it! Congratulations" |
Listing 2.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Revision of small game program asking a user to find a random number (Listing 2.7) | |
#Tell program to import the "random" module | |
import random | |
#Generate a random number between 0 and a number selected by the user | |
number_selection = "Select a maximum number:" | |
max_number = int(raw_input(number_selection).rstrip()) | |
secret_number = random.randint(0,max_number) | |
#Question to the user | |
question = "Guess the number between 0 and %d and press Enter." % max_number | |
while int(raw_input(question).strip()) != secret_number: | |
pass | |
#Tell user that they have won the game | |
print "You've found it! Congratulations" |
Listings 2.9-2.11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Listing 2.9 | |
g = "is" | |
f = "Substitution" | |
my_string = "%s %s fun" % (f, g) | |
# Listing 2.10 | |
g = "is" | |
f = "Substitution" | |
my_string = "%(topic)s %(copula)s fun" % {"topic": f, "copula": g} | |
# Listing 2.11 | |
g = "is" | |
f = "Substitution" | |
my_string = "{0} {1} fun".format(f,g) | |
my_string2 = "{topic} {copula} fun".format(topic=f, copula=g) |
Task 2.7.7: Tutorial on regular expressions
Click here to access.
Input file for Task 2.7.8
Click here to open in new page.
Chapter 3
Listing 3.3 (extended)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from datetime import datetime | |
from django.utils.translation import ugettext as _ | |
from django.utils.translation import get_language_info | |
from django.http import HttpResponse | |
from django.template import Context, loader | |
from app1 import settings | |
def home(request, collection='headlines', direction=-1, key_sort='published', limit=30): | |
#Get list of languages for language list | |
languages = [get_language_info(lang[0]).get('name_local') for lang in settings.LANGUAGE] | |
langs = dict(zip([lang[0] for lang in settings.LANGUAGE], languages)) | |
latest_headlines_list = None | |
t = loader.get_template('headlines/index.html') | |
if settings.MONGO_DB: | |
my_collection = settings.MONGO_DB[collection] | |
if my_collection.count() > 0: | |
latest_headlines_list = my_collection.find().sort(key_sort, | |
direction=direction).limit(limit) | |
#Make sure that the publication date is returned as datetime object (for localisation) | |
temp_list = [] | |
for h in latest_headlines_list: | |
h['published'] = datetime.strptime(h['published'], "%Y-%m-%dT%H:%M:%SZ") | |
temp_list.append(h) | |
latest_headlines_list = temp_list | |
# Translators: This title is followed by a list of NBA new stories | |
subheading = _("Your latest NBA headlines.") | |
c = Context({'subheading': subheading, 'latest_headlines_list': latest_headlines_list, | |
'collection': collection, 'db_server_name': settings.MONGO_HOST, | |
'languages': languages, 'langs': langs, 'url': request.get_host() | |
}) | |
return HttpResponse(t.render(c)) |
Listing 3.4 (extended)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load i18n %} | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>NBA4ALL</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.css" /> | |
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> | |
<script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script> | |
<style> | |
.ui-icon-myicon:after { | |
background-image: url("http://www.languageicon.org/icon.png"); | |
/* Make your icon fit */ | |
background-size: 18px 18px; | |
} | |
</style> | |
</head> | |
<body> | |
<div data-role="page"> | |
<div data-role="header"> | |
<h1 align="center" style="font-size:20px"><a href="http://app1.multilingualcontent.org" style="text-decoration: none">NBA4ALL</a></h1> | |
</div><!-- /header --> | |
<div role="main" class="ui-content" data-theme="a"> | |
<a href="#popupMenu" data-rel="popup" data-transition="slideup" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-myicon ui-btn-icon-left ui-btn-a">...</a> | |
<div data-role="popup" id="popupMenu" data-theme="a"> | |
<ul data-role="listview" data-inset="true" style="min-width:210px;"> | |
<li data-role="list-divider">{% trans 'Choose Language' %}</li> | |
{% for code, language in langs.items %} | |
<li><a href="http://{{ url }}/{{ code }}/">{{ language|title }}</a></li> | |
{% endfor %} | |
</ul> | |
</div> | |
<h2 align="center">{% trans "Your latest NBA headlines" %}</h2> | |
{% comment %}Translators: The next sentence should be quite catchy.{% endcomment %} | |
<p align="center">{% blocktrans %}See what your team has been up to! {% endblocktrans %}</p> | |
{% if latest_headlines_list %} | |
<!-- use data-filter-placeholder='?' on listview to change default text: http://jquerymobile.com/test/docs/lists/docs-lists.html --> | |
<ul data-role="listview" use data-filter-placeholder='?' data-filter="true" data-inset="true" class="ui-listview ui-listview-inset ui-corner-all ui-shadow" data-theme="a"> | |
{% for headline in latest_headlines_list %} | |
<li class="ui-li-static ui-body-inherit"> | |
<!-- format date and rely on Django's localisation https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date --> | |
<a target="_blank" href="{{ headline.links.web.href }} " data-rel="dialog" data-transition="slideup" title="{% trans 'Read more online.' %}"> | |
{{ headline.headline }}<br /> ({{ headline.published|date:"l d M Y" }}) | |
</a> | |
{% if headline.description %} | |
<p class="ui-li-desc" style="white-space:normal;"> | |
{{ headline.description }} | |
<!-- Show player names if available --> | |
{% if headline.categories %} | |
{% for category in headline.categories %} | |
{% if category.athlete %}-- | |
<a href="{{ category.athlete.links.web.athletes.href }}" | |
target="_blank">{{ category.athlete.description }}</a> | |
{% endif %} | |
{% endfor %} | |
{% endif %} | |
</p> | |
{% endif %} | |
</li> | |
{% endfor %} | |
</ul> | |
{% else %} | |
<p>{% trans 'No headlines available. Check later.'</p> | |
{% endif %} | |
</div><!-- /content --> | |
<div data-role="footer"> | |
<h4><a href="https://www.yahoo.com/?ilc=401" target="_blank"> <img src="https://poweredby.yahoo.com/purple.png" width="134" height="29"/> </a></h4> | |
</div><!-- /footer --> | |
</div><!-- /page --> | |
</body> | |
</html> |
Task 3.4.2 (code input and expected PO output)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Small game program asking a user to find a random number | |
#Tell program to import the "random" module | |
import random | |
#Import i18n mechanism | |
import gettext | |
_ = gettext.gettext | |
def generate_random_number(ask_for_input="yes"): | |
"""DOC: Generate a random number between 0 and a positive number chosen by the user""" | |
got_max_number = False | |
number_selection = "Select a maximum positive number:" | |
while not got_max_number: | |
user_input = raw_input(number_selection).rstrip() | |
if not user_input.isdigit() or user_input == '0': | |
print "Wrong input. %s" % number_selection | |
continue | |
max_number = int(user_input) | |
got_max_number = True | |
secret_number = random.randint(0,max_number) | |
return max_number, secret_number | |
def play_game(show_string="no", max_number=None, secret_number=None): | |
"""DOC: Main function to check whether user finds the random number.""" | |
key = "Enter" | |
attempts = 0 | |
#Question to the user | |
question = "Guess the number between 0 and %s and press %s." % (str(max_number), key) | |
got_secret_number = False | |
while not got_secret_number: | |
user_input = raw_input(question).strip() | |
attempts += 1 | |
if not user_input.isdigit() or int(user_input) != secret_number: | |
continue | |
break | |
#Tell user that they have won the game | |
print "You've found '%d' in %d attempts! Congratulations!" % (secret_number, attempts) | |
max_number, secret_number = generate_random_number() | |
play_game(max_number=max_number, secret_number=secret_number) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SOME DESCRIPTIVE TITLE. | |
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER | |
# This file is distributed under the same license as the PACKAGE package. | |
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. | |
# | |
#, fuzzy | |
msgid "" | |
msgstr "" | |
"Project-Id-Version: PACKAGE VERSION\n" | |
"Report-Msgid-Bugs-To: \n" | |
"POT-Creation-Date: 2014-07-28 16:27+0000\n" | |
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | |
"Language-Team: LANGUAGE <LL@li.org>\n" | |
"Language: \n" | |
"MIME-Version: 1.0\n" | |
"Content-Type: text/plain; charset=CHARSET\n" | |
"Content-Transfer-Encoding: 8bit\n" | |
#. TRANSLATORS: This message invites the user to pick a positive number. | |
#: secret3.py:16 | |
msgid "Select a maximum positive number:" | |
msgstr "" | |
#. TRANSLATORS: %s is _("Select a maximum positive number:") | |
#: secret3.py:21 | |
#, python-format | |
msgid "Wrong input. %s" | |
msgstr "" | |
#. TRANSLATORS: This refers to the "Enter" key from the keyboard | |
#: secret3.py:35 | |
msgid "Enter" | |
msgstr "" | |
#. TRANSLATORS: This question asks the user to pick a number and press a key | |
#: secret3.py:39 | |
#, python-format | |
msgid "Guess the number between 0 and %(max_number)s and press %(key)s." | |
msgstr "" | |
#. TRANSLATORS: This string tells the user that they have found the number after a certain number of attempts | |
#: secret3.py:50 | |
#, python-format | |
msgid "" | |
"You've found '%(secret_number)d' in %(attempts)d attempts! Congratulations!" | |
msgstr "" |
Task 3.4.3 (XML input and expected text output)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://docbook.org/xml/4.2/docbookx.dtd"> | |
<article> | |
<title>NBA4ALL Documentation</title> | |
<sect1> | |
<title>Filtering a list of headlines</title> | |
<para> | |
10 headlines are shown by the basketball news application by default so in order to filter this basketball news list, a word can be entered in the Search box and the list will change as soon as you start typing in the text box. | |
</para> | |
<note><title>Limitations</title> | |
<para>Currently it is not possible to search for multiple words.</para> | |
</note> | |
<tip><title>Tip</title> | |
<para>Both titles and descriptions are searched (eg searching for generic words may return more results than originally thought).</para> | |
</tip> | |
</sect1> | |
</article> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NBA4ALL Documentation | |
Filtering a list of headlines | |
10 headlines are shown by the basketball news application by default so in order to filter this basketball news list, a word can be entered in the Search box and the list will change as soon as you start typing in the text box. | |
Limitations | |
Currently it is not possible to search for multiple words. | |
Tip | |
Both titles and descriptions are searched (eg searching for generic words may return more results than originally thought). |
Task 3.4.5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NB4ALL Documentation | |
Filtering teh list of headlines | |
10 headlines are shown by the basketball news application by by default so in order to filter this basketball news list, a word can be entered in the Search box and the list will change as soon as you start typing in the text box. | |
Limitations | |
Its not possible to search for the multiple words. | |
Tip | |
Both titles and descriptions are searched (eg searching for generic words may return more results than originally thought). |
Chapter 4
Listing 4.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Tkinter | |
import sys | |
from gettext import gettext as _ | |
class App(Tkinter.Tk): | |
def __init__(self): | |
Tkinter.Tk.__init__(self) | |
menu_bar = Tkinter.Menu(self) | |
file_menu = Tkinter.Menu(menu_bar, tearoff=False) | |
file_menu2 = Tkinter.Menu(menu_bar, tearoff=False) | |
#Translators: hotkey is on first letter | |
menu_bar.add_cascade(label=_("Actions"), underline=0, menu=file_menu) | |
#Translators: hotkey is on second letter | |
menu_bar.add_cascade(label=_("About"), underline=1, menu=file_menu2) | |
file_menu.add_command(label="Quit", command=quit, accelerator="Ctrl+Q") | |
file_menu2.add_command(label="Exit", command=quit, accelerator="Ctrl+E") | |
self.config(menu=menu_bar) | |
self.bind_all("<Control-q>", self.quit) | |
self.bind_all("<Control-e>", self.quit) | |
def quit(self, event): | |
print "See you soon!" | |
sys.exit(0) | |
if __name__ == "__main__": | |
app=App() | |
app.title("Hotkeys") | |
app.mainloop() |
Task 4.6.2
Click here to access a list of public Pootle servers (opens in new page/tab).
Chapter 5
Task 5.9.2
Click here to access the HTML document (opens in new page/tab).
Chapter 6
Task 6.6.2
Click here or here to access NLP-based tools that require some language resource adaptation.