module Spacifier

Constants

VERSION

Public Class Methods

punct?(c) click to toggle source
# File lib/spacifier/punctuation.rb, line 5
def punct?(c)
  !(/\p{P}/.match(c) == nil)
end
spacify(words) click to toggle source
# File lib/spacifier.rb, line 9
def spacify(words)
  return '' unless words.is_a?(String)
  # strip words
  words.strip!
  # init vars
  new_words = ""
  last_word_type = nil # 0 for cn, 1 for en
  # iterate chars
  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