# Adaptive Text Editor (#217)

**URL:** <https://www.ruby-forum.com/t/adaptive-text-editor-217/173434>\
**Category:** Ruby\
**Created:** [August 8, 2009, 6:06pm UTC](https://www.ruby-forum.com/t/adaptive-text-editor-217/173434 "2009-08-08T18:06:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [August 8, 2009, 6:06pm UTC](https://www.ruby-forum.com/t/adaptive-text-editor-217/173434/1 "2009-08-08T18:06:45Z")

</div>

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

The three rules of Ruby Q.:

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 Q. by submitting ideas and responses  
as often as you can!  
Visit: [http://rubyquiz.strd6.com/suggestions](http://rubyquiz.strd6.com/suggestions)

3. Enjoy!

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

RSS Feed: [http://rubyquiz.strd6.com/quizzes.rss](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!

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [August 12, 2009, 12:45pm UTC](https://www.ruby-forum.com/t/adaptive-text-editor-217/173434/2 "2009-08-12T12:45:09Z")

</div>

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
