我想实现一个英文单词的拼写检查,在网上搜罗了一下,几乎都是调用office做拼写检查
前提是必须安装office(很不方便吧? 我也觉得)
然后引用类库:Microsoft.Office.Interop.Word.dll
以下是代码:
using Word = Microsoft.Office.Interop.Word;
public static bool SourceStringSpellingCheck(LocResource lr,ref string message)
        {
            try
            {
                string[] wordList = lr.SourceString.Value.Split(LCXOMToolbox.charArray, StringSplitOptions.RemoveEmptyEntries);
                object missing = System.Reflection.Missing.Value;
                Word._Application app = null;
                try
                {
                    app = new Word.Application();
                }
                catch
                {
                    Console.WriteLine("The Word Application can not be initialized, make sure you have MS office Word installed");
                    return false;
                }                int errors = 0;
                bool result = false;                app.Visible = false;                // Setting these variables is comparable to passing null to the function.
                // This is necessary because the C# null cannot be passed by reference.
                object template = missing;
                object newTemplate = missing;
                object documentType = missing;
                object visible = true;                //Setting these variables is comparable to passing null to the function.
                //Close and Quit method.
                object saveChanges = false;
                object originalFormat = missing;
                object routeDocument = missing;
                                foreach (string word in wordList)
                {
                    if (word.Length > 1)
                    {
                        Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
                        doc1.Words.First.InsertBefore(word);
                        Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors;
                        errors = spellErrorsColl.Count;                        if (errors > 0)
                        {
                            message = string.Format("{0}{1};", message, word);
                            errors = 0;
                            result = true;
                        }
                        //Close document
                        doc1.Close(ref saveChanges, ref originalFormat, ref routeDocument);
                    }
                }
                //Quit application
                app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Some exception occurs:{0}", ex.Message);
                return false;
            }
        }
但是这样做效率太慢, 大家能有更好的方法么?或者改进方法。
谢谢。
(ps:分不多,暂时使用100分吧)

解决方案 »

  1.   

    说下算法:自己写个词典。然后使用搜索树组织这个词典,形成拼写检查树,类似如下格式比如词典中有:
    above about ask asp along body boy
    那么就形成:
    a
     b
      o
       u
        t
       v
        e
     l
      o
       n
        g
     s
      k
      p
    b
     o
      d
       y
      y
    这样的形式。
    对于正文中的每个单词,都在这棵树上查找一次,找不到就是拼写错误。当然,也可以使用第三方专门的轻量的拼写检查组件。
      

  2.   

    你这个方法是可以,但是短期内我要建立好这个树的结构都困难,搜集信息这些方面都要考虑。
    大家可以考虑从这个office的类库入手,找下解决办法。
      

  3.   

    a
    --b
    ----o
    ------u
    --------t
    ------v
    --------e
    --l
    ----o
    ------n
    --------g
    --s
    ----k
    ----p
    b
    --o
    ----d
    ------y
    ----y
      

  4.   

    以前写过拼写检查代码的,只要一天的功夫就可以写得很好。
    组织词典也可以用现有的来转换。
    如果实在不想自己做,第三方这样的库或者控件很多的,没有必要非得Office,而且Office的授权好像不允许单独分发,也就是目标电脑必须装Office才能用你的程序了。
      

  5.   

    Check this out:http://sourceforge.net/projects/netspell/
      

  6.   

    刚在CSDN下载频道中上传了一个合法的英语单词参考包,比较全了,有兴趣的可以参考下。http://download.csdn.net/source/1606694
      

  7.   

    更正:http://download.csdn.net/source/1608467
      

  8.   

    不可能有abp啊。b下面只有o,不可以在同一层移动。