#!/usr/bin/ruby # frozen_string_literal: false SPECIAL = {"cmd" => "command", "unix" => "UNIX"} print "\rAUTOCORRECT\r\n" require 'set' $ln = Set.new File.read("/usr/share/dict/words").split exc = %w[coo verb cad Mar Mao ma Mai Mae wag wan T's Th wad cab mad strep pi goo fob foe yo yon fog fop war i ] # ' exc.each do |i| raise "No #{i}" unless $ln.include?(i) end $ln -= exc LINES = "\n" << $ln.sort.reverse.join("\n") << "\n" AMOUNT = 1 PUNCT = (("!".."@")).map do |i| i.chr end.reject do |i| ("0".."9").include? i end #puts PUNCT.to_a.to_s def get_word_(word) return word if $ln.include? word return word if $ln.include? word.downcase return word.capitalize if $ln.include? word.capitalize special = SPECIAL[word] if special; return special; end c = word.chars i = c.size def get(c, i) return "" if c.size.zero? while i != 0 c.permutation(i).each do |q| # if q == first_len # i-=1 # next # end q = q.join len = LINES.scan(reg = /\n#{q}[^\n]*/i).map do |i| i[1..-1] end.sort do |a, b| if a[0].downcase? == b[0].downcase? a <=> b else b <=> a end end # puts (len.reject do |i| i.size != 3; end.to_a.to_s) # warn "\n#{q}[^\n]".inspect[1..-2].prepend('/').concat('/') # warn format('permutation of %s chars of %s len of %s', i, q, len.inspect) # return len next if len.empty? char_size = c.size # puts char_size, len[0].size, len.to_s # sleep # puts len.inspect len.each do |i| cur_siz = i.size # puts [i, c].to_s next if cur_siz < char_size next if cur_siz > char_size + AMOUNT funcs = [] q.chars.each do |i| funcs << if ("A".."Z").cover? i :upcase else :downcase end end funcs.size.times do |q| i[q] = i[q].send funcs[q] end return i end end i -= 1 end "ERROR text is #{c.join.inspect}" end if i > 4 max = 0 l = $ln maxes = {} j = c.join # puts 55 (c.size - c.size).times do |n| n += 1 cont = 0 while cont < c.size first = j[0..cont - 1] second = j[cont..cont + n - 1].upcase.clear third = j[n + cont..-1] unless third cont += 1 next end first = "" if cont.zero? puts first + second + third cont += 1 end puts "DONE" end # puts 'DO' l.each_with_index do |a, _index| count = 0 orig_a = a.dup a = a.dup c.each do |char| if a.include? char count += 1 a.sub! char, "" end end next unless count >= max && count != 0 # puts [count, max].to_s maxes[orig_a] = count max = count end maxes.select! do |_k, v| v == max end l.each do |i_| next if i_.size < c.size maxes[i_] = max if i_[0..3] == c.join[0..3] end #puts l.size def foo(a, b, c) p = 0 while p < c.size && p < a.size break if c[p] != a[p] p += 1 end p2 = 0 while p2 < c.size && p2 < b.size break if c[p2] != b[p2] p2 += 1 end [p, p2] end val = maxes.keys.sort do |a, b| p, p2 = foo(a, b, c) if p2 - p >= 1 1 elsif p2 == p2 0 elsif p2 - p <= 1 -1 end end[0..20].sort_by(&:size)[0..10].sort do |a, b| p, p2 = foo(a, b, c) if p2 - p >= 1 1 elsif p2 == p2 0 elsif p2 - p <= 1 -1 end end[0] raise unless val.class == String return val else get(c, i) end end require "io/console" class String def end? self == "\r" || self == "\n" || self == "\t" end def downcase? !!match(/[a-z]/) end end class DoneException < Exception end def _get a = nil $c = "" while a != " " a = $stdin.getc #print a.inspect raise DoneException.new if a == "\x03" if a == "\u007F" $all_text = $all_text[0..-2] $c = $c[0..-2] print "\e[1D \e[1D" next end $c << a $all_text << a putc(a) unless a.end? return nil if a.end? end $c.chomp! " " # $c.gsub!(/\e\[[A-Z]/, "") # print c.inspect File.open("words", "a") do |f| f.puts c.inspect end if false c = $c $c = "" c end str = "" $all_text = "" $stdin.tty? && $stdin.raw do last_char = "." loop do qi = _get i=qi&.gsub(/\e\[[A-Z]/, "") if i.nil? print "\r\n" next end if i.empty? # print ' ' next end # raise if / |\e/.match?(i) new_word = get_word_(i.sub(/[[:punct:]]+/, "")) + i.match(/[[:punct:]]+$/ ).to_s; if PUNCT.include?(last_char); new_word[0]=new_word[0].upcase; end puts new_word.inspect if false amount = i.size + 1 f = File.open("words", "a") if false # if amount == 0 # # text = ""/ #else if new_word == i else print "\e[#{amount}D" << new_word << " " end $all_text = $all_text[0..-(1 + i.size + " ".size)] $all_text << new_word << " " last_char = new_word[-1] # end # puts new_word.inspect #print "\u001b[0K" #{new_word} " end rescue DoneException end #rescue DoneException #$all_text << $c #system 'tput reset' at=nil #puts "\n\nYour story\n\n" IO.popen("xclip -selection c", 'w') do |i| i.print at=$all_text.strip end puts "YOUR STORY COPIED" print at