如何propertly使用Bing翻译API? [英] How to propertly use Bing Translate API?
问题描述
我使用Bing的API,更precisely - 翻译部分,而这一切工作的很好,除了一件事 - 语言的自动检测。这怎么可能?
我的code工作正常,柜面有人需要看一下:
功能HTTPEn code(常量ASTR:字符串):字符串;
常量
NoConversion = ['A'..'Z','A'..'Z','*','@','_',' - ''。'];
VAR
我:整数;
开始
结果:=''; 对于i:= 1到长度(ASTR)做
开始
如果CharInSet(ASTR [I],NoConversion)然后
结果:=结果+黄芪甲甙[I]
其他
结果:=结果+格式('%%% 2倍',[ORD(ASTR [I])]);
结束;
结束;功能GetTranslation(文字,fromLang,toLang:字符串):字符串;
VAR
xmlDoc中:TXMLDocument;
的inode,了mnode,rnode,irnode:IXMLNode;
记者:整数;
URI:串;
idhttp:TIdHttp;开始
结果:=''; idhttp:= TIdHttp.Create(无);
xmlDoc中:= TXMLDocument.Create(应用);
尝试
xmldoc.LoadFromXML(idhttp.Get('http://api.search.live.net/xml.aspx?Appid='+的AppID +'和;查询='+ HTTPEn code(文本)+
'和;源=翻译'+
'和; Translation.SourceLanguage ='+ fromLang +
'和; Translation.TargetLanguage ='+ toLang));
最后
idhttp.Free;
结束; 尝试
索引节点:= xmldoc.ChildNodes.FindNode('SearchResponse'); 如果分配(索引节点),然后
开始
URI:='http://schemas.microsoft.com/LiveSearch/2008/04/XML/translation';
了mnode:= inode.ChildNodes.FindNode('翻译',URI);
如果分配(了mnode),然后
开始
rnode:= mnode.ChildNodes.FindNode(结果,URI);
如果分配(rnode),然后
开始
irnode:= rnode.ChildNodes.FindNode('TranslationResult',URI);
如果分配(irnode),然后
结果:= irnode.ChildNodes.FindNode('TranslatedTerm',URI).NodeValue;
结束;
结束;
结束;
最后
xmldoc.Free;
结束;
结束;开始
ShowMessage(GetTranslation('你好!','恩','德'));
结束;
我使用自动检测功能,当后面的 http://www.microsofttranslator.com/ 包,结果'从=;'是而如果源语言是英语这将会是'从= EN;'。我试着藏汉发送''作为源语言,但它没有工作 - 没有结果。
我如何使用自动检测?
我这样做是使用他们的AJAX API。如果用,从空构建查询参数,那么该服务会自动检测语言。
这是该查询网址我格式,使对服务的请求:
<$p$p><$c$c>@\"http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId={0}&from=&to={1}&text={2}\"关键一点是从=安培;为= {1}。
I'm using the API of Bing, more precisely - the translation part, and it all works quite well except one thing - auto detection of language. How is that possible?
My code is working fine,incase someone needs to look at:
function HTTPEncode(const AStr: string): string;
const
NoConversion = ['A'..'Z', 'a'..'z', '*', '@', '.', '_', '-'];
var
i: integer;
begin
Result := '';
for i := 1 to Length(AStr) do
begin
if CharInSet(AStr[i],NoConversion) then
Result := Result + AStr[i]
else
Result := Result + Format('%%%.2x',[ord(AStr[i])]);
end;
end;
function GetTranslation(text, fromLang, toLang: string): string;
var
xmldoc: TXMLDocument;
inode,mnode,rnode,irnode: IXMLNode;
j: integer;
uri: string;
idhttp:TIdHttp;
begin
Result := '';
idhttp:=TIdHttp.Create(nil);
xmldoc := TXMLDocument.Create(application);
try
xmldoc.LoadFromXML(idhttp.Get('http://api.search.live.net/xml.aspx?Appid=' + AppID + '&query='+HTTPEncode(text)+
'&sources=translation'+
'&Translation.SourceLanguage=' + fromLang +
'&Translation.TargetLanguage=' + toLang));
finally
idhttp.Free;
end;
try
inode := xmldoc.ChildNodes.FindNode('SearchResponse');
if Assigned(inode) then
begin
uri := 'http://schemas.microsoft.com/LiveSearch/2008/04/XML/translation';
mnode := inode.ChildNodes.FindNode('Translation',uri);
if Assigned(mnode) then
begin
rnode := mnode.ChildNodes.FindNode('Results',uri);
if Assigned(rnode) then
begin
irnode := rnode.ChildNodes.FindNode('TranslationResult',uri);
if Assigned(irnode) then
Result := irnode.ChildNodes.FindNode('TranslatedTerm',uri).NodeValue;
end;
end;
end;
finally
xmldoc.Free;
end;
end;
begin
ShowMessage(GetTranslation('Hello!','en','de'));
end;
I followed the packets from http://www.microsofttranslator.com/ when using auto-detection feature, the result was 'from=;' whereas if source language is English it'd be 'from=en;'.I tried aswell sending '' as source language,but it didn't work out - no result.
How do I use auto-detection?
I did this using their Ajax API. If you build your query with a null "from" parameter, then the service auto-detects the language.
This is the query url I format to make requests to the service:
@"http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId={0}&from=&to={1}&text={2}"
Key thing being "from=&to={1}"
.
这篇关于如何propertly使用Bing翻译API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!