public static List <searchResult> search(string query) { List <SearchEngines> searchEngines = SingletonEngines.Instance.searchEngines; List <searchResult> results = new List <searchResult>(); List <searchResult> winners = new List <searchResult>(); List <string> queries = getQueries(query); foreach (var engine in searchEngines) { results = new List <searchResult>(); foreach (var item in queries) { searchResult result = searchInEngine(engine, item.ToString()); results.Add(result); } var winnerByEngine = results.OrderByDescending(x => x.resultsCount).FirstOrDefault(); winnerByEngine.engine = winnerByEngine.engine + " winner"; winners.Add(winnerByEngine); } searchResult winner = winners.OrderByDescending(x => x.resultsCount).FirstOrDefault(); searchResult winnerAdd = winner.Clone() as searchResult; winnerAdd.engine = "Total winner"; winners.Add(winnerAdd); return(winners); }
public async Task search_ProcessAsync(int seqid, TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken) { var args = new searchArgs(); await args.ReadAsync(iprot, cancellationToken); await iprot.ReadMessageEndAsync(cancellationToken); var result = new searchResult(); try { result.Success = await _iAsync.searchAsync(args.Word, cancellationToken); await oprot.WriteMessageBeginAsync(new TMessage("search", TMessageType.Reply, seqid), cancellationToken); await result.WriteAsync(oprot, cancellationToken); } catch (TTransportException) { throw; } catch (Exception ex) { Console.Error.WriteLine("Error occurred in processor:"); Console.Error.WriteLine(ex.ToString()); var x = new TApplicationException(TApplicationException.ExceptionType.InternalError, " Internal error."); await oprot.WriteMessageBeginAsync(new TMessage("search", TMessageType.Exception, seqid), cancellationToken); await x.WriteAsync(oprot, cancellationToken); } await oprot.WriteMessageEndAsync(cancellationToken); await oprot.Transport.FlushAsync(cancellationToken); }
private void SearchButton_Click(object sender, RoutedEventArgs e) { string query = this.SearchTextBox.Text; List <Topic> ansList = new List <Topic>(); try { foreach (var subForum in user.SubForumsList) { List <Topic> topicList = vm.getTopics(subForum); foreach (var topic in topicList) { if (topic.subject.Contains(query) || topic.content.Contains(query)) { ansList.Add(topic); } } } } catch (Exception exception) { Console.WriteLine(exception); throw; } if (ansList.Count == 0) { MessageBox.Show("The search find nothing"); } else { searchResult sr = new searchResult(); foreach (var topic in ansList) { Discussion d = new Discussion(); d.m_content = topic.content; d.m_subject = topic.subject; d.nameLabel.Content = topic.userFirstOpenMsgName; d.SubjectLabel.Content = topic.subject; sr.StackPanel.Children.Add(d); } sr.Show(); } }
public async Task <string> searchAsync(string word, CancellationToken cancellationToken = default(CancellationToken)) { await OutputProtocol.WriteMessageBeginAsync(new TMessage("search", TMessageType.Call, SeqId), cancellationToken); var args = new searchArgs(); args.Word = word; await args.WriteAsync(OutputProtocol, cancellationToken); await OutputProtocol.WriteMessageEndAsync(cancellationToken); await OutputProtocol.Transport.FlushAsync(cancellationToken); var msg = await InputProtocol.ReadMessageBeginAsync(cancellationToken); if (msg.Type == TMessageType.Exception) { var x = await TApplicationException.ReadAsync(InputProtocol, cancellationToken); await InputProtocol.ReadMessageEndAsync(cancellationToken); throw x; } var result = new searchResult(); await result.ReadAsync(InputProtocol, cancellationToken); await InputProtocol.ReadMessageEndAsync(cancellationToken); if (result.__isset.success) { return(result.Success); } throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, "search failed: unknown result"); }
private static searchResult searchInEngine(SearchEngines engine, string query) { searchResult respResult = null; System.IO.Stream newStream = null; System.Net.WebResponse response = null; System.IO.StreamReader reader = null; try { /* * An example to call to the search engine directly * * string url = String.Format("{0}?q={1}", engine.url, query); * * System.Net.WebRequest wr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); * * wr.Method = "GET"; * wr.ContentType = "application/x-www-form-urlencoded"; * * response = wr.GetResponse(); * newStream = response.GetResponseStream(); * * reader = new System.IO.StreamReader(newStream); * string responseFromServer = reader.ReadToEnd(); */ /*This is for test purpose*/ respResult = new searchResult(); respResult.engine = engine.name; switch (engine.name) { case "google": respResult.resultsCount = RandomNumberBetween(1000, 5000); respResult.search = query; break; case "yahoo": respResult.resultsCount = RandomNumberBetween(1000, 5000); respResult.search = query; break; case "bing": respResult.resultsCount = RandomNumberBetween(1000, 5000); respResult.search = query; break; default: respResult.resultsCount = 0; respResult.search = query; break; } } catch (Exception ex) { //Log error } finally { if (reader != null) { reader.Close(); } if (newStream != null) { newStream.Close(); } if (response != null) { response.Close(); } } return(respResult); }
IEnumerator soundHelper(searchResult sr,bool download) { //first check to make sure it wasnt already downloaded/cached if (!cacheSounds.ContainsKey (sr.id)) { Dictionary <string,string> headers = new Dictionary<string, string> (); headers.Add ("Authorization", "Bearer " + gv.accessToken); string wholeUrl = "https://www.freesound.org/apiv2/sounds/" + sr.id + "/download/"; WWW www = new WWW (wholeUrl, null, headers); Debug.Log ("Loading " + www.url); //yield return www; while (!www.isDone) { progressTrack.text = "downloaded " + (www.progress * 100).ToString () + "%..."; yield return null; } progressTrack.text = ""; if (www.error == null) Debug.Log ("done"); else Debug.Log (www.error); //AudioClip loadedClip = www.GetAudioClip(false,false,AudioType.WAV); playSource.clip = www.GetAudioClipCompressed (false, AudioType.WAV); playSource.clip.name = sr.name;//will change to name at some point cacheSounds.Add (sr.id, playSource.clip); //create a directory if there isnt one if (!System.IO.Directory.Exists (Application.persistentDataPath + "/Downloads"))//change to persistent? System.IO.Directory.CreateDirectory (Application.persistentDataPath + "/Downloads");//change to persistent string fullPath = Application.persistentDataPath + "/Downloads/" + playSource.clip.name + ".wav";//just testing, need to change to persistent File.WriteAllBytes (fullPath, www.bytes); //delete when navigated away from this page if (!download) { playSource.Play (); deleteList.Add (playSource.clip.name); } else { gv.gameObject.GetComponent<soundBank> ().addedSounds.Add (playSource.clip.name + ".wav"); } } //sound has been cached else { AudioClip tempClip; cacheSounds.TryGetValue (sr.name, out tempClip); playSource.clip = tempClip; playSource.Play (); } //maybe some sort of cache system, to avoid downloading again? //check to make sure it has not already been downloaded }