[英]How can I parallelize check spelling using Delphi?
I've got a sort of spell checker written in Delphi. 我有一种用Delphi编写的拼写检查器。 It analyzes the text sentence by sentence.
它逐句分析文本。 It encolors wrong items according to some rules after parsing each sentence.
在解析每个句子后,它根据一些规则来包含错误的项目。 The user is able to interrupt this process, which is important.
用户可以中断此过程,这很重要。 How can I parallelize this process in general using some 3rd party Delphi libraries?
如何使用某些第三方Delphi库来一般并行化这个过程? In the current state I've got on the fly sentence coloration after check.
在目前的状态下,我在检查后立即着色。 Thus the user sees the progress.
因此,用户可以看到进度。
The algorithm would be as such: 算法如下:
How the multiple spell-checkers will access the dictionary is another problem. 多个拼写检查器如何访问字典是另一个问题。 You can load a copy of the dictionary in each worker or you can protect access to the dictionary with a lock (but that would slow things down).
您可以在每个工作程序中加载字典的副本,或者可以使用锁保护对字典的访问(但这会减慢速度)。 If you are lucky, dictionary is thread-safe for reading and you can do simultaneous queries without locking.
如果幸运的话,字典对于读取是线程安全的,您可以在不锁定的情况下同时进行查询。
Appropriate OmniThreadLibrary abstraction for the problem would be either a ParallelTask or a BackgroundWorker . 针对该问题的适当OmniThreadLibrary抽象可以是ParallelTask或BackgroundWorker 。
To parallelize, just create a new class descendent from TThread, create an object from it, give part of the job to the new thread, run Execute, and collect the results in the main thread. 要并行化,只需从TThread创建一个新的类后代,从中创建一个对象,将部分作业提供给新线程,运行Execute,并在主线程中收集结果。
Like this: 像这样:
TMySpellChecker = class(TThread)
protected
FText: String;
FResult: String;
public
procedure Execute; override;
property Text: String read FText write FText;
property Result: String read FResult write FResult;
end;
TMySpellChecker.Execute;
begin
// Analyze the text, and compute the result
end;
In the main thread: 在主线程中:
NewThread := TMySpellChecker.Create(True); // Create suspended
NewThread.Text := TextSegment;
NewThread.Execute;
The thread object will then do the analyzing in the background, while the main thread continues to run. 然后线程对象将在后台进行分析,同时主线程继续运行。
To handle the results, you need to assign a handler to the OnTerminate event of the thread object: 要处理结果,您需要为线程对象的OnTerminate事件分配一个处理程序:
NewThread.OnTerminate := HandleMySpellCheckerTerminate;
This must be done before you run Execute on the thread object. 必须在对线程对象运行Execute之前完成此操作。
To allow for interruptions, one possibility is to break the main text up into segments, place the segments in a list in the main thread, and then analyze the segments one by one using the thread object. 为了允许中断,一种可能性是将主文本分成段,将段放在主线程的列表中,然后使用线程对象逐个分析段。 You can then allow for interruptions between each run.
然后,您可以允许每次运行之间的中断。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.