-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcat_finder.rb
More file actions
65 lines (50 loc) · 1.58 KB
/
Copy pathconcat_finder.rb
File metadata and controls
65 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
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
36
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
require 'set'
class ConcatFinder
attr_reader :sub_words_set
attr_reader :word_candidates_list
def initialize
@sub_words_set = Set.new
@word_candidates_list = Array.new
end
def find
result_hash = Hash.new
@word_candidates_list.each do | word |
if sub_words = find_concats(word) then
result_hash[word] = sub_words
end
end
result_hash
end
def load(io)
io.each_line do |line|
line.strip!
@sub_words_set << line if line.size < 6
@word_candidates_list << line if line.size == 6
end
raise ArgumentError.new("No valid word candidate") if @word_candidates_list.empty?
raise ArgumentError.new("No valid subwords") if @sub_words_set.empty?
end
private
def find_concats(word)
@sub_words_set.each do | sub_word |
word = word.downcase
sub_word = sub_word.downcase
if (word.include?(sub_word)) then
index = word.index(sub_word)
if index == 0 then
first_part = sub_word
remaining = word[first_part.size,word.size - first_part.size]
second_part = remaining if @sub_words_set.member?(remaining)
else
second_part = sub_word
begining = word[0,word.size - second_part.size]
first_part = begining if @sub_words_set.member?(begining) and (begining+second_part) == word
end
if first_part and second_part then
return Array.new([first_part,second_part])
end
end
end
return nil
end
end