Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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#">
Posted
Comments
BillWoodruff 3-Nov-14 2:52am    
It would be helpful if you change the name of the Controls in your code so they reflect what you are doing. Which RichTextBox holds the English text, which holds the translated Arabic text ? Which RichTextBox holds the Text to search for ? Is the text to search for entered in English, or Arabic: or are you searching using a highlighted word in the English or Arabic RichTextBoxes ?

It's just too much work to try and figure out code like this.
Me is Needer 3-Nov-14 2:56am    
richtextbox2: for English words
richtextbox3: for Arabic words
textbox1: for searched words

in search textbox : you can enter the English word to search
BillWoodruff 3-Nov-14 3:07am    
Okay, that makes your code clearer, thanks. Let me ask you about the translation process: does the user enter text in English, and then click a button, or choose a menu item, to trigger translation into Arabic, or is each word in English entered in translated immediately to Arabic ?

Another way to ask the same thing is to say: "can the user go in and make edits in either the English, or the Arabic, without triggering automatic re-translations ?"

The answer to that question could be critical to deciding on a strategy here.
Me is Needer 3-Nov-14 4:14am    
the translation process is done when the user press other button is called translate.

if the user modify any english word , he must press translate button again; if the modified word is found then the meaning will be typed but if is not the program will type ***


i hope these ingormation make my question above clear , beacause i need help , please . :)


thank you in advance
BillWoodruff 3-Nov-14 4:37am    
Okay, I think I get the picture now, and will post some code I hope will be helpful.

1 solution

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.
C#
// 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. Code: ... note this is an outline of code, not complete working code
C#
// 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
    }
}
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," ?
 
Share this answer
 
v4
Comments
Me is Needer 3-Nov-14 6:10am    
the lines of dictionaries above where can i put it ???
really i could not understand your code :( , my problem i thought it is easy just to highlighted the new word searched and make the last not highlight !
BillWoodruff 3-Nov-14 7:09am    
The code I show here is based on the idea you want to make searches really fast by pre-computing exactly what needs to be highlighted for each search word. I also assume you want to highlight every occurrence of the search word in the text ... if there are multiple occurrences.

Don't worry if you can't use this code: it was fun for me to write as an exercise :)
Me is Needer 3-Nov-14 7:47am    
thanks for you , look i write your code in new project ti check it , i appear alot of errors specially in Dictionary lines !!

// dictionary mapping English word to Arabic translation
=Dictionary<string,> dctEnglishToArabic = new Dictionary<string,>();

// mapping English word to Locations in rtbEnglish
Dictionary<string,>> dctEnglishWordToLocations = new Dictionary<string,>>();

// mapping Arabic translation to Location in rtbArabic
Dictionary<string,>> dctArabicWordToLocations = new Dictionary<string,>>();


what can i do Teacher ?
BillWoodruff 3-Nov-14 10:15am    
Unfortunately, working code pasted into the CP editor often gets formatting errors when angle-brackets <> are involved.

I've corrected the Dictionary declarations.

Is it correct that you only want the matches of the English word(s) in the search box to be highlighted in the Arabic RichTextBox ?

cheers, Bill
Me is Needer 4-Nov-14 2:11am    
yes , i want when i press search button second time with new word , then the last highlighted words be without highlight color.

this is what i want :) !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900