[QUIZ] Adaptive Text Editor (#217)

D

Daniel Moore

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

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!
 
M

Martin Boese

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



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

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top