在C#中使用microsoftword查找同义词 [英] Find synonyms using microsoftword in C#

查看:154
本文介绍了在C#中使用microsoftword查找同义词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using WordApplication = Microsoft.Office.Interop.Word.Application;
using Microsoft.Office.Interop.Outlook;
namespace synonyms_try1
{
    public partial class Form1 : Form
    {
        Class1 c=new Class1();
        public Form1()
        {
            InitializeComponent();
        }

        private void bSubmit_Click(object sender, EventArgs e)
        {
              Microsoft.Office.Interop.Word.Application appWord;      // word application var
      object objNull = null;      // word object method calls require
                        // references to objects... create
                        // object for null and
      object objFalse = false;      // false entries and language

      object objLanguage = Microsoft.Office.Interop.Word.WdLanguageID.wdEnglishUS; // or appropritate lang!

      try
      {
            // Try opening Word app
            appWord = new Microsoft.Office.Interop.Word.Application();
      }
      catch(System.Exception exc)
      {
            // could not open word... show error message and return
            MessageBox.Show(exc.Message);
            return;
      }

      // clear synonym listbox lbSynonym
      lbSynonym.Items.Clear();

      // now call get_SynonymInfo to get SynonymInfo structure for
      // word entered in TextBox tbWord
      Microsoft.Office.Interop.Word.SynonymInfo si =
               appWord.get_SynonymInfo(tbWord.Text, ref (objLanguage));
        
      
      // first find out how many meanings were found for word
      int iMeanings = (int)si.MeaningCount;
      
      if(iMeanings > 0)
      {
            // one or more meanings were found... loop over each
            // (notice SynonymInfo.MeaningList is type System.ArrayList!)
          
          foreach (string strMeaning in (Array)si.MeaningList)
            {
                  // get Synonym List for each meaning... note that
                  // get_SynonymList takes an object ref, thus we
                  // must create objMeaning object
                  object objMeaning = strMeaning;
                  System.Array aSynonyms =
                        (System.Array)si.get_SynonymList(ref objMeaning);
                  foreach(string strSynonym in aSynonyms)
                  {
                        // loop over each synonym in ArrayList
                        // and add to lbSynonym ListBox
                        lbSynonym.Items.Add(strSynonym);
                  }
            }
      }
      else
      {
            // no meanings/synonyms found... set ListBox value to "NONE"
            lbSynonym.Items.Add("NONE");
      }
      // Clean up COM object
      c.Destroy(si);

      // quit WINWORD app
      appWord.Quit(ref objFalse, ref objNull, ref objNull);
      // clean up COM object
      c.Destroy(appWord);
      
}

        }
    }



im使用此代码查找同义词但会出错


i m using this code for finding synonyms but it give error

Unable to cast object of type 'System.String[*]' to type 'System.String[]'.



该行的错误......


The error on that line......

foreach (string strMeaning in (Array)si.MeaningList)





告诉我你的建议,如果你有..我从这个链接获取此代码jczerk解决方案在此链接中使用..

C#:WordNet / MS Word对象库拼写检查/同义词信息 [ ^ ]



我尝试过:



i我试图使用微软词找到同义词....但它给了错误



Tell me the suggestion if u have.. i take this code from this link jczerk solution use in this link..
C#: WordNet / MS Word Object Library Spellchecker/SynonymInfo[^]

What I have tried:

i am trying to find the synonyms using microsoft word ....but it give the error

推荐答案

尝试更改代码以使用 As Array 并使用 var 用于您的声明。例如。 (我还添加了一些空检查)

Try changing your code to use As Array and use var for your declarations. E.g. (I've also added in some null checking)
if (si.MeaningCount > 0)
{
    // one or more meanings were found... loop over each
    // (notice SynonymInfo.MeaningList is type System.ArrayList!)
    var strMeanings = si.MeaningList as Array;
    if (strMeanings != null)
        foreach (var strMeaning in strMeanings)
        {
            // get Synonym List for each meaning... note that
            // get_SynonymList takes an object ref, thus we
            // must create objMeaning object
            var objMeaning = strMeaning;

            var aSynonyms = si.SynonymList[objMeaning];

            var strSynonyms = si.SynonymList[objMeaning] as Array;
            if (strSynonyms != null)
                foreach (string strSynonym in strSynonyms)
                {
                    // loop over each synonym in ArrayList
                    // and add to lbSynonym ListBox
                    lbSynonym.Items.Add(strSynonym);
                }
        }
}
else
{
    // no meanings/synonyms found... set ListBox value to "NONE"
    lbSynonym.Items.Add("NONE");
}



本文供参考:如何:使用as和is Operators安全地进行投射(C#编程指南) [ ^ ]


这篇关于在C#中使用microsoftword查找同义词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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