-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_main.rb
62 lines (52 loc) · 1.71 KB
/
old_main.rb
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
text = File.read('words.txt')
filter = File.read('filter_words.txt')
#words_occurrences = text.scan(/\w+/).reduce(Hash.new(0)){|res,w| res[w.downcase]+=1;res}.sort.to_h
words_occurrences = text.scan(/[^\p{Alpha}']/i).reduce(Hash.new(0)){|res,w| res[w.downcase]+=1;res}.sort.to_h
filtered_words ||= words_occurrences.reject do |word| filter.include?(word)
end
sorted_words_by_value = filtered_words.sort_by {|_key, value| value}.to_h
reverted = filtered_words.keys.sort[0,10].each do |key|
end
longest_words = filtered_words.inject({}) do |result, word|
result[word] = word.length
result
end.group_by { |key, value| value }.max_by { |key, value| key }.last
puts "-"*20
puts words_occurrences
#puts "-"*20, 'words_occurrences_cleaned'
#puts words_occurrences_cleaned
puts "-"*20
puts words_occurrences.length
puts "-"*20, 'Filtered words'
puts filtered_words
puts "-"*20
puts filtered_words.length
puts "-"*20
puts filtered_words.size
puts "-"*20
puts sorted_words_by_value
puts "-"*20
puts reverted
puts "-"*20
puts filtered_words.group_by { |key, value| value }.max_by { |key, value| key }.last
puts "-"*20
puts longest_words
puts "-"*20
WORDS_ONLY_REGEX = /[^\p{Alpha}']/i
SPACE_ONLY_REGEX = /\s+/
text_clean = text.gsub(WORDS_ONLY_REGEX, ' ').gsub(SPACE_ONLY_REGEX, ' ').to_s
puts text_clean
puts "-"*20
puts text_clean.size
puts "-"*20, 'text clean words'
#text_clean_words = text_clean.scan(/\w+/).reduce(Hash.new(0)){|res,w| res[w.downcase]+=1;res}.sort.to_s
#filtered_words2 ||= text_clean.reject do |word| filter.include?(word)
#end
#puts filtered_words2
puts "-"*20
#puts text_clean_words
puts "-"*20
#puts text_clean_words.size
puts "-"*20
#last_five = Hash[text_clean_words.sort_by { |k,v| -v }[0..4]]
#puts last_five