Class: DxTitleSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/dxtitle_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil, sources: nil, level: 1, debug: false) ⇒ DxTitleSearch

Returns a new instance of DxTitleSearch.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dxtitle_search.rb', line 11

def initialize(obj=nil, sources: nil, level: 1, debug: false)

  @debug = debug
  @indexer = Indexer101.new debug: debug
  @level = level

  s = if sources then

    puts "found sources".info if @debug
    dx = Dynarex.new(sources)

    puts 'before scan_dxindex' if @debug
    a = dx.all.map(&:uri)
    puts 'a: ' + a.inspect if @debug
    @indexer.scan_dxindex a, level: level

  elsif obj and (obj.is_a?(DxLite) or obj.is_a?(Dynarex)) or obj.lines.length < 2

    @indexer.scan_dxindex  obj, level: level

  end

  #jr230620 @indexer.build

end

Instance Method Details

#search(keywords, minchars: 3) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
# File 'lib/dxtitle_search.rb', line 37

def search(keywords, minchars: 3)

  a2 = @indexer.search keywords.split(/[\s:"!\?\(\)£]+(?=[\w#_'-]+)/), \
      minchars: minchars
  # format each result as a Hash object
  a3 = a2.map do |date, title, url|

    {title: title, url: url, date: date}

  end

  puts 'a3: ' + a3.inspect if @debug

  @dx = Dynarex.new('results/result(title, url, date)').import(a3)

  def a3.to_dx()
    Dynarex.new('results/result(title, url, date)').import(self)
  end

  def a3.to_tags()
    a = self.map {|x| x[:title].scan(/(?<=#)(\w+)/)}.flatten
    a.uniq.sort.map {|x| [x, a.count(x)]}
  end

  def a3.search(keywords)

    dx = Dynarex.new('results/result(title, url, date)').import(self)

    level = keywords[0] == '#' ? 0 : 1
    dts = DxTitleSearch.new dx, level: level
    dts.search keywords

  end

  def a3.tag_search(keywords)

    dx = Dynarex.new('results/result(title, url, date)').import(self)

    level = keywords[0] == '#' ? 0 : 1
    dts = DxTitleSearch.new dx, level: level
    dts.tag_search keywords

  end

  return Array.new(a3)

end

#tag_search(keywords) ⇒ Object



85
86
87
88
# File 'lib/dxtitle_search.rb', line 85

def tag_search(keywords)
  r = @indexer.lookup *keywords.split(/[\W]+(?=[\w]+)/).map {|x| "#" + x}
  r.map {|x| x.to_s[1..-1]}
end

#to_tagsObject



90
91
92
93
94
95
96
97
98
# File 'lib/dxtitle_search.rb', line 90

def to_tags()

  a = @indexer.index.map do |key, value|
    [key.to_s[1..-1], value.length]
  end

  a.sort_by(&:first)

end