Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
1da87927
Commit
1da87927
authored
Mar 11, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
ssh://vo20.nl/git/uva
parents
26858616
7a596c97
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
17 deletions
+22
-17
.gitignore
.gitignore
+2
-0
telematica/ass2/Makefile
telematica/ass2/Makefile
+4
-1
telematica/ass2/report.tex
telematica/ass2/report.tex
+16
-16
No files found.
.gitignore
View file @
1da87927
...
...
@@ -10,3 +10,5 @@
*.gz
compilerbouw/
robotica/
*.out
*.bz2
telematica/ass2/Makefile
View file @
1da87927
...
...
@@ -7,7 +7,7 @@
all
:
highlight compile
clean
:
rm
-vf
*
.out
*
.aux
*
.toc translate.tex report.pdf
rm
-vf
*
.out
*
.aux
*
.toc
*
.bz2
translate.tex report.pdf
compile
:
pdflatex report
...
...
@@ -19,3 +19,6 @@ highlight: translate.tex
translate.tex
:
translate
pygmentize
-O
style
=
colorful
-l
php
-o
$@
$^
tarball
:
tar
-cvjf
translate.tar.bz2 translate report.pdf
telematica/ass2/report.tex
View file @
1da87927
\documentclass
[10pt,a4paper]
{
article
}
\usepackage
{
float,
url
}
\usepackage
{
float,
hyperref
}
% Load code highlighter color scheme
\input
{
colors
}
...
...
@@ -14,24 +14,24 @@
\section
{
Assignment
}
% {{{
\label
{
sec:Assignment
}
The assignment is to create a command-line
client that uses the Google Translat
e
Webservice API to translate data entered by the user. The source and target
language must be entered in the command line, and there must
be an option
'--help'
available.
The assignment is to create a command-line
interface that uses the Googl
e
Translate Webservice API to translate data entered by the user. The source and
target language must be entered in the command line, and there should
be an option
\texttt
{
--help
}
available.
% }}}
\section
{
Protocol
}
% {{{
\label
{
sec:Protocol
}
The Google Translate API ca
l be called with a
HTTP GET request. To be able to
use the API, we need an ``API key'', which we have
received from Google. The
request parameters should at least contain the API key, the target language and
one or more texts that are to be translated. If no source language is specified
in the parameters, the server will try to detect and return the source language
from the entered text values. Since our program requires the source language to
be specified, we will not use this functionality. The request URL thu
s
always has the following format:
The Google Translate API ca
n be called with an
HTTP GET request. To be able to
use the API, we need an ``API key'', which we have
obtained from the Google API
Console website. The request parameters should at least contain the API key, the
target language and one or more texts that are to be translated. If no source
language is specified in the parameters, the server will try to detect and
return the source language from the entered text values. Since our program
requires the source language to be specified, we will not use thi
s
functionality. The request URL thus
always has the following format:
\begin{verbatim}
https://www.googleapis.com/language/translate/v2?key=
{
KEY
}
...
...
@@ -75,9 +75,9 @@ program should be called.
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
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
\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
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,
...
...
@@ -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
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,
f
or
m which we can easily extract the translation:
f
ro
m which we can easily extract the translation:
\begin{verbatim}
return
$
response
[
"data"
][
"translations"
][
0
][
"translatedText"
]
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment