Commit 1da87927 authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen

Merge branch 'master' of ssh://vo20.nl/git/uva

parents 26858616 7a596c97
...@@ -10,3 +10,5 @@ ...@@ -10,3 +10,5 @@
*.gz *.gz
compilerbouw/ compilerbouw/
robotica/ robotica/
*.out
*.bz2
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
all: highlight compile all: highlight compile
clean: clean:
rm -vf *.out *.aux *.toc translate.tex report.pdf rm -vf *.out *.aux *.toc *.bz2 translate.tex report.pdf
compile: compile:
pdflatex report pdflatex report
...@@ -19,3 +19,6 @@ highlight: translate.tex ...@@ -19,3 +19,6 @@ highlight: translate.tex
translate.tex: translate translate.tex: translate
pygmentize -O style=colorful -l php -o $@ $^ pygmentize -O style=colorful -l php -o $@ $^
tarball:
tar -cvjf translate.tar.bz2 translate report.pdf
\documentclass[10pt,a4paper]{article} \documentclass[10pt,a4paper]{article}
\usepackage{float, url} \usepackage{float, hyperref}
% Load code highlighter color scheme % Load code highlighter color scheme
\input{colors} \input{colors}
...@@ -14,24 +14,24 @@ ...@@ -14,24 +14,24 @@
\section{Assignment} % {{{ \section{Assignment} % {{{
\label{sec:Assignment} \label{sec:Assignment}
The assignment is to create a command-line client that uses the Google Translate The assignment is to create a command-line interface that uses the Google
Webservice API to translate data entered by the user. The source and target Translate Webservice API to translate data entered by the user. The source and
language must be entered in the command line, and there must be an option target language must be entered in the command line, and there should be an option
'--help' available. \texttt{--help} available.
% }}} % }}}
\section{Protocol} % {{{ \section{Protocol} % {{{
\label{sec:Protocol} \label{sec:Protocol}
The Google Translate API cal be called with a HTTP GET request. To be able to The Google Translate API can be called with an HTTP GET request. To be able to
use the API, we need an ``API key'', which we have received from Google. The use the API, we need an ``API key'', which we have obtained from the Google API
request parameters should at least contain the API key, the target language and Console website. The request parameters should at least contain the API key, the
one or more texts that are to be translated. If no source language is specified target language and one or more texts that are to be translated. If no source
in the parameters, the server will try to detect and return the source language language is specified in the parameters, the server will try to detect and
from the entered text values. Since our program requires the source language to return the source language from the entered text values. Since our program
be specified, we will not use this functionality. The request URL thus requires the source language to be specified, we will not use this
always has the following format: functionality. The request URL thus always has the following format:
\begin{verbatim} \begin{verbatim}
https://www.googleapis.com/language/translate/v2?key={KEY} https://www.googleapis.com/language/translate/v2?key={KEY}
...@@ -75,9 +75,9 @@ program should be called. ...@@ -75,9 +75,9 @@ program should be called.
The program reads input from the user from \texttt{stdin}. When a newline The program reads input from the user from \texttt{stdin}. When a newline
character is entered, the function \texttt{translate} is called with the entered character is entered, the function \texttt{translate} is called with the entered
text. This function uses the built in PHP function \texttt{file\_get\_contents} text. This function uses the built in PHP function \texttt{file\_get\_contents}
(\url{www.php.net/file\_get\_contents}) to call the Google Translate API using (\url{http://nl3.php.net/file_get_contents}) to call the Google Translate API using
the URL mentioned in section \ref{sec:Protocol}. This function returns the URL mentioned in section \ref{sec:Protocol}. This function returns
\texttt{FALSE} on failure, which is handled by the program. To suppress any \texttt{false} on failure, which is handled by the program. To suppress any
warnings that are generated by a ``bad request''-error, we use \texttt{@} in warnings that are generated by a ``bad request''-error, we use \texttt{@} in
front of the function call. We can safely do this because we handle any front of the function call. We can safely do this because we handle any
failures manually. The two most probable reasons for a ``bad request'' to occur, failures manually. The two most probable reasons for a ``bad request'' to occur,
...@@ -86,7 +86,7 @@ source/target language. Since both are critical for the program's ability to ...@@ -86,7 +86,7 @@ source/target language. Since both are critical for the program's ability to
function correctly, we have chosen to end the program after a bad request. If function correctly, we have chosen to end the program after a bad request. If
the request is succesful, we use the built in PHP function \texttt{json\_decode} the request is succesful, we use the built in PHP function \texttt{json\_decode}
to parse the response. The converts the JSON object into an associative array, to parse the response. The converts the JSON object into an associative array,
form which we can easily extract the translation: from which we can easily extract the translation:
\begin{verbatim} \begin{verbatim}
return $response["data"]["translations"][0]["translatedText"]; return $response["data"]["translations"][0]["translatedText"];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment