Skip to content

Instantly share code, notes, and snippets.

@pcdavid
Created July 18, 2009 12:57
Show Gist options
  • Save pcdavid/149545 to your computer and use it in GitHub Desktop.
Save pcdavid/149545 to your computer and use it in GitHub Desktop.
kwic.rb
#!/usr/bin/env ruby
# From "On the Criteria To Be Used in Decomposing Systems into
# Modules", David L. Parnas, in CACM 15.12, p. 1053-1058, 1972:
#
# "The KWIC index system accepts an ordered set of lines, each line is
# an ordered set of words, and each word is an ordered set of
# characters. Any line may be "circularly shifted" by repeatedly
# removing the first word and appending it at the end of the line. The
# KWIC index system outputs a listing of all circular shifts of all
# lines in alphabetical order.
# This is a small system. Except under extreme circumstances (huge
# data base, no supporting software), such a system could be produced
# by a good programmer within a week or two."
lines = Array.new
ARGF.each_line do |line|
words = line.chomp.split
words.each_index do |i|
lines << ( words[i..-1] + words[0...i] ).join(' ')
end
end
puts lines.sort.uniq
# "When in doubt, use brute force." -- Ken Thompson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment