Module: TWFilter::Block

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

Constant Summary collapse

SIZE =

Sentences per judged window, accepted or rejected as a unit. 300 is large enough for the evidence rate to be stable and small enough to stay within one document.

Returns:

  • (::Integer)
300

Class Method Summary collapse

Class Method Details

.each_kept(lines, size: SIZE, policy: Policy.corpus) ⇒ void

This method returns an undefined value.

Parameters:

  • (::Enumerable[::String])
  • size: (::Integer) (defaults to: SIZE)
  • policy: (Policy) (defaults to: Policy.corpus)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/twfilter/block.rb', line 21

def each_kept(lines, size: SIZE, policy: Policy.corpus)
  return to_enum(:each_kept, lines, size: size, policy: policy) unless block_given?

  window = []
  lines.each do |line|
    window << line
    next if window.length < size

    judge(window, policy: policy).each { |kept| yield kept }
    window = []
  end

  judge(window, policy: policy).each { |kept| yield kept } if window.any?
end

.judge(window, policy: Policy.corpus) ⇒ ::Array[::String]

Parameters:

  • (::Array[::String])
  • policy: (Policy) (defaults to: Policy.corpus)

Returns:

  • (::Array[::String])


11
12
13
14
15
16
17
18
19
# File 'lib/twfilter/block.rb', line 11

def judge(window, policy: Policy.corpus)
  return [] if window.empty?

  reports = window.map { |line| TWFilter.examine(line, policy: policy) }
  return [] if reports.count { |report| !report.ok? } > policy.block_tolerance * window.length
  return [] if Evidence.count(window.join) < policy.evidence_per_100 * window.length / 100

  reports.filter_map { |report| report.text if report.ok? }
end