OSDN Git Service

beaf24ad49e4d8e76adaaec1d2c68b5f485fd6e1
[passer/Passer2.git] / macro.bing / BingTranslationMacro.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Microsoft.VisualBasic;
5 using System.Globalization;
6 using Lugens.Passer.Macro.Bing.net.bing.api;
7
8 namespace Lugens.Passer.Macro.Bing
9 {
10     public class TranslationMacro : IMacro
11     {
12         const string AppId = "A01584CB8A078CEF40EF61C013A6EC87F144870E";
13
14         event MacroEventHandler macroCompletedEvent;
15
16         event MacroEventHandler IMacro.OnMacroCompleted
17         {
18             add { macroCompletedEvent += value; }
19             remove { macroCompletedEvent -= value; }
20         }
21          
22         public string Id()
23         {
24             return "Lugens.Passer.Macro.Bing.PopupTranslation";
25         }
26
27         public MacroType Type()
28         {
29             return MacroType.InOutPopup;
30         }
31
32         public bool Init()
33         {
34             return true;
35         }
36
37         public string Name()
38         {
39             return "PopupBingTranslation";
40         }
41
42         public string Summary()
43         {
44             return "翻訳します";
45         }
46
47         public string Description()
48         {
49             return new StringBuilder()
50                 .Append("引数:0~1個").Append(Environment.NewLine)
51                 .ToString();
52         }
53
54         public string Execute(string str, string[] args)
55         {
56             return null;
57         }
58
59
60         public void ExecuteAsync(string str, string[] args)
61         {
62             using (BingService service = new BingService())
63             {
64                 try
65                 {
66                     SearchRequest request = BuildRequest(str);
67
68                     // Send the request; display the response.
69                     service.SearchCompleted += OnSearchCompleted;
70                     service.SearchAsync(request);
71                     //SearchResponse response = service.Search(request);
72                     //s = DisplayResponse(response);
73                 }
74                 catch (System.Web.Services.Protocols.SoapException)
75                 {
76                     // A SOAP Exception was thrown. Display error details.
77                 }
78                 catch (System.Net.WebException)
79                 {
80                     // An exception occurred while accessing the network.
81                 }
82             }
83         }
84
85         static SearchRequest BuildRequest(string str)
86         {
87             SearchRequest request = new SearchRequest();
88
89             // Common request fields (required)
90             request.AppId = AppId;
91             request.Query = str;
92             request.Sources = new SourceType[] { SourceType.Translation };
93
94             // SourceType-specific fields (required)
95             request.Translation = new TranslationRequest();
96             request.Translation.SourceLanguage = "en";
97             request.Translation.TargetLanguage = "ja";
98
99             // Common request fields (optional)
100             request.Version = "2.2";
101
102             return request;
103         }
104
105         static string DisplayResponse(SearchResponse response)
106         {
107             StringBuilder sb = new StringBuilder();
108             // Display the results header.
109             Console.WriteLine("Bing API Version " + response.Version);
110             Console.WriteLine("Translation results for " + response.Query.SearchTerms);
111             Console.WriteLine();
112
113             // Display the Translation results.
114             foreach (TranslationResult result in response.Translation.Results)
115             {
116                 sb.Append(result.TranslatedTerm);
117             }
118             return sb.ToString();
119         }
120
121         public bool Test()
122         {
123             using (BingService service = new BingService())
124             {
125                 try
126                 {
127                     SearchRequest request = new SearchRequest();
128                     request.AppId = AppId;
129                     request.Query = "test";
130                     request.Sources = new SourceType[] { SourceType.Translation };
131                     request.Translation = new TranslationRequest();
132                     request.Translation.SourceLanguage = "en";
133                     request.Translation.TargetLanguage = "ja";
134                     request.Version = "2.2";
135                     service.Search(request);
136                 }
137                 catch (System.Web.Services.Protocols.SoapException)
138                 {
139                     return false;
140                 }
141                 catch (System.Net.WebException)
142                 {
143                     return false;
144                 }
145             }
146             return true;
147         }
148
149         public void OnSearchCompleted(object sender, SearchCompletedEventArgs e)
150         {
151             string s = DisplayResponse(e.Result);
152             if (macroCompletedEvent != null)
153                 macroCompletedEvent(s);
154
155         }
156
157     }
158
159 }