在c#中突出显示搜索的单词及其含义 [英] Highlighting searched word and its meaning in c#

查看:116
本文介绍了在c#中突出显示搜索的单词及其含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i完成了我的英语语音识别项目,我制作了翻译按钮,将第一个richtextbox中的每个单词翻译成阿拉伯语secong richtextbox中的语言。



i添加了搜索按钮,突出显示搜索的单词并突出显示其含义。







我的问题是:当我再次按下搜索按钮时,突出显示的英文单词更新,但之前突出显示的阿拉伯语单词仍然是新的。





如何在每个按下搜索按钮时突出显示独立突出显示??????









这是我的代码:



Hello every one ,

i have done my project which is in speech recognition in English language, and i make translate button which translate each word in first richtextbox to Arabic language in secong richtextbox.

i have added search button which make highlighting on the word searched and highlighting its meaning also .



my problem is : when i press the search button again , the english words updated in highlighting but the Arabic words which highlighted before is still to the new .


how can i make the highlighting independent highlighting in each i press search button ??????




this is my code :

private void button9_Click(object sender, EventArgs e)
        {
            if (richTextBox2.Text != "" && textBox1.Text != "")
            {
                int ct = 0;
                string temp = richTextBox2.Text;
                richTextBox2.Text = "";
                richTextBox2.Text = temp;
                while (ct < richTextBox2.Text.LastIndexOf(textBox1.Text))
                {
richTextBox2.Find(textBox1.Text, ct, richTextBox2.TextLength, RichTextBoxFinds.None);
richTextBox2.SelectionBackColor = Color.Yellow;
  ct = richTextBox2.Text.IndexOf(textBox1.Text, ct) + 1;
                }

                ct = 0;
                string constring = "Data Source=.;Initial Catalog=speakerpro;Integrated Security=True";
                string query = "select mean from Translator1 where word = '" + textBox1.Text + "'";
                SqlConnection con = new SqlConnection(constring);
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                DataSet ds = new DataSet();
                da.Fill(ds, "mean");
                if (ds.Tables["mean"].Rows.Count != 0)
                {
string smean = ds.Tables["mean"].Rows[0]["mean"].ToString();
    while (ct < richTextBox3.Text.LastIndexOf(smean))
                    {
 richTextBox3.Find(smean, ct, richTextBox3.TextLength, RichTextBoxFinds.None);
 richTextBox3.SelectionBackColor = Color.Yellow;

   ct = richTextBox3.Text.IndexOf(smean, ct) + 1;

                    }

                }
                else
                {
                    MessageBox.Show("There is no meaning for this word!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            else
            {
                MessageBox.Show("There is no word to highlight! ","Message", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }       
        }


 <pre lang="c#">

推荐答案

假设:一个WinForms项目,其中:



1.控件



按钮: btnTranslate:点击翻译英语到阿拉伯语:使用rtbEnglish,rtbArabic

按钮:btnSearchButton:点击搜索英文单词:使用tbSearchEnglish



RichTextBox:rtbEnglish:显示用户输入的英文文本

RichTextBox:rtbArabic:以rtbEnglish显示英文文本翻译成阿拉伯语

TextBox:tbSearchEnglish:显示用户输入的英文文本,用来搜索



2.运行时行为:翻译



a。单击btnTranslate时:检查rtbEnglish中的文本是否有效条目,如果有效:



b。被处理成数组或通用的字符串列表;并且:



c。清除rtbArabic RichTextBox,并且:执行数据库中每个英语单词的逐字查找,返回相应的阿拉伯单词,然后将其附加到rtbArabic RichTextBox文本。



3.运行时行为:搜索



a。 tbSearch被解析为一个有效的条目(它可以有多个单词?),如果有效:



b。突出显示rtbEnglish中任何单词的出现,并突出显示rtbArabic中相应的单词。



4.运行时行为:用户编辑rtbEnglish



a。 rtbArabic被清除,rtbEnglish中的任何亮点都被删除了吗?



5.策略:



a。由于在没有重新翻译的情况下不允许进行运行时编辑,因此,当逐个单词执行翻译时,您可以知道每个英语单词的位置(开始和结束),并且您可以知道每个单词的位置插入阿拉伯语单词/短语。



b。我们可以使用词典:一个用于将英语单词映射到阿拉伯语翻译,两个用于类型< string,List< int>>在rtbEnglish和rtbArabic RichTextBoxes中保存每个英文单词映射到所有的出现。
Assume: a WinForms project where:

1. Controls

Button: btnTranslate: click to translate English to Arabic: uses rtbEnglish, rtbArabic
Button: btnSearchButton: click to search for an English word: uses tbSearchEnglish

RichTextBox: rtbEnglish: displays user-entered English text
RichTextBox: rtbArabic: displays English text in rtbEnglish translated into Arabic
TextBox: tbSearchEnglish: displays user-entered English text, to used to search

2. Run-time behavior: Translate

a. When the btnTranslate is clicked: text in rtbEnglish is checked for a valid entry, and if valid:

b. is processed into an Array or generic List of strings; and:

c. the rtbArabic RichTextBox is cleared, and: a word-by-word look-up of each English word in the database is performed which returns a corresponding Arabic word(s) which is then appended to the rtbArabic RichTextBox Text.

3. Run-time behavior: Search

a. tbSearch is parsed for a valid entry (can it have more than one word ?), and, if valid:

b. occurrence of any of the word(s) in the rtbEnglish is highlighted, and their corresponding words in rtbArabic are highlighted.

4. Run-time behavior: user edits rtbEnglish

a. rtbArabic is cleared, any highlights in rtbEnglish are removed ?

5. Strategy:

a. since no run-time editing is allowed without re-translation, then, when the translation is performed word-by-word, you can know the position (start, and end) of each English word, and you can know the position of each Arabic word/phrase inserted.

b. we can use Dictionaries: one to map English words to the Arabic translation, and two of type <string, List<int>> to hold a "mapping" of each English word to all its occurrences in both rtbEnglish, and rtbArabic RichTextBoxes.
// dictionary mapping English word to Arabic translation
Dictionary<string, string> dctEnglishToArabic = new Dictionary<string, string>();
    
// dictionaries mapping word to Locations
Dictionary<string, List<int>> dctEnglishWordToLocations = new Dictionary<string, List<int>>();

Dictionary<string, List<int>> dctArabicWordToLocations = new Dictionary<string, List<int>>();

6。代码:...注意这是代码大纲,不是完整的工作代码

6. Code: ... note this is an outline of code, not complete working code

// assume you have a function like: private string YourDataBaseLookUp(string englishWord);
// that takes an English word as a parameter, and returns a string which
// contains the Arabic translation

// characters to ignore in the English text
private char[] charsToSplit = new char[] {' ', '\t', '\n'};     

// translate
private void btnTranslate_Click(object sender, EventArgs e)
{
    // get ready
    rtbArabic.Clear();
    dctEnglishToArabic.Clear(); 
    dctEnglishWordToLocations.Clear();
    dctArabicWordToLocations.Clear();

    // create the list of English words
    List<string> englishWords = rtbEnglish.Text.Split(charsToSplit, StringSplitOptions.RemoveEmptyEntries).ToList();

    // keep track of where we are in content
    int englishNdx = 0;
    int arabicNdx = 0;

    // build the Dictionary that maps words to locations

    string theArabic = "";

    foreach (string s in englishWords)
    {
        // repeat of word ? we already have a translation
        if (dctEnglishWordToLocations.Contains(s))
        {
            // what do you need to do here
            // to add the new start location
            // to the location dictionaries ?
            // and update the indexes ?
        }
        else
        {
            // length of the English word
            int eLen = s.Length;

            // new word
            theArabic = YourDataBaseLookUp(s);

            // length of the Arabic translation
            int aLen = theArabic.Length;

            dctEnglishToArabic.Add(s, theArabic);

            // what do you need to do to calculate the location
            // using the index variables and length of the strings ?
            // what do you need to do to update the index variables ?

            // you initialize the location lists in each dictionary here
            dctEnglishWordToLocations.Add(s, new List<int>());
            dctArabicWordToLocations.Add(s, new List<int>());

            // then: what do you need to do to insert the location
            // of the new word into the location dictionaries ?
        }
    }
}
// search
private void btnSearch_Click(object sender, EventArgs e)
{
    // get a list of the words in tbSearch
    // List<string> searchWords = ????;</string>
    
    // go through the list
    foreach(string wrd in searchWords
    {
       List<int> eLocations = dctEnglishWordToLocations[wrd];
       List<int> aLocations = dctArabicWordToLocations[wrd];

       // now use both lists, and perform the high-lighting in both
       // both rtbEnglish, and rtbArabic
    }
}

注意:未解决的一个大问题是你想要通过考虑它们发生的确切位置来获取英文文本中的单词位置(rtbEnglish中包括前面的空格);我不熟悉用阿拉伯语标点符号,不知道你将面临什么问题。



注意:这个大纲已经很快完成了;因为我有时间,我会修改它。



注意:你需要处理的问题是你有这样的英文文本:



每天吃一个苹果,会让医生远离我,但是,吃太多苹果,比如金苹果,可能会让你生病。



你怎么处理翻译苹果苹果苹果?

Note: A big problem not addressed here is that you will want to get the positions of the words in the English text by considering exactly where they occur (and that includes the white-space before them) in the rtbEnglish; I am not familiar with punctuation in Arabic, and do not know what issues you will face there.

Note: this "outline" has been done quickly; I'll revise it as I have time.

Note: A problem you will have to deal with here is when you have English text like this:

"An apple a day, will keep the doctor away, but, eating too many apples, like, for example, golden delicious apples, could make you sick."

What do you do to handle translating "apple" "apples" "apples," ?


这篇关于在c#中突出显示搜索的单词及其含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆