<commit_before>#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-

module UD
  module Formatting

    # Fit a text in a given width (number of chars). It returns
    # a list of lines of text.
    def self.fit(txt, width=79)
      # from http://stackoverflow.com/a/7567210/735926
      txt.split("\n").map{|l| l.scan(/(.{1,#{width}})(?:\s|$)/m) }.flatten
    end

    # Add a tab at the beginning of a text. If it's a list, add a tab at
    # the beginning of each element.
    # [txt] The text to tab, may be a string or a list of strings
    # [width] The width (number of spaces) of a tab
    def self.tab(txt, width=4)
        tab = ' ' * width

        return tab + txt if txt.is_a?(String)

        txt.map { |l| tab + l }
    end

      # Format results for text output (e.g. in the terminal)
      # [results] this must be an array of results, as returned by +UD.query+.
      def self.text(results, color=true)
        require 'colored' if color

        results.map do |r|

          word  = r[:word]
          upvotes = r[:upvotes]
          downvotes = r[:downvotes]

          if (color)
            word      = word.bold
            upvotes   = upvotes.to_s.green
            downvotes = downvotes.to_s.red
          end

          votes = "#{upvotes}/#{downvotes}"
          definition = tab(fit(r[:definition], 75)).join("\n")
          example    = tab(fit(r[:example], 75)).join("\n")

          s = ''

          s << "* #{word} (#{votes}):\n"
          s << "\n"
          s << definition
          s << "\n\n Example:\n"
          s << example
          s << "\n\n"

        end.join("\n")

      end

  end
end
<commit_msg>Support for some edge cases.<commit_after>#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-

module UD
  module Formatting

    # Fit a text in a given width (number of chars). It returns
    # a list of lines of text.
    def self.fit(txt, width=79)
      return [] if width < 1

      # from http://stackoverflow.com/a/7567210/735926
      r = /(.{1,#{width}})(?:\s|$)/m
      txt.split("\n").map { |l| l.scan(r) }.flatten
    end

    # Add a tab at the beginning of a text. If it's a list, add a tab at
    # the beginning of each element.
    # [txt] The text to tab, may be a string or a list of strings
    # [width] The width (number of spaces) of a tab
    def self.tab(txt, width=4)
      width = 0 if width < 0

      tab = ' ' * width

      return tab + txt if txt.is_a?(String)

      txt.map { |l| tab + l }
    end

    # Format results for text output (e.g. in the terminal)
    # [results] this must be an array of results, as returned by +UD.query+.
    def self.text(results, color=true)
      require 'colored' if color

      results.map do |r|

        word  = r[:word]
        upvotes = r[:upvotes]
        downvotes = r[:downvotes]

        if (color)
          word      = word.bold
          upvotes   = upvotes.to_s.green
          downvotes = downvotes.to_s.red
        end

        votes = "#{upvotes}/#{downvotes}"
        definition = tab(fit(r[:definition], 75)).join("\n")
        example    = tab(fit(r[:example], 75)).join("\n")

        s = ''

        s << "* #{word} (#{votes}):\n"
        s << "\n"
        s << definition
        s << "\n\n Example:\n"
        s << example
        s << "\n\n"

      end.join("\n")
    end

  end
end

{'fraction_non_alphanumeric': 0.1447129909365559, 'fraction_numerical': 0.01419939577039275, 'mean_word_length': 1.7753562447611064, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 3, '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': '4852068', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1101, 'n_words': 354}