Forum: Ruby Adaptive Text Editor (#217)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
33117162fff8a9cf50544a604f60c045?d=identicon&s=25 Daniel X Moore (yahivin)
on 2009-08-08 18:06
(Received via mailing list)
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Quiz:

1.  Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have elapsed from the time this message was
sent.

2.  Support Ruby Quiz by submitting ideas and responses
as often as you can!
Visit: http://rubyquiz.strd6.com/suggestions

3.  Enjoy!

Suggestion:  A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion.  Please reply to
the original quiz message, if you can.

RSS Feed: http://rubyquiz.strd6.com/quizzes.rss

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Adaptive Text Editor (#217)

Mbote Rubyists,

Sometimes the auto-completion features of text editors leave much to
be desired. I've dreamed of a day when editors could successfully
anticipate my actions to a high degree of accuracy.

This week's quiz is to create an auto-completion module that will scan
a corpus of text and use it to generate an auto-completion list. The
adaptive part comes in where the most chosen auto-completion options
appear first and the least used eventually drop off the list.
Additionally if the text that is scanned was part of a project written
by the user then the editor will be adapting the the user in that way
as well.

It is not necessary to write an entire editor for the quiz. The
simplest solution would be something like a class that can be
initialized on a collection of files or text and have an
`autocomplete` method that takes the most recently typed letters as a
parameter. The method could also have optional parameters, such as a
context, to be used to enhance the accuracy of the suggestions. Some
contexts that would be most useful would be `inside a a class` or
`inside a method body`. It could even get fancier by having the
specific class that it is called in passed as an option as well, so as
to suggest methods in that class higher than other methods.

As always you can embellish as much or as little as you wish. All
solutions are welcome.

Have Fun!
7b56484f1e9d9af7b4c2c7ef16142197?d=identicon&s=25 Martin Boese (Guest)
on 2009-08-12 12:45
(Received via mailing list)
Below my basic solution. It must first 'learn' from a text and store the
words and the count into a hash. I downcase all words and remove some
special characters. The 'autocomplete' method will lookup matching words
by a composed regexp, then sort by the number found in the text and
finally return the best matching word(s).

class Autocompleter

def autocomplete(word, choices = 1)
   @words.find_all do |k,v|
         k =~ Regexp.new("^#{word.downcase}")
   end.sort { |a,b| b[1] <=> a[1] }.map { |e| e[0] }[0..choices-1]
end

def learn(data)
   @words ||= {}
   data.split(' ').each do |word|
     w =  word.downcase.gsub(/[\,\.\(\)]/, '')
     @words[w] ||= 0
     @words[w] += 1
   end
end

# debug only: show word database
def dump_db
   @words.sort do |a,b|
      b[1] <=> a[1]
   end.each { |word, count| puts "#{count} #{word}" }
end
end


# Some testing

ac = Autocompleter.new

text = <<-END

Sometimes the auto-completion features of text editors leave much to
be desired. I've dreamed of a day when editors could successfully
anticipate my actions to a high degree of accuracy.

This week's quiz is to create an auto-completion module that will scan
a corpus of text and use it to generate an auto-completion list. The
adaptive part comes in where the most chosen auto-completion options
appear first and the least used eventually drop off the list.
Additionally if the text that is scanned was part of a project written
by the user then the editor will be adapting the the user in that way
as well.

It is not necessary to write an entire editor for the quiz. The
simplest solution would be something like a class that can be
initialized on a collection of files or text and have an
`autocomplete` method that takes the most recently typed letters as a
parameter. The method could also have optional parameters, such as a
context, to be used to enhance the accuracy of the suggestions. Some
contexts that would be most useful would be `inside a a class` or
`inside a method body`. It could even get fancier by having the
specific class that it is called in passed as an option as well, so as
to suggest methods in that class higher than other methods.
As always you can embellish as much or as little as you wish. All
solutions are welcome.

Have Fun!
END

ac.learn(text)
ac.dump_db

# some tests
(('a'..'z').to_a + ['cl', 'co', 'us', 'me', 'in']).each do |c|
  puts "Autocomplete on #{c} --> #{ac.autocomplete(c, 10).inspect}"
end
This topic is locked and can not be replied to.