<commit_before>module Signore class Signature < Sequel::Model

  NBSP = ' '

  many_to_many :labels

  def self.find_random_by_labels labels
    sigs = labels.empty? ? all : labels.map { |label| Label[:name => label].signatures rescue [] }.inject(:&)
    sigs.sort_by { rand }.first
  end

  def self.create_with_labels params
    labels = params.delete(:labels) || []
    params.delete_if { |key, value| value.empty? }
    sig = self.create params
    labels.each { |label| sig.add_label Label.find_or_create :name => label }
    sig
  end

  def display
    # FIXME: figure out how to drop the force_encoding call
    lines = text.force_encoding 'UTF-8'
    lines += " #{meta}" if has_meta?
    lines = wrap lines
    lines = right_align_meta lines
    lines.tr NBSP, ' '
  end

  private

  def has_meta?
    author or source
  end

  def meta
    # FIXME: figure out how to drop the force_encoding call
    case
    when author && source then "[#{author}, #{source}]"
    when author           then "[#{author}]"
    when source           then "[#{source}]"
    else ''
    end.force_encoding('UTF-8').tr ' ', NBSP
  end

  def right_align_meta lines
    return lines unless has_meta?
    lenghts = lines.split("\n").map(&:size)
    if lenghts.size > 1 and lenghts.last == lenghts.max and lenghts.count(lenghts.last) == 1
      lines[-meta.size-1] = "\n"
    end
    lenghts = lines.split("\n").map(&:size)
    lines[-meta.size..-1] = ' ' * (lenghts.max - lenghts.last) + meta
    lines
  end

  def wrap lines
    last_line = lines.split("\n").last
    lines.split("\n").map do |line|
      wrap_line line, line == last_line
    end.join "\n"
  end

  def wrap_line line, is_last
    best_wrap = line.gsub /(.{1,80})( |$\n?)/, "\\1\n"
    max_height = best_wrap.count "\n"
    79.downto 1 do |size|
      new_wrap = line.gsub /(.{1,#{size}})( |$\n?)/, "\\1\n"
      lengths = new_wrap.split("\n").map(&:size)
      break if has_meta? and is_last and lengths.last == lengths.max and lengths.count(lengths.max) == 1
      break if new_wrap.count("\n") > max_height
      best_wrap = new_wrap
    end
    best_wrap.chomp
  end

end end
<commit_msg>simplify Signature#wrap_line a bit<commit_after>module Signore class Signature < Sequel::Model

  NBSP = ' '

  many_to_many :labels

  def self.find_random_by_labels labels
    sigs = labels.empty? ? all : labels.map { |label| Label[:name => label].signatures rescue [] }.inject(:&)
    sigs.sort_by { rand }.first
  end

  def self.create_with_labels params
    labels = params.delete(:labels) || []
    params.delete_if { |key, value| value.empty? }
    sig = self.create params
    labels.each { |label| sig.add_label Label.find_or_create :name => label }
    sig
  end

  def display
    # FIXME: figure out how to drop the force_encoding call
    lines = text.force_encoding 'UTF-8'
    lines += " #{meta}" if has_meta?
    lines = wrap lines
    lines = right_align_meta lines
    lines.tr NBSP, ' '
  end

  private

  def has_meta?
    author or source
  end

  def meta
    # FIXME: figure out how to drop the force_encoding call
    case
    when author && source then "[#{author}, #{source}]"
    when author           then "[#{author}]"
    when source           then "[#{source}]"
    else ''
    end.force_encoding('UTF-8').tr ' ', NBSP
  end

  def right_align_meta lines
    return lines unless has_meta?
    lenghts = lines.split("\n").map(&:size)
    if lenghts.size > 1 and lenghts.last == lenghts.max and lenghts.count(lenghts.last) == 1
      lines[-meta.size-1] = "\n"
    end
    lenghts = lines.split("\n").map(&:size)
    lines[-meta.size..-1] = ' ' * (lenghts.max - lenghts.last) + meta
    lines
  end

  def wrap lines
    last_line = lines.split("\n").last
    lines.split("\n").map do |line|
      wrap_line line, line == last_line
    end.join "\n"
  end

  def wrap_line line, is_last
    best_wrap = line.gsub /(.{1,80})( |$\n?)/, "\\1\n"
    79.downto 1 do |size|
      new_wrap = line.gsub /(.{1,#{size}})( |$\n?)/, "\\1\n"
      break if new_wrap.count("\n") > best_wrap.count("\n")
      lengths = new_wrap.split("\n").map(&:size)
      break if has_meta? and is_last and lengths.last == lengths.max and lengths.count(lengths.max) == 1
      best_wrap = new_wrap
    end
    best_wrap.chomp
  end

end end

{'fraction_non_alphanumeric': 0.14299021891010713, 'fraction_numerical': 0.007452258965999069, 'mean_word_length': 2.6584327086882453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3273917', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1497, 'n_words': 394}