如何使用Delphi并行化检查拼写? [英] How can I parallelize check spelling using Delphi?

查看:93
本文介绍了如何使用Delphi并行化检查拼写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种用Delphi编写的拼写检查器.它逐句分析文本. 解析每个句子后,它会根据一些规则为错误的项目着色.用户能够中断此过程,这一点很重要. 我一般如何使用一些第三方的Delphi库来并行化此过程? 在当前状态下,检查后我可以进行动态句子着色.这样用户就可以看到进度.

I've got a sort of spell checker written in 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? In the current state I've got on the fly sentence coloration after check. Thus the user sees the progress.

推荐答案

算法如下:

  • 创建多个工作程序.
  • 在每个工作人员中创建一个拼写检查器.
  • 抓取文本并将其拆分为工作单元(单词或句子).每个工作单元都必须随附原始位置.
  • 将工作单位发送给工人.好的方法是将数据发送到工人正在上班的公共队列中.队列必须支持多个读取器,或者您必须使用锁定来访问它.
  • 每个工人都需要一个工作单元,进行拼写检查,然后将结果(连同原始文本中的位置)返回给所有者.
    • 最简单的返回结果的方法是向主线程发送一条消息.
    • 或者,您可以将结果写入结果队列(必须使用锁定或支持多个编写器),然后应用程序可以轮询这些结果(通过计时器或OnIdle处理程序).
    • 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).

      多个拼写检查者将如何访问字典是另一个问题.您可以在每个工作进程中加载​​词典的副本,也可以使用锁保护对词典的访问(但这会降低速度).如果幸运的话,Dictionary对于读取是线程安全的,并且您可以同时进行查询而无需锁定.

      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.

      针对该问题的适当的OmniThreadLibrary抽象将是 BackgroundWorker .

      Appropriate OmniThreadLibrary abstraction for the problem would be either a ParallelTask or a BackgroundWorker.

      这篇关于如何使用Delphi并行化检查拼写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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