Инструкции за
програмна връзка с Lat2BG
Lat2BG e сървър
достъпен чрез HTTP. Текста за
превод се подава чрез HTTP
Post или Get на сървъра,
който връща резултата от превода. URL-а
на сървъра е: http://lat2bg.kredor.com/scgi-bin/og.php?op=lat2bg&mode=a Името
на полето съдържащо текста за превод
трябва да е 'Latin' (Latin=TekstZaPrevod) Следната
Get заявка илюстрира принципа за
достъп до Lat2BG: http://lat2bg.kredor.com/scgi-bin/og.php?op=lat2bg&mode=a&Latin=Kas
tekst za prevod Пимерен
код за свързване със Лат2БГ.
Можете да го използвате директно.
Пишете ни ако имате проблеми. Perl PHP Java Perl
скрипт (download)
#!/usr/bin/perl
($inp)=@ARGV;
if ($inp
eq "-help" ) { #print Help
print
"lat2bg 1.0 (25 Feb 2002)";
print
"Usage: cat file_with_text_in_latin |lat2bg >
file_text_in_cyrillic\n"
print
"This program is only for text in bulgarian:\n\n"
print
"lat2bg bgLatin.txt >bgCyrillic.txt\n"
print
"cat bgLatin.txt |lat2bg >bgCyrillic.txt";
print
"Note: This program does conversion from Latin to Cyrillic
spelling only for bulgarian. Output will not appear until all
processing is done, so there might be a delay for large
texts.";
exit;
}
# If no
argument containing text to convert is present read text from stdin
if
(!$inp) {
while
($line=<STDIN>) {
$inp =
"$inp$line";
}
}
$inp =~
s/([^\wа-яА-Я\-\.\@\=])/$1 eq "
"?"+":sprintf("%%%2.2x",ord($1))/eg;
open(lynx_PIPE,
"|/usr/bin/lynx -source -post_data
\"http://lat2bg.kredor.com/scgi-bin/og?op=lat2bg&mode=a\"");
print
lynx_PIPE "Latin=$inp";
close
(lynx_PIPE);
PHP
функция за достъп до Lat2BG
function
lat2bg ($input_text)
{
//URL-encode the text to be sent
$data="Latin=" . urlencode($input_text);
// Generate the request header
$ReqHeader = "POST http://lat2bg.kredor.com/scgi-bin/og.php?op=lat2bg&mode=a
HTTP/1.0\n" .
"Host: lat2bg.kredor.com\n" .
"Content-Type: application/x-www-form-urlencoded\n" .
"Content-Length: " . strlen($data) ."\n" .
"Connection: close\n\n";
// Open the connection to the host
$socket = fsockopen("lat2bg.kredor.com", 80, &$errno, &$errstr);
if (!$socket) {
$Result["errno"] = $errno;
$Result["errstr"] = $errstr;
return $Result;
}
//Submit data
fputs($socket, $ReqHeader);
fputs($socket, $data);
while (!feof($socket))
$result .= fgets($socket, 128);
fclose($socket);
//Strip HTTP header
$result=substr($result, strpos($result, "\r\n\r\n") + 4);
return $result;
}
Java
class за достъп до Lat2BG
// Lat2BgClient.java - Sample Code 7/2002, KREDOR
// Converts text from latin spelling to native spelling using the Lat2Bg server
import java.net.*;
import java.io.*;
public class Lat2BgClient {
public Lat2BgClient(){}
// Converts text from latin spelling to cyrillic
public String translate(String sLatin) {
try {
// URLEncode the text to be send
String sData = "Latin=" + URLEncoder.encode(sLatin);
// Generate HTTP request header
String sReqHeader = "POST
http://lat2bg.kredor.com/scgi-bin/og.php?op=lat2bg&mode=a HTTP/1.0\n" +
"Host: lat2bg.kredor.com\n" +
"Content-Type: application/x-www-form-urlencoded\n" +
"Content-Length: " + sData.length() + "\n" +
"Connection: close\n\n";
// Open connection to the server
Socket s = new Socket("lat2bg.kredor.com", 80);
PrintWriter outp = new
PrintWriter(s.getOutputStream(),true);
BufferedReader inp = new
BufferedReader(new
InputStreamReader(s.getInputStream()));
// Send request
outp.print(sReqHeader);
outp.print(sData);
outp.flush();
// Read response and strip HTTP headers to get just the
// converted text
String sLine;
String sResponse="";
while ( null != (sLine = inp.readLine())){
sResponse = sResponse + sLine + "\r\n";
}
s.close();
sResponse =
sResponse.substring(0,sResponse.length()-2);
sResponse =
sResponse.substring(sResponse.indexOf("\r\n\r\n")+4);
return sResponse;
}
catch (Exception e) {
System.out.println(e.toString());
return null;
}
}
}
|