BreakSentences Method
Breaks a piece of text into sentences and returns an array containing the lengths in each sentence.
| Request URI |
|---|
| http://api.microsofttranslator.com/V2/Ajax.svc/BreakSentences |
| Parameter | Description |
|---|---|
|
appId |
Required. A string containing "Bearer" + " " + access token. |
|
text |
Required. A string representing the text to split into sentences. The size of the text must not exceed 10000 characters. |
|
language |
Required. A string representing the language code of input text. |
| Return value |
|---|
| An array of integers representing the lengths of the sentences. The length of the array is the enumber of sentences, and the values are the length of each sentence. |
Note
|
|---|
|
Bing App ID is deprecated and is no longer supported. Please obtain an access token to use the Microsoft Translator API. For details, go here. |
Example
Obtain an Access Token on the server-side
[System.Web.Services.WebMethod]
public static AdmAccessToken GetAccessToken()
{
AdmAccessToken admToken;
string headerValue;
//Get Client Id and Client Secret from https://datamarket.azure.com/developer/applications/
AdmAuthentication admAuth = new AdmAuthentication("clientid", "clientsecret");
admToken = admAuth.GetAccessToken();
// Create a header with the access_token property of the returned token
headerValue = "Bearer" + " " + HttpUtility.UrlEncode(admToken.access_token);
return admToken;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var text = encodeURIComponent("Use the Microsoft Translator webpage widget to deliver your site in the visitor’s language. The visitor never leaves your site, and the widget seamlessly translates each page as they navigate.");
function breakSentence() {
PageMethods.GetAccessToken(OnSucceeded, OnFailed);
}
function OnSucceeded(result, usercontext, methodName) {
window.mycallback = function (response) {
var result = "BreakSentences broke up the above sentence into ";
result += response.length + " sentences.<br />";
var sentence = "Use the Microsoft Translator webpage widget to deliver your site in the visitor’s language. The visitor never leaves your site, and the widget seamlessly translates each page as they navigate.";
for (var i = 0; i < response.length; i++) {
if (i == 0) {
result += "Sentence 1: " + sentence.substring(0, response[i] - 1) + "<br />";
}
else {
result += "Sentence " + (i + 1) + ": " + sentence.substring(response[i - 1], response[i - 1] + response[i] - 1) + "<br />";
}
}
document.getElementById('<%= lblResult.ClientID %>').innerHTML = result;
}
var language = "en";
var s = document.createElement("script");
s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/BreakSentences?oncomplete=mycallback&appId=Bearer " + encodeURIComponent(result.access_token) + "&text=" + text + "&language=" + language;
document.getElementsByTagName("head")[0].appendChild(s);
}
function OnFailed(error, userContext, methodName) {
alert("Error");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true" />
<asp:Label ID="lblText" runat="server" Text="Use the Microsoft Translator webpage widget to deliver your site in the visitor’s language. The visitor never leaves your site, and the widget seamlessly translates each page as they navigate."></asp:Label>
<br />
<asp:Button ID="buttonbreak" runat="server" Text="Break Sentences" OnClientClick="breakSentence();return false;" />
<br />
<asp:Label ID="lblResult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Note