Module: GetUrl::Searcher

Extended by:
Searcher
Included in:
Searcher
Defined in:
lib/geturl/geturl-searcher.rb

Overview

The Searcher deals with collecting all urls and searching through them.

Instance Method Summary collapse

Instance Method Details

#add_score(keyword, item, factor = 1) ⇒ Object

Adds a score for the given keyword on the given item. The given factor gives extra weight to the score.

Parameters:

  • keyword (String)

    The keyword.

  • item (Hash)

    The item.

  • factor (FixNum) (defaults to: 1)

    The weighing factor.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/geturl/geturl-searcher.rb', line 105

def add_score(keyword, item, factor = 1)
  score = 0
  begin
    score += get_field_score(item['name'], keyword, 50, 25)
    score += get_field_score(item['url'], keyword, 50, 25)
    score += get_field_score(item['description'], keyword, 30, 2)
    item['tags'].to_a.each {|tag|
      score += get_field_score(tag, keyword, 30, 20)
    }
  rescue
  end
  item['score'] ||= 0
  item['scored_keywords'] ||= 0
  if score > 0
    item['score'] += score * factor
    item['scored_keywords'] += 1
  end
end

#all(ids = nil) ⇒ Array

Returns all urls of the given ids. If ids is empty, all urls of all sources will be loaded.

Parameters:

  • ids (Array) (defaults to: nil)

    The array of source ids.

Returns:

  • (Array)

    The array of url items.



48
49
50
51
# File 'lib/geturl/geturl-searcher.rb', line 48

def all(ids = nil)
  load_sources(ids)
  return @items
end

#get_field_score(value, keyword, equal_score, contains_score) ⇒ Integer

Returns the relevant score:

  • If the given value and keyword are equal, the given equal_score is returned.

  • Else if the given keyword is a substring of value, the contains_score is returned.

  • Otherwise, 0 is returned.

Parameters:

  • value (String)

    The value.

  • keyword (String)

    The keyword.

  • equal_score (Integer)

    The score when value is equal to the keyword.

  • contains_score (Integer)

    The score when value contains the keyword.

Returns:

  • (Integer)


135
136
137
138
139
140
141
# File 'lib/geturl/geturl-searcher.rb', line 135

def get_field_score(value, keyword, equal_score, contains_score)
  v = value.downcase
  k = keyword.downcase
  return equal_score if (v == k)
  return contains_score if v.include?(k)
  return 0
end

#load_source(id) ⇒ Object

Loads the urls of the source with the given id.

Parameters:

  • id (String)

    The id



16
17
18
19
20
21
22
23
# File 'lib/geturl/geturl-searcher.rb', line 16

def load_source(id)
  file = FileManager.get_local_source_file(id)
  source_items = FileManager.load_items_from_yaml_file(file)
  source_items.each {|item|
    item['source'] = id
    @items << item
  }
end

#load_sources(ids = nil) ⇒ Object

Loads all the given source ids. If ids is empty, all sources will be loaded.

Parameters:

  • ids (Array) (defaults to: nil)

    An array of source ids.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/geturl/geturl-searcher.rb', line 27

def load_sources(ids = nil)
  @items.clear

  all_sources = (ids.nil? || ids.empty?)
  if (all_sources) && (FileManager.all_items_cache_exist?)
    @items = FileManager.get_all_items_from_cache
    return
  end

  ids = Sources.get_sources.keys + ['local'] if ids.nil? || ids.empty?
  load_source(LocalUrls.private_source_id) if ids.include?('local')
  Sources.get_sources.keys.each {|id|
    load_source(id) if ids.include?(id)
  }
  FileManager.cache_all_items(@items) if all_sources
end

#search(keywords = [], options = {}) ⇒ Array

Searches in all urls for those that matches one or more keywords. Each item gets a score on relevance. For specific ids, the options can be given.

Parameters:

  • keywords (Array) (defaults to: [])

    The keywords

  • options (Hash) (defaults to: {})

    The options

Returns:

  • (Array)

    The array of items.



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
# File 'lib/geturl/geturl-searcher.rb', line 60

def search(keywords = [], options = {})
  load_sources(options['ids'])

  # Calculate score for each item, for each keyword.
  @items.each {|item|
    keywords.each {|keyword|
      add_score(keyword, item)
    }
    # Not only individual keywords, but also check on all keywords combined (when mo)
    if (item.size > 1)
      full = keywords.join(' ')
      add_score(full, item, 2)
    end
  }

  # Removes all items that are not relevant (score of 0).
  @items.delete_if {|item| item['score'] == 0}

  if options['--match-all']
    @items.delete_if {|item| item['scored_keywords'] < keywords.size}
  end

  # Sort by score
  @items.sort! {|x, y|
    y['score'] <=> x['score'] rescue y <=> x
  } if (@items.size > 1)

  # Remove all score values
  @items.each {|item|
    item.delete('score')
    item.delete('scored_keywords')
  } unless options.include?('--show-score')

  n = options['-n'].to_i || 0
  @items = @items[0..n-1] if n > 0

  return @items
end