SynonymInfo[] For Microsoft.Office.Interop.Word in C#

880 views Asked by At

In C# i using

using word = Microsoft.Office.Interop.Word;

For get a Synonym for words by using this code

 var app = new word.Application();
 var infosyn = app.SynonymInfo[Wtext[Op + 1].ToString(), word.WdLanguageID.wdArabic];
 foreach (var item in infosyn.MeaningList as Array)
     {

               listBox1.Items.Add(item.ToString());

     }

Image here

My Issue is i got only the meaning list ( What is red boxes in image), but i want all words like in the image ( words in red boxes and blue arrows, The whole list). Note: i use Meaninglist, RelatedWordList and it's not working and make loops in loop take a each synonym words and check their synonyms. Like This

var apps = new words.Application();
var infosyns = apps.SynonymInfo[item.ToString(), words.WdLanguageID.wdArabic] ;
foreach (var iitem in infosyns.MeaningList as Array)
{

listBox1.Items.Add(iitem.ToString());

var appss = new wordss.Application();
var infosynss = appss.SynonymInfo[iitem.ToString(),wordss.WdLanguageID.wdArabic] ;
foreach (var iiitem in infosyns.MeaningList as Array)
{

listBox1.Items.Add(iiitem.ToString());

}

}

Image here

2

There are 2 answers

0
Oli4 On

I had a similar problem, however by looping a predefined amount of times through the results, I got a lot more. This was just a quick code and I believe the efficiency could be improved, However I believe it will get you on the right track. (It is somewhat similar to what you have done.

private static string[] getAllMeanings(Application wordApp, string word, int maxSize = 12,bool addOriginal = false)
    {
        List<string> stringArr = new List<string>();
        if (addOriginal)stringArr.Add(word);
        SynonymInfo theSynonyms = wordApp.SynonymInfo[word];
        foreach (var Meaning in theSynonyms.MeaningList as Array)
        {
            if (stringArr.Contains(Meaning) == false) stringArr.Add((string)Meaning);
        }
        for (int ii = 0; ii < stringArr.Count; ii++)
        {
            theSynonyms = wordApp.SynonymInfo[stringArr[ii]];
            foreach (string Meaning in theSynonyms.MeaningList as Array)
            {
                if (stringArr.Contains(Meaning)) continue;
                stringArr.Add(Meaning);
            }
            if (stringArr.Count >= maxSize) return stringArr.ToArray();
        }
        return stringArr.ToArray();
    }

Basically this function gets all the related words and then recursively adds the related word and finds their related words. This ends up by very closely resembling your result if you set the maxSize to 15;

  • Note: maxSize is to stop the function, for instance the word "have" has at least 3600 'synonyms'. And optimally your result should remain relevant.
0
Louwrens Potgieter On

Oli4 is correct. You need to loop through the underlying data by doing something like this:

foreach (var iiitem in infosyns.MeaningList as Array)
            {

                listBox1.Items.Add(iiitem.ToString());

                foreach (var item in iiitem.MeaningList)
                {
                    listBox1.Items.Add(item.ToString());
                }

            }