In [ ]:
import goslate # pip install goslate
from bs4 import BeautifulSoup # pip install beautifulsoup4
import urllib2 # pip install requests
In [ ]:
inventary_dict = {'milk': 23, 'coockies': 12, 'chocolate': 26, 'yogourt': 5}
print "This is the original dictionary:"
print inventary_dict
print " "
print "This is the value associated to 'milk':"
print inventary_dict['milk']
print " "
print "We add a new element to the dictionary:"
inventary_dict.update({'sugar': 103})
print inventary_dict
print " "
print "We increment the value of one of the elements:"
inventary_dict['coockies'] += 10
print inventary_dict
print " "
In [ ]:
keys = inventary_dict.keys()
print "These are the keys of the dictionary:"
print keys
print " "
values = inventary_dict.values()
print "These are the values of the dictionary:"
print values
print " "
print "The size of this dictionary is %d, and it stores the following elements:" % len(inventary_dict.keys())
for key in keys:
print key + ": " + str(inventary_dict[key])
In [ ]:
languages_dict = <COMPLETAR>
print "Vamos a traducir de %s a %s." % (languages_dict['es'], languages_dict['it'])
In [ ]:
def view_codes(mydict):
<COMPLETAR>
view_codes(languages_dict)
In [ ]:
agent = {'User-Agent':"Mozilla/4.0"}
url1 = "https://www.u-tad.com/conocenos/conoce-u-tad/"
request = urllib2.Request(url1, headers=agent)
page = urllib2.urlopen(request).read()
n_caracteres_descargados = <COMPLETAR>
print "La página descargada tiene %d caracteres." % n_caracteres_descargados
print "Estos son los primeros 1000 caracteres:"
print "=" * 100
print <COMPLETAR>
print "=" * 100
In [ ]:
bs = BeautifulSoup(page, "html5lib")
for script in bs(["script", "style"]):
script.extract()
text_utad = bs.get_text()
text_utad = ' '.join(text_utad.split())
print text_utad
In [ ]:
url = "https://translate.google.com/m?hl=de&sl=auto&q=adiós+amigos"
print url
In [ ]:
destiny_language = 'it'
my_text = "Hola a todos mis amigos"
def url_translate(destiny_language, text):
url = <COMPLETAR> % (destiny_language, "auto", text.replace(<COMPLETAR>))
return url
url = url_translate(destiny_language, my_text)
print url
In [ ]:
def get_html(lang, text):
agent = {'User-Agent':"Mozilla/4.0"}
url = <COMPLETAR>
request = urllib2.Request(url, headers=agent)
html = urllib2.urlopen(request).read()
return html
html = get_html(destiny_language, my_text)
n_caracteres_descargados = <COMPLETAR>
print "La página descargada tiene %d caracteres." % n_caracteres_descargados
print "=" * 100
print html
print "=" * 100
In [ ]:
def translate(lang, text):
html = <COMPLETAR>
bs = BeautifulSoup(html, "html5lib")
translation =bs.findAll('div')[2].text
return translation
key = 'en'
print u"Traducción al " + unicode(languages_dict[key],'utf-8') + ":"
print translate(key, my_text)
In [ ]:
for <COMPLETAR>:
print u"Traducción al " + unicode(languages_dict[key],'utf-8') + ":"
print <COMPLETAR>
print " "
In [ ]:
languages_dict.update(<COMPLETAR>)
languages_dict.<COMPLETAR>
languages_dict.<COMPLETAR>
languages_dict.<COMPLETAR>
languages_dict.<COMPLETAR>
languages_dict.<COMPLETAR>
languages_dict.<COMPLETAR>
view_codes(languages_dict)
In [ ]:
<COMPLETAR>
gu: Gujarati
zh-TW: Chinese (Traditional)
gd: Scots Gaelic
ga: Irish
gl: Galician
lb: Luxembourgish
la: Latin
lo: Lao
(...)
In [ ]:
gs = goslate.Goslate()
all_languages_dict = gs.get_languages()
view_codes(<COMPLETAR>)
Traducción al Gujarati:
અમે પહેલાથી જ પ્રારંભિક અભ્યાસક્રમ પૂર્ણ કર્યા
Traducción al Chinese (Traditional):
我們已經完成了入門課程
Traducción al Scots Gaelic:
Tha sinn air crěoch a chur air mar-thŕ a 'chiad chůrsa
Traducción al Irish:
Táimid tar éis i gcrích cheana féin ar an gcúrsa tosaigh
(...)
In [ ]:
my_text = 'Ya hemos completado el curso introductorio'
for key in <COMPLETAR>:
print u"\nTraducción al " + <COMPLETAR> + ":"
print <COMPLETAR>
In [ ]: