简体   繁体   English

如何使用Delphi并行化检查拼写?

[英]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: 算法如下:

  • Create multiple workers. 创建多个工人。
  • Create a spell-checker in each worker. 在每个工人中创建一个拼写检查器。
  • Grab the text and split it into work units (word or sentences). 抓取文本并将其拆分为工作单位(单词或句子)。 Each work unit must be accompanied with the location in original text. 每个工作单位必须附有原始文本的位置。
  • Send work units to workers. 将工作单位发送给工人。 Good approach is to send data into common queue from which workers are taking work units. 好的方法是将数据发送到工作单位的公共队列中。 Queue must either support multiple readers or you must use locking to access it. 队列必须支持多个读取器,或者必须使用锁定才能访问它。
  • Each worker takes a work unit, runs a spell-check and returns the result (together with the location in the original text) to the owner. 每个工人都需要一个工作单元,进行拼写检查并将结果(连同原始文本中的位置)返回给所有者。
    • The simplest way to return a result is to send a message to the main thread. 返回结果的最简单方法是向主线程发送消息。
    • Alternatively, you can write results into a result queue (which must either use locking or support multiple writers) and application can then poll those results (either from a timer or from the OnIdle handler). 或者,您可以将结果写入结果队列(必须使用锁定或支持多个编写器),然后应用程序可以轮询这些结果(来自定时器或来自OnIdle处理程序)。

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抽象可以是ParallelTaskBackgroundWorker

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.

相关问题 如何通过Delphi中的Ole更改MS Word拼写检查语言? - How to change MS Word spelling check language through Ole in Delphi? 如何检查Delphi中枚举的IStorage元素的类型? - How can I check the type of the enumerated IStorage element in Delphi? 如何从Sourceforge查看Delphi演示? - How can I check out the Delphi demos from Sourceforge? 在 Delphi 中,如何使用 DLL 过程修改 TStringGrid 中的单元格? - In Delphi, how can I modify cells in a TStringGrid using a DLL procedure? 如何找到是否安装了打印机(使用Delphi) - How can I find if there are printers installed (using Delphi) 如何在图像Delphi上使用鼠标绘制多边形? - How can i draw polygon by using mouse on an image Delphi? 在Delphi中使用TObjectDictionary时,如何避免EInvalidPointer错误? - How can I avoid EInvalidPointer error when using TObjectDictionary in Delphi? 如何在Delphi中使用“文件查找”执行布尔“ AND”搜索? - How can I perform a boolean 'AND' search in Delphi using 'Find in Files? 如何使用Delphi(BDE组件)优化更新Postgres? - How can I optimize updating Postgres using Delphi (BDE components)? 如何使用 delphi 7 将密钥发送到另一个应用程序? - How can I send keys to another application using delphi 7?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM