Module: TWFilter::Han

Defined in:
lib/twfilter/han.rb,
sig/twfilter.rbs

Constant Summary collapse

CHAR =

Returns:

  • (::Regexp)
/\p{Han}/
RUN =

Returns:

  • (::Regexp)
/\p{Han}+/
BMP =

CJK Unified Ideographs and Extension A — the planes a Taiwanese font is expected to render. \pTWFilter::Han is wider; .basic? distinguishes the two.

(0x4E00..0x9FFF).freeze
EXTENSION_A =
(0x3400..0x4DBF).freeze

Class Method Summary collapse

Class Method Details

.basic?(char) ⇒ Boolean

Parameters:

  • (::String)

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/twfilter/han.rb', line 25

def basic?(char)
  code = char.ord
  BMP.cover?(code) || EXTENSION_A.cover?(code)
end

.char?(char) ⇒ Boolean

Parameters:

  • (::String)

Returns:

  • (Boolean)


14
# File 'lib/twfilter/han.rb', line 14

def char?(char) = char.match?(CHAR)

.count(text) ⇒ ::Integer

Parameters:

  • (::String)

Returns:

  • (::Integer)


16
# File 'lib/twfilter/han.rb', line 16

def count(text) = text.each_char.count { |char| char.match?(CHAR) }

.ratio(text) ⇒ ::Float

Parameters:

  • (::String)

Returns:

  • (::Float)


20
21
22
23
# File 'lib/twfilter/han.rb', line 20

def ratio(text)
  length = text.length
  length.zero? ? 0.0 : count(text).fdiv(length)
end

.runs(text) ⇒ ::Array[::String]

Parameters:

  • (::String)

Returns:

  • (::Array[::String])


18
# File 'lib/twfilter/han.rb', line 18

def runs(text) = text.scan(RUN)