| Implementing the Search Functionality |
This page outlines how to implement the actual search functionality and how to output the results in the rich text element using a simple string output.
Start by adding a new class called Search to your project. Implement a public function called DoConcordanceSearch, which takes the search text as string parameter and a boolean parameter that indicates whether the search should be done in the target index. In addition, we pass the SelectedIndex property of the comboLanguagePairs control as another parameter. The index of this list corresponds to the index of the server TM language pair. The function returns the search result as a string.
The function is called by clicking the search button on the main lookup form, which then fills the rich text control with the search result:
private void btnSearch_Click(object sender, EventArgs e) { try { #region "SearchObject" Search search = new Search(); #endregion #region "SourceOrTarget" // Determine whether to do the concordance search in the // source or in the target language; bool searchTarget; if (this.comboIndex.Text == "Target") searchTarget = true; else searchTarget = false; #endregion #region "FillHitlist" // Fill the search result into the rich text box. this.lblHitCount.Text = search.DoConcordanceSearch(this.txtSearch.Text, searchTarget, comboLanguagePairs.SelectedIndex); #endregion } catch(Exception ex) { MessageBox.Show("No TM selected." + ex.Message); } }
/// Do the search in either a file- or server-based TM. SearchResults results; if (Connector.server) { results = Connector.serverTM.LanguageDirections[langDirIndex].SearchText(GetSearchSettings(target), searchText); } else { results = Connector.fileTm.LanguageDirection.SearchText(GetSearchSettings(target), searchText); }
/// <summary> /// Configure the settings that should be applied for the search, i.e. /// minimum match value, maximum hit count, and whether the concordance /// search should be done in the source or in the target language. /// </summary> private SearchSettings GetSearchSettings(bool target) { SearchSettings settings = new SearchSettings(); settings.MaxResults = frmSettings.maxHits; settings.MinScore = frmSettings.minFuzzy; if (target) settings.Mode = SearchMode.TargetConcordanceSearch; else settings.Mode = SearchMode.ConcordanceSearch; return settings; }
/// Build up the string that holds the hitlist result. string hitlist; hitlist = "Hit count: " + results.Count.ToString() + "\n\n";
foreach (SearchResult result in results) { hitlist += GetTuInformation(result); }
/// <summary> /// This function returns further information on the given translation unit (TU). /// </summary> private string GetTuInformation(SearchResult tuResult) { string tuInfo; #region "score" /// The matching score tuInfo = "\nScore: " + tuResult.ScoringResult.Match.ToString() + "%\n"; #endregion #region "segments" /// The source and target segments tuInfo += "Source: " + tuResult.MemoryTranslationUnit.SourceSegment.ToPlain() + "\n"; tuInfo += "Target: " + tuResult.MemoryTranslationUnit.TargetSegment.ToPlain() + "\n"; #endregion #region "date" /// The TU creation date. tuInfo += "Creation date: " + tuResult.MemoryTranslationUnit.SystemFields.CreationDate + "\n"; #endregion #region "fields" /// Any field values (e.g. Customer, Project id, etc. associated with the /// given TU. foreach (FieldValue field in tuResult.MemoryTranslationUnit.FieldValues) { tuInfo += field.Name + ": " + field.ToString(); } tuInfo += "\n"; #endregion return tuInfo; }
return hitlist;Add the following private function called GetSearchSettings, which configures the search settings according to the settings configured on the search settings form (see Adding the Search Settings Form).
/// <summary> /// Configure the settings that should be applied for the search, i.e. /// minimum match value, maximum hit count, and whether the concordance /// search should be done in the source or in the target language. /// </summary> private SearchSettings GetSearchSettings(bool target) { SearchSettings settings = new SearchSettings(); settings.MaxResults = frmSettings.maxHits; settings.MinScore = frmSettings.minFuzzy; if (target) settings.Mode = SearchMode.TargetConcordanceSearch; else settings.Mode = SearchMode.ConcordanceSearch; return settings; }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using Sdl.LanguagePlatform.TranslationMemoryApi; using Sdl.LanguagePlatform.TranslationMemory; using Sdl.LanguagePlatform.Core.Tokenization; using Sdl.LanguagePlatform.Core; namespace Sdl.SDK.LanguagePlatform.Samples.TmLookup { class Search { #region "search" /// <summary> /// This function performs the actual concordance search operation. /// </summary> public string DoConcordanceSearch(string searchText, bool target, int langDirIndex) { #region "execute" /// Do the search in either a file- or server-based TM. SearchResults results; if (Connector.server) { results = Connector.serverTM.LanguageDirections[langDirIndex].SearchText(GetSearchSettings(target), searchText); } else { results = Connector.fileTm.LanguageDirection.SearchText(GetSearchSettings(target), searchText); } #endregion #region "StartHitlist" /// Build up the string that holds the hitlist result. string hitlist; hitlist = "Hit count: " + results.Count.ToString() + "\n\n"; #endregion #region "result" foreach (SearchResult result in results) { hitlist += GetTuInformation(result); } #endregion #region "CloseHitlist" return hitlist; #endregion } #endregion #region "TuInfo" /// <summary> /// This function returns further information on the given translation unit (TU). /// </summary> private string GetTuInformation(SearchResult tuResult) { string tuInfo; #region "score" /// The matching score tuInfo = "\nScore: " + tuResult.ScoringResult.Match.ToString() + "%\n"; #endregion #region "segments" /// The source and target segments tuInfo += "Source: " + tuResult.MemoryTranslationUnit.SourceSegment.ToPlain() + "\n"; tuInfo += "Target: " + tuResult.MemoryTranslationUnit.TargetSegment.ToPlain() + "\n"; #endregion #region "date" /// The TU creation date. tuInfo += "Creation date: " + tuResult.MemoryTranslationUnit.SystemFields.CreationDate + "\n"; #endregion #region "fields" /// Any field values (e.g. Customer, Project id, etc. associated with the /// given TU. foreach (FieldValue field in tuResult.MemoryTranslationUnit.FieldValues) { tuInfo += field.Name + ": " + field.ToString(); } tuInfo += "\n"; #endregion return tuInfo; } #endregion #region "settings" /// <summary> /// Configure the settings that should be applied for the search, i.e. /// minimum match value, maximum hit count, and whether the concordance /// search should be done in the source or in the target language. /// </summary> private SearchSettings GetSearchSettings(bool target) { SearchSettings settings = new SearchSettings(); settings.MaxResults = frmSettings.maxHits; settings.MinScore = frmSettings.minFuzzy; if (target) settings.Mode = SearchMode.TargetConcordanceSearch; else settings.Mode = SearchMode.ConcordanceSearch; return settings; } #endregion } }