Class: ExpandRuby::Generator
- Inherits:
-
Object
- Object
- ExpandRuby::Generator
- Defined in:
- lib/re_expand/Generator.rb
Instance Method Summary collapse
- #add(pat, command) ⇒ Object
- #delete ⇒ Object
-
#generate(pat, blockambig = 0) ⇒ Object
ルールを解析して状態遷移機械を作成し、patにマッチするもののリストを返す.
-
#initialize(s = nil) ⇒ Generator
constructor
A new instance of Generator.
Constructor Details
#initialize(s = nil) ⇒ Generator
Returns a new instance of Generator.
35 36 37 38 39 40 |
# File 'lib/re_expand/Generator.rb', line 35 def initialize(s = nil) @s = (s ? [s] : []) @matchedlist = [] @par = 0 @commands = [] end |
Instance Method Details
#add(pat, command) ⇒ Object
42 43 44 45 |
# File 'lib/re_expand/Generator.rb', line 42 def add(pat,command) @s << pat @commands << command end |
#delete ⇒ Object
47 48 49 50 |
# File 'lib/re_expand/Generator.rb', line 47 def delete @s.pop @commands.pop end |
#generate(pat, blockambig = 0) ⇒ Object
ルールを解析して状態遷移機械を作成し、patにマッチするもののリストを返す
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/re_expand/Generator.rb', line 55 def generate(pat, blockambig=0) res = [[],[],[]] # 曖昧度0,1,2のマッチ結果 patterns = pat.split.map { |p| p.downcase } @asearch = Asearch.new(pat) scanner = Scanner.new(@s.join('|')) # HelpDataで指定した状態遷移機械全体を生成 # (少し時間がかかる) (startnode, endnode) = regexp(scanner,true) # top level # # 状態遷移機械からDepth-Firstで文字列を生成する # n個のノードを経由して生成される状態の集合をlists[n]に入れる # 生成しながらマッチングも計算する # lists = [] listed = [{},{},{}] block_listed = {} # # 初期状態 # list = [] list[0] = GenNode.new(startnode.id, @asearch.initstate) lists[0] = list # loopcount = 0 (0..10000).each { |length| #loop do # length = loopcount list = lists[length] newlist = [] # puts "#{length} - #{list.length}" list.each { |entry| srcnode = Node.node(entry.id) if list.length * srcnode.trans.length < 100000 then srcnode.trans.each { |trans| ss = entry.substrings.dup srcnode.pars.each { |i| ss[i-1] = ss[i-1].to_s + trans.arg } newstate = @asearch.state(entry.state, trans.str) # 新しいマッチング状態を計算してノードに保存 s = entry.s + trans.str acceptno = trans.dest.accept newlist << GenNode.new(trans.dest.id, newstate, s, ss, acceptno) # # この時点で、マッチしているかどうかをstateとacceptpatで判断できる # マッチしてたら出力リストに加える # if acceptno then if block_given? then (0..blockambig).each { |ambig| if !block_listed[s] then if (newstate[ambig] & @asearch.acceptpat) != 0 then # マッチ block_listed[s] = true yield [s] + ss end end } else maxambig = 2 (0..maxambig).each { |ambig| if !listed[ambig][s] then if (newstate[ambig] & @asearch.acceptpat) != 0 then # マッチ maxambig = ambig if ambig < maxambig # 曖昧度0でマッチすれば曖昧度1の検索は不要 listed[ambig][s] = true sslen = ss.length if sslen > 0 then # patstr = "(.*)\t" * (sslen-1) + "(.*)" patstr = (["(.*)"] * sslen).join("\t") /#{patstr}/ =~ ss.join("\t") end # 'set date #{$2}' のような記述の$変数にsubstringの値を代入 res[ambig] << [s, eval('%('+@commands[acceptno]+')')] end end } end end } end } break if newlist.length == 0 lists << newlist break if res[0].length > 100 # loopcount += 1 #end } [res[0], res[1], res[2]] end |