本文整理汇总了C#中StringParser.ReadItemsList方法的典型用法代码示例。如果您正苦于以下问题:C# StringParser.ReadItemsList方法的具体用法?C# StringParser.ReadItemsList怎么用?C# StringParser.ReadItemsList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringParser
的用法示例。
在下文中一共展示了StringParser.ReadItemsList方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TranslateWord
void TranslateWord(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
lock(sederetCode)
{
if(string.IsNullOrEmpty(sederetCode) || coockieTime < DateTime.Now.AddHours(-1))
{ //emulate first access to site
WebRequestHelper helpertop =
new WebRequestHelper(result, new Uri("http://web.cecs.pdx.edu/~bule/bahasa/search.php"),
networkSetting,
WebRequestContentType.UrlEncodedGet, encoding);
helpertop.CookieContainer = cookieContainer;
coockieTime = DateTime.Now;
string responseFromServertop = helpertop.GetResponse();
sederetCode = StringParser.Parse("<input type=\"hidden\" name=\"id\" value=\"", "\"", responseFromServertop);
}
}
WebRequestHelper helper =
new WebRequestHelper(result, new Uri("http://web.cecs.pdx.edu/~bule/bahasa/search.php"),
networkSetting,
WebRequestContentType.UrlEncoded, encoding);
helper.CookieContainer = cookieContainer;
//English to Indonesian, Indonesian to English
string query = "id={0}&language={1}&method=fuzzy&stoken={2}";
if(languagesPair.From == Language.Indonesian)
query = string.Format(CultureInfo.InvariantCulture, query, sederetCode, "Indonesian to English", HttpUtility.UrlEncode(phrase));
else
query = string.Format(CultureInfo.InvariantCulture, query, sederetCode, "English to Indonesian", HttpUtility.UrlEncode(phrase));
helper.AddPostData(query);
string responseFromServer = helper.GetResponse();
lock(sederetCode)
{
sederetCode = StringParser.Parse("<input type=\"hidden\" name=\"id\" value=\"", "\"", responseFromServer);
}
if(!responseFromServer.Contains("<th colspan=\"6\"><hr>"))
{
result.ResultNotFound = result.Childs.Count == 0;
return;
}
string translation = StringParser.Parse("<th colspan=\"6\"><hr>", "<tr><td colspan=\"6\"><hr>", responseFromServer);
Result child;
StringParser subparser;
{
subparser = new StringParser(translation);
string[] subtranslation_list = subparser.ReadItemsList("<td", "</td>");
for(int i = 0; i < subtranslation_list.Length; i+=6)
{
string subphrase = subtranslation_list[i];
subphrase = StringParser.ExtractRight(">", subphrase);
child = CreateNewResult(subphrase, languagesPair, subject);
result.Childs.Add(child);
string subtranslation = subtranslation_list[i+1];
subtranslation = StringParser.ExtractRight(">", subtranslation);
child.Translations.Add(HttpUtility.HtmlDecode(subtranslation));
string abbr = subtranslation_list[i+2];
abbr = StringParser.ExtractRight(">", abbr);
child.Abbreviation = abbr;
}
}
result.ResultNotFound = result.Childs.Count == 0;
}
示例2: DoTranslate
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
//http://www.urduword.com/search.php?English=test
string query = "http://www.urduword.com/search.php?English={0}";
if(languagesPair.From == Language.Urdu)
query = "http://www.urduword.com/search.php?Roman={0}";
query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase));
WebRequestHelper helper =
new WebRequestHelper(result, new Uri(query),
networkSetting,
WebRequestContentType.UrlEncodedGet);
string responseFromServer = helper.GetResponse();
if(responseFromServer.Contains("<p>Could not find translation for <b>"))
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else
{
if(responseFromServer.Contains("<table border=\"0\" width=\"100%\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\" >"))
{
result.ArticleUrl = query;
result.ArticleUrlCaption = phrase;
string translation = StringParser.Parse("<table border=\"0\" width=\"100%\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\" >", "</table>", responseFromServer);
StringParser parser = new StringParser(translation);
string[] translations = parser.ReadItemsList("<tr>", "</tr>");
Result subres = null;
foreach(string str in translations)
{
if(str.Contains("class=\"tablehead\""))
continue;
string word = StringParser.Parse(">", "<", StringParser.Parse("<a", "/a>", str));
translation = StringParser.Parse("align=\"center\">", "<", str);
if(languagesPair.From == Language.Urdu)
{
if(subres == null || subres.Phrase != translation)
{
subres = CreateNewResult(translation, languagesPair, subject);
result.Childs.Add(subres);
}
}
else
{
if(subres == null || subres.Phrase != word)
{
subres = CreateNewResult(word, languagesPair, subject);
result.Childs.Add(subres);
}
}
if(languagesPair.From == Language.Urdu)
subres.Translations.Add(word);
else
subres.Translations.Add(translation);
}
}
//more
if(responseFromServer.Contains("page=2\">Next"))
{
query = "http://www.urduword.com/search.php?English={0}&page=2";
if(languagesPair.From == Language.Urdu)
query = "http://www.urduword.com/search.php?Roman={0}&page=2";
query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase));
string link = "html!<p><a href=\"{0}\" title=\"{0}\">{1}</a></p>";
link = string.Format(link,
query,
"More ...");
Result subres = CreateNewResult(link, languagesPair, subject);
result.Childs.Add(subres);
}
if(result.Childs.Count == 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
}
}
示例3: DoTranslate
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
if(string.IsNullOrEmpty(viewState))
{ //emulate first access to site
WebRequestHelper helpertop =
new WebRequestHelper(result, new Uri("http://www.priberam.pt/dlpo/dlpo.aspx"),
networkSetting,
WebRequestContentType.UrlEncodedGet,
Encoding.GetEncoding("iso-8859-1"));
string responseFromServertop = helpertop.GetResponse();
viewState = StringParser.Parse("name=\"__VIEWSTATE\" value=\"", "\"", responseFromServertop);
}
WebRequestHelper helper =
new WebRequestHelper(result, new Uri("http://www.priberam.pt/dlpo/definir_resultados.aspx"),
networkSetting,
WebRequestContentType.UrlEncoded,
Encoding.GetEncoding("iso-8859-1"));
string queryStr = "__EVENTTARGET=&__EVENTARGUMENT=&NOTVIEWSTATE={0}" +
"&definActionTarget=%2Fdlpo%2Fdefinir_resultados.aspx" +
"&pesqActionTarget=%2Fdlpo%2Fpesquisar_resultados.aspx" +
"&conjugaActionTarget=%2Fdlpo%2Fconjugar_resultados.aspx" +
"&CVdefinActionTarget=%2Fdcvpo%2Fdefinir_resultados.aspx" +
"&CVpesqActionTarget=%2Fdcvpo%2Fpesquisar_resultados.aspx" +
"&CVconjugaActionTarget=%2Fdcvpo%2Fconjugar_resultados.aspx" +
"&h_seccao_index=&h_vista=&h_abrev_cat=1&h_abrev_flex=1&h_abrev_dom=1" +
"&h_abrev_exemp=1&h_abrev_etim=1&h_abrev_outras=1&h_filtro=&h_dominio=" +
"&h_var_geografica=&h_categoria=&h_reg_linguistico=&h_etimologia=" +
"&h_desc_dominio=&h_desc_var_geografica=&h_desc_categoria=" +
"&h_desc_reg_linguistico=&h_desc_etimologia=&accao=" +
"&h_texto_pesquisa={1}&h_n={2}&accao=" +
"&pal={1}&Dicionario%3ApalVisible={3}";
string query = string.Format(queryStr,
HttpUtility.UrlEncode(viewState, helper.Encoding),
HttpUtility.UrlEncode(phrase, helper.Encoding),
0,
HttpUtility.UrlEncode(phrase, helper.Encoding)
);
helper.AddPostData(query);
string responseFromServer = helper.GetResponse();
viewState = StringParser.Parse("name=\"__VIEWSTATE\" value=\"", "\"", responseFromServer);
if(responseFromServer.Contains("A palavra não foi encontrada.</span>"))
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
if(responseFromServer.Contains("<blockquote>"))
{
StringParser parser = new StringParser(responseFromServer);
string[] subblocks = parser.ReadItemsList("<blockquote>", "</blockquote>");
if(subblocks.Length == 1)
{
SetResult(result, subblocks[0]);
}
else
{
foreach(string block in subblocks)
{
Result child = new Result(result.ServiceItem, phrase, result.LanguagePair, result.Subject);
result.Childs.Add(child);
SetResult(child, block);
}
}
}
else if(responseFromServer.Contains("javascript:SeleccionaEntrada(""))
{
StringParser parser = new StringParser(responseFromServer);
string[] sublinks = parser.ReadItemsList("javascript:SeleccionaEntrada("", "")\"");
if(sublinks.Length <= 1)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
string key = "","";
foreach(string sublink in sublinks)
{
int idx = sublink.IndexOf(key);
if(idx < 0)
throw new TranslationException("Can't found start tag : \"","\" in string : " + sublink);
string val = sublink.Substring(0, idx);
string num = sublink.Substring(idx + key.Length);
query = string.Format(queryStr,
HttpUtility.UrlEncode(viewState, helper.Encoding),
HttpUtility.UrlEncode(val, helper.Encoding),
num,
HttpUtility.UrlEncode(phrase, helper.Encoding));
helper =
new WebRequestHelper(result, new Uri("http://www.priberam.pt/dlpo/definir_resultados.aspx"),
networkSetting,
//.........这里部分代码省略.........
示例4: DoTranslate
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
string query = "http://www.online-translator.com/text/references.aspx?prmtlang=en&direction={0}&word={1}";
query = string.Format(query, PromtUtils.ConvertLanguagesPair(languagesPair),
HttpUtility.UrlEncode(phrase));
WebRequestHelper helper =
new WebRequestHelper(result, new Uri(query),
networkSetting,
WebRequestContentType.UrlEncodedGet);
string responseFromServer = helper.GetResponse();
if(responseFromServer.IndexOf("class=\"ref_not_found\"") >= 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
result.ArticleUrl = query;
result.ArticleUrlCaption = phrase;
StringParser parser = new StringParser(responseFromServer);
string[] translation_list = parser.ReadItemsList("<span class=\"ref_source\"", "</table>");
string subphrase;
string abbreviation;
Result child;
StringParser subparser;
foreach(string translation in translation_list)
{
subphrase = StringParser.Parse(">","</span>",translation);
abbreviation = StringParser.Parse("<span class=\"ref_psp\">","</span>",translation);
child = CreateNewResult(subphrase, languagesPair, subject);
child.Abbreviation = abbreviation;
result.Childs.Add(child);
subparser = new StringParser(translation);
string[] subtranslation_list = subparser.ReadItemsList("<span class=\"ref_result\">", "</span>");
foreach(string subtranslation in subtranslation_list)
{
child.Translations.Add(HttpUtility.HtmlDecode(subtranslation));
}
}
}
示例5: DoTranslate
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
if(string.IsNullOrEmpty(viewState))
{ //emulate first access to site
WebRequestHelper helpertop =
new WebRequestHelper(result, new Uri("http://www.prolingoffice.com/page.aspx?l1=43"),
networkSetting,
WebRequestContentType.UrlEncodedGet,
Encoding.GetEncoding(1251));
string responseFromServertop = helpertop.GetResponse();
viewState = StringParser.Parse("id=\"__VIEWSTATE\" value=\"", "\"", responseFromServertop);
eventValidation = StringParser.Parse("id=\"__EVENTVALIDATION\" value=\"", "\"", responseFromServertop);
}
WebRequestHelper helper =
new WebRequestHelper(result, new Uri("http://www.prolingoffice.com/page.aspx?l1=43"),
networkSetting,
WebRequestContentType.UrlEncoded,
Encoding.GetEncoding(1251));
//__EVENTTARGET=_ctl1%24Menu1&__EVENTARGUMENT=1&
//__VIEWSTATE={0}&q={1}&_ctl1%3AtsLang=rbLangU&LanguageH=RUS&
//__EVENTVALIDATION={2}
string query = "__EVENTTARGET=_ctl1%24Menu1&__EVENTARGUMENT=1&__VIEWSTATE={0}&q={1}&_ctl1%3AtsLang=rbLangU&LanguageH=RUS&__EVENTVALIDATION={2}";
query = string.Format(query,
HttpUtility.UrlEncode(viewState, helper.Encoding),
HttpUtility.UrlEncode(phrase, helper.Encoding),
HttpUtility.UrlEncode(eventValidation, helper.Encoding));
helper.AddPostData(query);
string responseFromServer = helper.GetResponse();
viewState = StringParser.Parse("id=\"__VIEWSTATE\" value=\"", "\"", responseFromServer);
eventValidation = StringParser.Parse("id=\"__EVENTVALIDATION\" value=\"", "\"", responseFromServer);
if(responseFromServer.IndexOf("��������� ����� ����� �� ��������. ��������� �������� ����� ������, ��� ����������� � �����������, ��� �������� � <a") >= 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else if(responseFromServer.IndexOf("� ����� ���������� ������. �������� ������� � ����:</b>") >= 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else if(responseFromServer.IndexOf("������� ��� ����� ����� �� ��������. ��������� �������� ����� ������, ��� ����������� � �����������, ��� �������� � <") >= 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else if(responseFromServer.IndexOf("� ����� ���������� ������.</b>") >= 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
string translation = StringParser.Parse("<span class=\"wrd\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\">", "</TABLE></span>", responseFromServer);
string abbr = StringParser.Parse("title=\"", "\"", translation);
abbr += " " + StringParser.Parse("xslt\">", "</span>", translation).Trim();
//result.Abbreviation = abbr;
StringParser parser = new StringParser(translation);
string[] translations = parser.ReadItemsList("<span class=\"tolk\"", "</td><td></td></tr>", "3495783-4572385");
foreach(string subtranslation in translations)
{
translation = StringParser.Parse(">", "</span>", subtranslation);
Result subres;
subres = CreateNewResult(translation, languagesPair, subject);
result.Childs.Add(subres);
parser = new StringParser(subtranslation);
string[] subtranslations = parser.ReadItemsList("<a ", "/a>", "3495783-4572385");
foreach(string s in subtranslations)
subres.Translations.Add(StringParser.Parse(">", "<", s));
}
}
示例6: DoTranslate
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
WebRequestHelper helper =
new WebRequestHelper(result, new Uri("http://intertran.tranexp.com/Translate/result.shtml"),
networkSetting,
WebRequestContentType.UrlEncoded);
helper.Encoding = GetEncoding(languagesPair.From, languagesPair.To);//Encoding.GetEncoding(GetEncoding(languagesPair.To));
string query = "Submit.x=56&Submit.y=10&from={0}&keyb=non&text={1}&to={2}&translation=";
query = string.Format(CultureInfo.InvariantCulture,
query,
ConvertLanguage(languagesPair.From),
HttpUtility.UrlEncode(phrase, Encoding.GetEncoding(GetEncoding(languagesPair.From))),
ConvertLanguage(languagesPair.To)
);
helper.AddPostData(query);
string responseFromServer = helper.GetResponse();
if(string.IsNullOrEmpty(responseFromServer))
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else if(responseFromServer.Contains("there was no translation for your original query.</center>"))
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else if(responseFromServer.Contains("name=\"translation\">An error occurred during translation. Please try again later.</textarea>"))
{
//result.ResultNotFound = true;
throw new TranslationException("An error occurred during translation. Please try again later.");
}
else if(responseFromServer.Contains("<center>Warning:<br> Could not open"))
{
//result.ResultNotFound = true;
throw new TranslationException("An error occurred during translation. Please try again later.");
}
else if(responseFromServer.Contains("<center>Warning:</strong><br> Could not open"))
{
//result.ResultNotFound = true;
throw new TranslationException("An error occurred during translation. Please try again later.");
}
else
{
string translation = StringParser.Parse("<INPUT TYPE=\"hidden\" NAME=\"alltrans\" VALUE=\"", "\">", responseFromServer).Trim();
if(string.IsNullOrEmpty(translation))
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
string subphrase;
bool firstRun = true;
Result subres = result;
StringParser parser = new StringParser(translation);
string[] translations = parser.ReadItemsList("{{", "}}");
foreach(string subtranslation in translations)
{
string subtrans = "|" + subtranslation + "|";
parser = new StringParser(subtrans);
string[] subtranslations = parser.ReadItemsList("|", "|");
if(subtranslations.Length > 1)
{
subphrase = subtranslations[0];
if(firstRun && translation.Length == 1 && string.Compare(subphrase, phrase, true, CultureInfo.InvariantCulture) ==0)
{
for(int i = 1; i < subtranslations.Length; i++)
result.Translations.Add(subtranslations[i]);
return;
}
firstRun = false;
subres = CreateNewResult(subphrase, languagesPair, subject);
result.Childs.Add(subres);
for(int i = 1; i < subtranslations.Length; i++)
subres.Translations.Add(subtranslations[i]);
}
}
}
}
示例7: DoTranslate
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
//http://www.urduenglishdictionary.org/English-To-Urdu-Translation/test/Page-1.htm
string query = "http://www.urduenglishdictionary.org/English-To-Urdu-Translation/{0}/Page-1.htm";
if(languagesPair.From == Language.Urdu)
query = "http://www.urduenglishdictionary.org/Urdu-To-English-Translation/{0}/Page-1.htm";
query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase));
WebRequestHelper helper =
new WebRequestHelper(result, new Uri(query),
networkSetting,
WebRequestContentType.UrlEncodedGet);
string responseFromServer = helper.GetResponse();
if(responseFromServer.Contains("</B> could not be found!</h2>") &&
responseFromServer.Contains("<U>Suggestions:</U><BR><BR><BR><BR><BR><BR></div>")
)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else if(responseFromServer.Contains("</B> could not be found!</h2>") &&
responseFromServer.Contains("<U>Suggestions:</U><BR><BR><a")
)
{
//suggestions exists
string suggestions = StringParser.Parse("<U>Suggestions:</U><BR><BR>", "</div>", responseFromServer);
StringParser parser = new StringParser(suggestions);
string[] suggestions_list = parser.ReadItemsList("<a href='", "</a>");
foreach(string str in suggestions_list)
{
result.Translations.Add("html!<p><a href='http://www.urduenglishdictionary.org" + str + "</a></p>");
}
}
else
{
if(responseFromServer.Contains("<tr class='head-row'>"))
{
result.ArticleUrl = query;
result.ArticleUrlCaption = phrase;
string translation = StringParser.Parse("<tr class='head-row'>", "</table>", responseFromServer);
StringParser parser = new StringParser(translation);
string[] translations = parser.ReadItemsList("<tr class='data-row'>", "</tr>");
Result subres = null;
foreach(string str in translations)
{
string word = "";
if(str.Contains("align=left valign=top nowrap>"))
word = StringParser.Parse("align=left valign=top nowrap>", "<sup>", str).Trim();
else
word = StringParser.Parse("<td class='data-cell' align=left valign=top>", "<",
StringParser.Parse("<td class='data-cell' align=left valign=top>", "sup>", str)).Trim();
string abbr = StringParser.Parse("<span class='feature'>", "</span>", str).Trim();
translation = StringParser.Parse("<td class='urdu-cell' align=right valign=top>", "<", str).Trim();
if(translations.Length == 1)
subres = result;
else if(languagesPair.From == Language.Urdu)
{
subres = CreateNewResult(translation, languagesPair, subject);
result.Childs.Add(subres);
}
else
{
subres = CreateNewResult(word, languagesPair, subject);
result.Childs.Add(subres);
}
subres.Abbreviation = abbr;
if(languagesPair.From == Language.Urdu)
subres.Translations.Add(word);
else
subres.Translations.Add(translation);
}
}
//more
if(responseFromServer.Contains("Page-2.htm"))
{
query = "http://www.urduenglishdictionary.org/English-To-Urdu-Translation/{0}/Page-2.htm";
if(languagesPair.From == Language.Urdu)
query = "http://www.urduenglishdictionary.org/Urdu-To-English-Translation/{0}/Page-2.htm";
query = string.Format(CultureInfo.InvariantCulture, query, HttpUtility.UrlEncode(phrase));
string link = "html!<p><a href=\"{0}\" title=\"{0}\">{1}</a></p>";
link = string.Format(link,
query,
"More ...");
Result subres = CreateNewResult(link, languagesPair, subject);
result.Childs.Add(subres);
}
if(result.Childs.Count == 0 && result.Translations.Count == 0)
{
result.ResultNotFound = true;
//.........这里部分代码省略.........
示例8: DoTranslate
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
string responseFromServer = UlifHelper.GetSynonymsPage(phrase, networkSetting);
if(string.IsNullOrEmpty(responseFromServer))
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
responseFromServer = StringParser.Parse("<div class=\"p_cl\">", "</div>", responseFromServer);
responseFromServer = StringParser.RemoveAll("<A ondblclick", ">", responseFromServer);
responseFromServer = responseFromServer.Replace("</A>", "");
responseFromServer = responseFromServer.Replace("<B>", "");
responseFromServer = responseFromServer.Replace("</B>", "");
responseFromServer = responseFromServer.Replace("<I>", "");
responseFromServer = responseFromServer.Replace("</I>", "");
StringParser parser = new StringParser(responseFromServer);
string[] translations = parser.ReadItemsList("<P>", "</P>", "3495783-4572385");
if(translations.Length == 1)
{
result.Translations.Add(translations[0]);
}
else
{
foreach(string subtranslation in translations)
{
Result subres = CreateNewResult("", languagesPair, subject);
result.Childs.Add(subres);
subres.Translations.Add(subtranslation);
}
}
}
示例9: DoTranslate
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
string query = "http://www.pcdigest.net/cgi-bin/u/book/sis.pl?Qry={0}&found=10&action=search";
query = string.Format(query, HttpUtility.UrlEncode(phrase, Encoding.GetEncoding(1251)));
WebRequestHelper helper =
new WebRequestHelper(result, new Uri(query),
networkSetting,
WebRequestContentType.UrlEncodedGet,
Encoding.GetEncoding(1251));
string responseFromServer = helper.GetResponse();
responseFromServer = StringParser.Parse("<!-- START CONTENT -->", "<!-- RIGHT SHORT TABLE -->", responseFromServer);
responseFromServer = StringParser.Parse("<P align=justify>", "</table>", responseFromServer);
responseFromServer = responseFromServer.Replace("<font color=darkgreen>", "");
responseFromServer = responseFromServer.Replace("<font color=red>", "");
responseFromServer = responseFromServer.Replace("</font>", "");
responseFromServer = responseFromServer.Replace("<B>", "");
responseFromServer = responseFromServer.Replace("</B>", "");
responseFromServer = responseFromServer.Replace("<I>", "");
responseFromServer = responseFromServer.Replace("</I>", "");
responseFromServer = responseFromServer.Replace("<i>", "");
responseFromServer = responseFromServer.Replace("</i>", "");
responseFromServer = responseFromServer.Replace("<u>", "");
responseFromServer = responseFromServer.Replace("</u>", "");
StringParser parser = new StringParser(responseFromServer);
string[] translations = parser.ReadItemsList("<P align=justify>", "</P>", "3495783-4572385");
if(translations.Length == 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
result.ArticleUrl = query;
result.ArticleUrlCaption = phrase;
string translation, abrr, data;
int idx;
foreach(string subtranslation in translations)
{
idx = subtranslation.IndexOf("(");
if(idx < 0)
idx = subtranslation.IndexOf("-");
translation = subtranslation.Substring(0, idx);
if(subtranslation.IndexOf("(") >= 0)
abrr = StringParser.Parse("(", ")", subtranslation);
else
abrr = "";
idx = subtranslation.IndexOf("-");
if(idx >= 0)
data = subtranslation.Substring(idx + 1);
else
data = "";
Result subres;
if(translations.Length > 1)
{
subres = CreateNewResult(translation, languagesPair, subject);
result.Childs.Add(subres);
}
else
subres = result;
if(!string.IsNullOrEmpty(abrr))
subres.Abbreviation = abrr;
if(!string.IsNullOrEmpty(data))
subres.Translations.Add(data);
}
}
示例10: DoSearch
public static void DoSearch(string searchHost, string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
string query = "http://{0}.{1}/w/api.php?action=query&list=search&srsearch={2}&srlimit={3}&format=xml&srwhat=text";
string lang = WikiUtils.ConvertLanguage(languagesPair.To);
query = string.Format(query, lang,
searchHost,
HttpUtility.UrlEncode(phrase),
MonolingualSearchEngine.ResultsLimit);
WebRequestHelper helper =
new WebRequestHelper(result, new Uri(query),
networkSetting,
WebRequestContentType.UrlEncodedGet);
string responseFromServer = helper.GetResponse();
if(responseFromServer.IndexOf("info=\"") >= 0)
{
string error = StringParser.Parse("info=\"", "\"", responseFromServer);
throw new TranslationException(error);
}
if(responseFromServer.IndexOf("<p ns=\"0\" title=\"") < 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
StringParser parser = new StringParser(responseFromServer);
string[] items = parser.ReadItemsList("<p ns=\"0\" title=\"", "\"", "787654323");
string link;
foreach(string part in items)
{
//link = "html!";
link = "html!<p><a href=\"http://{0}.{1}/wiki/{2}\" title=\"http://{0}.{1}/wiki/{2}\">{3}</a></p>";
link = string.Format(link, lang,
searchHost,
part,
part);
result.Translations.Add(link);
}
}
示例11: TranslateWord
void TranslateWord(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
lock(sederetCode)
{
if(string.IsNullOrEmpty(sederetCode) || coockieTime < DateTime.Now.AddHours(-1))
{ //emulate first access to site
WebRequestHelper helpertop =
new WebRequestHelper(result, new Uri("http://www.sederet.com/translate.php"),
networkSetting,
WebRequestContentType.UrlEncodedGet, encoding);
helpertop.CookieContainer = cookieContainer;
coockieTime = DateTime.Now;
string responseFromServertop = helpertop.GetResponse();
sederetCode = StringParser.Parse("<input type=\"hidden\" name=\"var\" value=\"", "\"", responseFromServertop);
}
}
WebRequestHelper helper =
new WebRequestHelper(result, new Uri("http://www.sederet.com/translate.php"),
networkSetting,
WebRequestContentType.UrlEncoded, encoding);
helper.CookieContainer = cookieContainer;
//eng2indo, indo2eng
string query = "from_to={0}&kata={1}&var={2}";
if(languagesPair.From == Language.Indonesian)
query = string.Format(CultureInfo.InvariantCulture, query, "indo2eng", HttpUtility.UrlEncode(phrase), sederetCode);
else
query = string.Format(CultureInfo.InvariantCulture, query, "eng2indo", HttpUtility.UrlEncode(phrase), sederetCode);
helper.AddPostData(query);
string responseFromServer = helper.GetResponse();
lock(sederetCode)
{
sederetCode = StringParser.Parse("<input type=\"hidden\" name=\"var\" value=\"", "\"", responseFromServer);
}
string translation = StringParser.Parse("<span id=\"result_title\"", "id=\"part_right\">", responseFromServer);
Result child;
StringParser subparser;
if(translation.Contains("<b id=\"match_title\">Exact Match:</b>"))
{
child = CreateNewResult(phrase, languagesPair, subject);
result.Childs.Add(child);
string subblock = StringParser.Parse("<b id=\"match_title\">Exact Match:</b>", "</table>", translation);
subparser = new StringParser(subblock);
string[] subtranslation_list = subparser.ReadItemsList("<td id='result_td'>", "</td>");
for(int i = 0; i < subtranslation_list.Length; i+=2)
{
string subtranslation = subtranslation_list[i+1];
subtranslation = StringParser.RemoveAll("<", ">", subtranslation);
child.Translations.Add(HttpUtility.HtmlDecode(subtranslation));
}
}
if(translation.Contains("<b id=\"match_title\">Other Match(es):</b>"))
{
string subblock = StringParser.Parse("<b id=\"match_title\">Other Match(es):</b>", "</table>", translation);
subparser = new StringParser(subblock);
string[] subtranslation_list = subparser.ReadItemsList("<td id='result_td'>", "</td>");
for(int i = 0; i < subtranslation_list.Length; i+=2)
{
string subphrase = subtranslation_list[i];
subphrase = StringParser.RemoveAll("<", ">", subphrase);
child = CreateNewResult(subphrase, languagesPair, subject);
result.Childs.Add(child);
string subtranslation = subtranslation_list[i+1];
subtranslation = StringParser.RemoveAll("<", ">", subtranslation);
child.Translations.Add(HttpUtility.HtmlDecode(subtranslation));
}
}
result.ResultNotFound = result.Childs.Count == 0;
}
示例12: DoTranslateSentences
public static void DoTranslateSentences(ServiceItem serviceItem, string url, string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
string query = string.Format(url, HttpUtility.UrlEncode(phrase, Encoding.GetEncoding(1251)));
query += ConvertLanguagesPair(languagesPair);
WebRequestHelper helper =
new WebRequestHelper(result, new Uri(query),
networkSetting,
WebRequestContentType.UrlEncodedGet, Encoding.GetEncoding(1251));
string responseFromServer = helper.GetResponse();
if(responseFromServer.IndexOf("</form>") < 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else
{
result.EditArticleUrl = query;
string translation = StringParser.Parse("</form>", "<table", responseFromServer);
translation = translation.Replace("<p>", "<end><begin>");
translation += "<end>";
StringParser parser = new StringParser(translation);
string[] translations = parser.ReadItemsList("<begin>", "<end>");
string subpart;
string subphrase = "";
string subtranslation;
Result subsubres = null;
foreach(string part in translations)
{
subpart = part;
if(subpart.Contains("title=\""))
subphrase = StringParser.Parse("title=\"", "\"", subpart);
else
subphrase = phrase;
subtranslation = StringParser.RemoveAll("<span", ">", subpart);
subtranslation = StringParser.RemoveAll("<a", ">", subtranslation);
subtranslation = subtranslation.Replace("</a>", "");
subtranslation = subtranslation.Replace("<i>", "");
subtranslation = subtranslation.Replace("</i>", "");
subtranslation = subtranslation.Replace("</span>", "");
subtranslation = subtranslation.Replace("<sub>", "");
subtranslation = subtranslation.Replace("</sub>", "");
subtranslation = subtranslation.Replace("<b>", "");
subtranslation = subtranslation.Replace("</b>", "");
subtranslation = subtranslation.Replace(">>", "");
subsubres = serviceItem.CreateNewResult(subphrase, languagesPair, subject);
result.Childs.Add(subsubres);
int idx = subtranslation.IndexOf("\n");
if(idx < 0)
subsubres.Translations.Add("Parse error. Can't found '\\n' in " + subtranslation);
else
{
subsubres.Translations.Add(subtranslation.Substring(0, idx));
subsubres.Translations.Add(subtranslation.Substring(idx + 1).Trim());
}
}
}
}
示例13: DoTranslatePhrases
public static void DoTranslatePhrases(ServiceItem serviceItem, string url, string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
string query = string.Format(url, EncodePhrase(phrase, languagesPair));
query += ConvertLanguagesPair(languagesPair);
WebRequestHelper helper =
new WebRequestHelper(result, new Uri(query),
networkSetting,
WebRequestContentType.UrlEncodedGet, GetEncoding(languagesPair));
result.ArticleUrl = query;
result.ArticleUrlCaption = phrase;
if(languagesPair.From == Language.Japanese || languagesPair.To == Language.Japanese)
helper.StreamConvertor = new EucJPStreamFixer();
string responseFromServer = helper.GetResponse();
if((responseFromServer.IndexOf("ask in forum</a>") >= 0
&& responseFromServer.IndexOf(" single words found") < 0)
||
(responseFromServer.IndexOf("спросить в форуме</a>") >= 0
&& responseFromServer.IndexOf(" найдены отдельные слова") < 0)
||
responseFromServer.IndexOf("<table cellspacing=\"0\" border=\"0\" width=\"100%\">") < 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else
{
string translation = StringParser.Parse("createAutoComplete();", "<script><!--", responseFromServer);
translation = translation.Replace("</TD>", "</td>");
translation = translation.Replace("<TD width", "<td width");
translation = translation.Replace("</td><tr>", "</td></tr><tr>");
translation = translation.Replace("</a><tr>", "</a></td></tr><tr>");
translation = translation.Replace("\"><tr>", "\"></td></tr><tr>");
translation = translation.Replace("</tr></td>", "</td></tr>");
StringParser parser = new StringParser(translation);
string[] translations = parser.ReadItemsList("<tr>", "</td></tr>");
string subpart;
string subphrase = "";
string subtranslation;
string abbreviation;
Result subsubres = null;
string subres_url;
foreach(string part in translations)
{
if(!part.Contains("<td width=\"5%\">"))
continue;
subpart = part;
subphrase = StringParser.Parse("<td width=\"5%\">", "</td>", subpart);
subres_url = StringParser.Parse("href=\"", "\"", subphrase);
subphrase = StringParser.RemoveAll("<span", ">", subphrase);
subphrase = StringParser.RemoveAll("<a", ">", subphrase);
subphrase = subphrase.Replace("</a>", "");
subphrase = subphrase.Replace("<i>", "");
subphrase = subphrase.Replace("</i>", "");
subphrase = subphrase.Replace("</span>", "");
subphrase = subphrase.Replace("<sub>", "");
subphrase = subphrase.Replace("</sub>", "");
subphrase = subphrase.Replace("<b>", "");
subphrase = subphrase.Replace("</b>", "");
abbreviation = StringParser.Parse("<i>", "</i>", subpart);
subsubres = serviceItem.CreateNewResult(subphrase, languagesPair, subject);
subsubres.Abbreviation = abbreviation;
subsubres.ArticleUrl = "http://www.multitran.ru/c/" + subres_url;
result.Childs.Add(subsubres);
subtranslation = subpart + "<end>";
subtranslation = StringParser.Parse("<td width=\"20%\">", "</td>", subtranslation);
subtranslation = StringParser.RemoveAll("<span", ">", subtranslation);
subtranslation = StringParser.RemoveAll("<a", ">", subtranslation);
subtranslation = StringParser.RemoveAll("<td", ">", subtranslation);
subtranslation = subtranslation.Replace("</td>", " ");
subtranslation = subtranslation.Replace("</a>", "");
subtranslation = subtranslation.Replace("<i>", "");
subtranslation = subtranslation.Replace("</i>", "");
subtranslation = subtranslation.Replace("</span>", "");
subtranslation = subtranslation.Replace("<sub>", "");
subtranslation = subtranslation.Replace("</sub>", "");
subtranslation = subtranslation.Replace("<b>", "");
subtranslation = subtranslation.Replace("</b>", "");
subsubres.Translations.Add(subtranslation);
subtranslation = subpart + "<end>";
subtranslation = StringParser.Parse("<td width=\"75%\">", "<end>", subtranslation);
subtranslation = StringParser.RemoveAll("<span", ">", subtranslation);
subtranslation = StringParser.RemoveAll("<a", ">", subtranslation);
subtranslation = StringParser.RemoveAll("<td", ">", subtranslation);
//.........这里部分代码省略.........
示例14: DoTranslateDictionary
public static void DoTranslateDictionary(ServiceItem serviceItem, string url, string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
string query = string.Format(url, EncodePhrase(phrase, languagesPair));
query += ConvertLanguagesPair(languagesPair);
WebRequestHelper helper =
new WebRequestHelper(result, new Uri(query),
networkSetting,
WebRequestContentType.UrlEncodedGet, GetEncoding(languagesPair));
if(languagesPair.From == Language.Japanese || languagesPair.To == Language.Japanese)
helper.StreamConvertor = new EucJPStreamFixer();
string responseFromServer = helper.GetResponse();
if((responseFromServer.IndexOf("ask in forum</a>") >= 0
&& responseFromServer.IndexOf(" single words found") < 0)
||
(responseFromServer.IndexOf("спросить в форуме</a>") >= 0
&& responseFromServer.IndexOf(" найдены отдельные слова") < 0)
||
responseFromServer.IndexOf("<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">") < 0)
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
else
{
string translation = StringParser.Parse("createAutoComplete();", "</table>", responseFromServer);
translation = translation.Replace("</TD>", "</td>");
translation = translation.Replace("<TD width", "<td width");
translation = translation.Replace("</td><tr>", "</td></tr><tr>");
translation = translation.Replace("</a><tr>", "</a></td></tr><tr>");
translation = translation.Replace("\"><tr>", "\"></td></tr><tr>");
translation = translation.Replace("</td>\r\n </tr>", "</td></tr>");
StringParser parser = new StringParser(translation);
string[] translations = parser.ReadItemsList("<tr>", "</td></tr>");
string subpart;
string subphrase = "";
string subtranslation;
string abbreviation;
Result subres = null;
Result subsubres = null;
foreach(string part in translations)
{
subpart = part;
if(subpart.StartsWith("<td bgcolor=\"#DBDBDB\" width=\"100%\" colspan=\"2\">"))
{ //new subres
subpart = subpart.Replace("<td bgcolor=\"#DBDBDB\" width=\"100%\" colspan=\"2\">","");
subphrase = StringParser.Parse("\">", "</a>", subpart);
subphrase = StringParser.RemoveAll("<span", ">", subphrase);
subphrase = StringParser.RemoveAll("<a", ">", subphrase);
subphrase = subphrase.Replace("<FONT SIZE=2>", "");
subphrase = subphrase.Replace("</FONT>", "");
if(subpart.Contains("<em>"))
abbreviation = StringParser.Parse("<em>", "</em>", subpart);
else
abbreviation = "";
subres = serviceItem.CreateNewResult(subphrase, languagesPair, subject);
subres.Abbreviation = abbreviation;
subres.ArticleUrl = query;
//subres.ArticleUrlCaption = phrase;
result.Childs.Add(subres);
}
else
{
if(!subpart.Contains("title=\""))
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
abbreviation = StringParser.Parse("title=\"", "\"", subpart);
abbreviation += "(";
abbreviation += StringParser.Parse("<i>", "</i>", subpart);
abbreviation += ")";
//subsubres = serviceItem.CreateNewResult("", languagesPair, subject);
//subsubres.Abbreviation = abbreviation;
subsubres = serviceItem.CreateNewResult(abbreviation, languagesPair, subject);
subsubres.ArticleUrl = "http://www.multitran.ru/c/" + StringParser.Parse("href=\"", "\"", subpart);
subres.Childs.Add(subsubres);
subtranslation = subpart.Substring(subpart.IndexOf("<td>") + 4);
subtranslation = StringParser.RemoveAll("<span", ">", subtranslation);
subtranslation = subtranslation.Replace("<a href=\"m.exe?t=", "<end><begin><a");
subtranslation = StringParser.RemoveAll("<a", ">", subtranslation);
subtranslation = subtranslation.Replace("</a>", "");
subtranslation = subtranslation.Replace("<i>", "");
subtranslation = subtranslation.Replace("</i>", "");
subtranslation = subtranslation.Replace("</span>", "");
subtranslation = subtranslation.Replace("<sub>", "");
subtranslation = subtranslation.Replace("</sub>", "");
subtranslation = subtranslation.Replace("<b>", "");
subtranslation = subtranslation.Replace("</b>", "");
subtranslation += "<end>";
//.........这里部分代码省略.........
示例15: DoTranslate
protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
{
string query = "http://www.google.com/dictionary?aq=f&langpair={1}&q={0}&hl=en";
query = string.Format(query, HttpUtility.UrlEncode(phrase, System.Text.Encoding.UTF8 ), GoogleUtils.ConvertLanguagesPair(languagesPair));
WebRequestHelper helper =
new WebRequestHelper(result, new Uri(query),
networkSetting,
WebRequestContentType.UrlEncodedGet);
result.ArticleUrl = query;
result.ArticleUrlCaption = phrase;
string responseFromServer = helper.GetResponse();
if(responseFromServer.Contains("No dictionary translations were found for: <strong>"))
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
if(responseFromServer.Contains("No dictionary definitions were found for: <strong>"))
{
result.ResultNotFound = true;
throw new TranslationException("Nothing found");
}
result.HasAudio = responseFromServer.Contains("<object data=\"/dictionary/flash");
responseFromServer = StringParser.Parse("<div class=\"dct-srch-otr\">", "<div class=\"dct-rt-sct\">", responseFromServer);
//pronuncation
if(responseFromServer.Contains("<span class=\"dct-tp\">/"))
{
string pronuncation = StringParser.Parse("<span class=\"dct-tp\">/", "/</span>", responseFromServer);
pronuncation = pronuncation.Trim();
result.Abbreviation = pronuncation;
}
//translations
//string translations = StringParser.Parse("<div class=\"dct-srch-rslt\">", "</div>", responseFromServer);
string translations = "";
//TODO: additional sences like in "water" - "water down" not supported
if(responseFromServer.Contains("<div class=\"sen\">"))
translations = StringParser.Parse("<ul class=\"dct-e2\" id=\"pr-root\" >", "</ul>\n<div class=\"sen\">", responseFromServer);
else if(responseFromServer.Contains("<h3>Related phrases</h3>"))
translations = StringParser.Parse("<ul class=\"dct-e2\" id=\"pr-root\" >", "</ul>\n<h3>Related phrases</h3>", responseFromServer);
else if(responseFromServer.Contains("<h3>Web definitions</h3>"))
translations = StringParser.Parse("<ul class=\"dct-e2\" id=\"pr-root\" >", "</ul>\n<h3>Web definitions</h3>", responseFromServer);
else
translations = StringParser.Parse("<ul class=\"dct-e2\" id=\"pr-root\" >", "</ul>", responseFromServer);
StringParser parser = null;
List<string> subtranslations = new List<string>();
if(translations.Contains("<li class=\"dct-ec\""))
{
//"</li>\n</ul>\n</li>"
parser = new StringParser(translations);
string[] subtranslation_list = parser.ReadItemsList("<li class=\"dct-ec\"", "</li>\n</ul>\n</li>", "3485730457203");
subtranslations.AddRange(subtranslation_list);
}
else if(translations.Contains("<div style=\"font-weight:bold\">Synonyms:</div>"))
{
Result synonyms_tr = CreateNewResult("Synonyms", languagesPair, subject);
result.Childs.Add(synonyms_tr);
string synonyms = StringParser.Parse("<div style=\"font-weight:bold\">Synonyms:</div>", "</div>", translations);
parser = new StringParser(synonyms);
string[] syn_group_list = parser.ReadItemsList("<li>", "</li>", "3485730457203");
foreach(string syngroup in syn_group_list)
{
string syn_group_name = StringParser.Parse("title=\"Part-of-speech\">", "</span>", syngroup);
Result syn_tr = CreateNewResult(syn_group_name, languagesPair, subject);
synonyms_tr.Childs.Add(syn_tr);
parser = new StringParser(syngroup);
string[] syn_list = parser.ReadItemsList("<a", "</a>", "3485730457203");
foreach(string syn in syn_list)
{
string synonym = StringParser.ExtractRight(">", syn);
syn_tr.Translations.Add(synonym);
}
}
subtranslations.Add(translations);
}
else
{
subtranslations.Add(translations);
}
Result subres_tr = result;
Result sub2res_tr = null;
Result sub3res_tr = null;
string abbr_str;
foreach(string subtranslation in subtranslations)
{
if(subtranslation.Contains("<div class=\"dct-ec\">"))
{
abbr_str = StringParser.Parse("title=\"Part-of-speech\">", "</span>", subtranslation);
//.........这里部分代码省略.........