Module: TWFilter::Checks::Script

Defined in:
lib/twfilter/checks/script.rb,
sig/twfilter.rbs

Constant Summary collapse

NAME =

Returns:

  • (::Symbol)
:script
TIER_FILES =
{
  TIERS.fetch(:common) => "moe_common.txt",
  TIERS.fetch(:secondary) => "moe_secondary.txt",
  TIERS.fetch(:rare) => "moe_rare.txt"
}.freeze

Class Method Summary collapse

Class Method Details

.call(subject) ⇒ ::Array[Finding]

Parameters:

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/twfilter/checks/script.rb', line 14

def call(subject)
  findings = []

  simplified = subject.han_chars.select { |char| simplified_only.include?(char) }.uniq
  findings << finding(:simplified, simplified.first(DETAIL_SAMPLE).join) if simplified.any?

  converted = subject.han_chars.select { |char| converted_orthography.include?(char) }.uniq
  if converted.any?
    severity = subject.policy.orthography_rejects ? :reject : :mark
    findings << finding(:converted_orthography, converted.first(DETAIL_SAMPLE).join, severity)
  end

  unlisted = subject.han_chars.reject { |char| tiers.key?(char) }.uniq
  findings << finding(:unlisted, unlisted.first(DETAIL_SAMPLE).join) if unlisted.any?

  limit = subject.policy.tier_limit
  above = subject.han_chars.select { |char| (tiers[char] || 0) > limit }.uniq
  findings << finding(:above_tier, above.first(DETAIL_SAMPLE).join) if above.any?

  findings
end

.converted_orthography::Set[::String]

Returns:

  • (::Set[::String])


58
# File 'lib/twfilter/checks/script.rb', line 58

def converted_orthography = Tables.set("converted_orthography.txt")

.reset!Object



54
# File 'lib/twfilter/checks/script.rb', line 54

def reset! = @tiers = nil

.simplified_only::Set[::String]

Returns:

  • (::Set[::String])


56
# File 'lib/twfilter/checks/script.rb', line 56

def simplified_only = Tables.set("simplified_only.txt")

.tier(char) ⇒ ::Integer?

Parameters:

  • (::String)

Returns:

  • (::Integer, nil)


36
# File 'lib/twfilter/checks/script.rb', line 36

def tier(char) = tiers[char]

.tier_of(text) ⇒ ::Integer?

Parameters:

  • (::String)

Returns:

  • (::Integer, nil)


38
39
40
41
42
43
# File 'lib/twfilter/checks/script.rb', line 38

def tier_of(text)
  levels = text.each_char.filter_map { |char| tiers[char] if Han.char?(char) }
  return nil if levels.length < text.each_char.count { |char| Han.char?(char) }

  levels.max || TIERS.fetch(:common)
end

.tiers::Hash[::String, ::Integer]

Returns:

  • (::Hash[::String, ::Integer])


45
46
47
48
49
50
51
52
# File 'lib/twfilter/checks/script.rb', line 45

def tiers
  @tiers ||= TIER_FILES
    .flat_map { |level, file| Tables.rows(file).map { |char| [char, level] } }
    .reverse
    .to_h
    .merge(Tables.rows("moe_exception.txt").to_h { |char| [char, TIERS.fetch(:common)] })
    .freeze
end