Class: SubstringRules

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lexicon) ⇒ SubstringRules

Returns a new instance of SubstringRules.



4
5
6
7
# File 'lib/my_segments/substring_rules.rb', line 4

def initialize(lexicon)
	@candidates = Candidates.new
   @lex = lexicon
end

Class Method Details

.for(query_term) ⇒ Object

Generates substring rules (segments) for the query term and returns them.



11
12
13
14
15
16
17
18
19
20
# File 'lib/my_segments/substring_rules.rb', line 11

def self.for(query_term)
  segs = []
segs << self.method_1(query_term)
segs << self.method_3(query_term)
segs << self.method_4(query_term)
segs << self.method_5(query_term)
segs << self.method_6(query_term)
segs << self.method_7(query_term)
  segs.flatten
end

.method_1(query) ⇒ Object

This function cuts off one letter at a time from the start and end of the search term… It then re-searches using the new term. It continues to do so until the ET is reached, Or the term has become too small to cut off more letters. Example: %Slovakia% %lovaki% %ovak% etc



40
41
42
43
44
45
46
47
48
# File 'lib/my_segments/substring_rules.rb', line 40

def self.method_1(query)
	q = String.new(query)
	@substring_rules = []
	while q.size > 3 do
		q = q[1..-2]
		@substring_rules << "%" + q + "%"
	end
	@substring_rules
end

.method_3(query) ⇒ Object

This function replaces the middle of the search term with %‘s MySQL views %’s “match anything”. The function then re-searches The database using the new query until either the ET is reached, Or until the query is too short to continue dividing. Example: %Slovakia% %Slov%kia% %Slo%ia% etc



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/my_segments/substring_rules.rb', line 59

def self.method_3(query)
	q = String.new(query)
	@substring_rules =  []
	@length = q.length
	while @length > 3 do
		q.gsub!('%', '')
		q[@length/2] = '%'
		@length = q.length
		@substring_rules << String.new(q)
	end
	@substring_rules
end

.method_4(query) ⇒ Object

This function divides the query in 1/2 and cuts off the front 1/2. It only adds %‘s to the BEGINING of the word. Exmaple: %Slovakia% %akia



78
79
80
81
82
83
84
85
# File 'lib/my_segments/substring_rules.rb', line 78

def self.method_4(query)
  if query.length == 1
     return [query]
   else
 		query = ["%" + query[(query.length/2)..-1]]
 		return query
 	end
end

.method_5(query) ⇒ Object

Same as above function, but keeps the latter 1/2 of the query. However, a percent SHOULD be put at the end of the query and NOT at the begining of the query. Example: %Slovakia% Slov%



94
95
96
97
98
99
100
# File 'lib/my_segments/substring_rules.rb', line 94

def self.method_5(query)
  if query.length == 1
     return [query]
   else
 		return [query[0..(query.length/2)-1] + "%"]
	end
end

.method_6(query) ⇒ Object

This function cuts everything out of the middle of the query… Only leaving the first and last letters. It replaces the chars in the middle of the query wiht a %. Example: Slovakia S%a



109
110
111
# File 'lib/my_segments/substring_rules.rb', line 109

def self.method_6(query)
	query = [query[0].chr + "%" + query[-1].chr]
end

.method_7(query) ⇒ Object

Same as above, but it keeps the last two AND first two chars of the query. Example: Slovakia Sl%ia



118
119
120
121
122
123
124
# File 'lib/my_segments/substring_rules.rb', line 118

def self.method_7(query)
   if query.length == 1
     return [query]
   else
 		return [query[0..1] + "%" + query[-2..-1]]
	end
end

Instance Method Details

#find(query) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/my_segments/substring_rules.rb', line 22

def find(query)
	@misspelled = query
	
	# First look for an exact match, and return if one is found
	find_candidates([query], true)
   if @candidates.size > 0
 		return @candidates
	end
end