Class: TextDetector::Detector::Simple

Inherits:
Base
  • Object
show all
Defined in:
lib/text_detector/detector/simple.rb

Overview

BM法っぽく(トライ木を調べてる時に見かけた実装を参考に)

Instance Attribute Summary

Attributes inherited from Base

#dictionary

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from TextDetector::Detector::Base

Instance Method Details

#detect(text) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/text_detector/detector/simple.rb', line 7

def detect(text)
  # 0文字目から末尾の一つ前まで一文字ずつ始点を移動していく
  0.upto(text.size - 1) do |start|
    # 語の長さ配列から切り出し文字数を取り出していく
    dictionary.depth.each do |size|
      target = text[start, size]
      # 切り出した文字列の長さが、切り出し分より短ければ次のターン
      break if size > target.size

      # 切り出した文字列が辞書に含まれていれば探索終了
      return target if dictionary.lookup(target)
    end
  end

  nil
end