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
|
# File 'lib/spacifier.rb', line 9
def spacify(words)
return '' unless words.is_a?(String)
words.strip!
new_words = ""
last_word_type = nil
words.each_char do |c|
if c == " "
new_words << c
last_word_type = nil
elsif /\d/.match(c) != nil
new_words << c
last_word_type = nil
elsif punct?(c)
new_words << c
last_word_type = nil
elsif /\p{Han}/.match(c) == nil
new_words << " " if last_word_type == 0
new_words << c
last_word_type = 1
else
new_words << " " if last_word_type == 1
new_words << c
last_word_type = 0
end
end
new_words
end
|