我想实现一个英文单词的拼写检查,在网上搜罗了一下,几乎都是调用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分吧)
前提是必须安装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分吧)
解决方案 »
- 求助:关于DataSet
- 确保列表中的最大索引小于列表的大小,很奇怪的问题,望高手解答。
- 请教:datagrid上绑定了一列删除按钮,如何点击某个按钮删除当前行,并且在删除前提示用户是否删除?
- 高手求救,SOCKET
- 难搞的问题。。。。。求救
- 很偏门的问题,特来高手多多的c#版求教:如何动态给PropertyGrid1加载属性
- 急需 水晶报表怎么用
- 提供打包CSDN文档中心所有文档及CSDN论坛2003年数据!!
- winform 隐藏Panel 显示多个button 当其中button离开时 panel显示
- 关于如何使用ini的问题
- SqlParameter 的问题
- 访问数据库如何给表加锁?解锁?
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
这样的形式。
对于正文中的每个单词,都在这棵树上查找一次,找不到就是拼写错误。当然,也可以使用第三方专门的轻量的拼写检查组件。
大家可以考虑从这个office的类库入手,找下解决办法。
--b
----o
------u
--------t
------v
--------e
--l
----o
------n
--------g
--s
----k
----p
b
--o
----d
------y
----y
组织词典也可以用现有的来转换。
如果实在不想自己做,第三方这样的库或者控件很多的,没有必要非得Office,而且Office的授权好像不允许单独分发,也就是目标电脑必须装Office才能用你的程序了。