Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
generate some massachusetts town names
#!/usr/bin/env ruby
class WordMarkov
def initialize(data=[])
@nexts = {}
@initials = []
data.each do |word|
chars = word.split('')
@initials << chars[0..1]
chars[0..-3].each_with_index do |char, i|
key = chars[i..i+1].join
val = chars[i + 2]
@nexts[key] ? @nexts[key] << val : @nexts[key] = [val]
end
key = chars[-2..-1].join
val = nil
@nexts[key] ? @nexts[key] << val : @nexts[key] = [val]
end
end
def word
out = @initials[rand(@initials.length)].dup
while out[-1] != nil
possibles = @nexts[out[-2..-1].join]
out << possibles[rand(possibles.length)]
end
out.pop
return out.join
end
def possible?(word)
chars = word.split('')
while chars.length > 2
if @nexts[chars[0..1].join].include? chars[2]
chars.shift
else
return false
end
end
return true
end
end
if __FILE__ == $0
towns = ['Abington', 'Acton', 'Acushnet', 'Adams', 'Agawam', 'Alford', 'Amesbury', 'Amherst', 'Andover', 'Aquinnah', 'Arlington', 'Ashburnham', 'Ashby', 'Ashfield', 'Ashland', 'Athol', 'Attleboro', 'Auburn', 'Avon', 'Ayer', 'Barnstable', 'Barre', 'Becket', 'Bedford', 'Belchertown', 'Bellingham', 'Belmont', 'Berkley', 'Berlin', 'Bernardston', 'Beverly', 'Billerica', 'Blackstone', 'Blandford', 'Bolton', 'Boston', 'Bourne', 'Boxborough', 'Boxford', 'Boylston', 'Braintree', 'Brewster', 'Bridgewater', 'Brimfield', 'Brockton', 'Brookfield', 'Brookline', 'Buckland', 'Burlington', 'Cambridge', 'Canton', 'Carlisle', 'Carver', 'Charlemont', 'Charlton', 'Chatham', 'Chelmsford', 'Chelsea', 'Cheshire', 'Chester', 'Chesterfield', 'Chicopee', 'Chilmark', 'Clarksburg', 'Clinton', 'Cohasset', 'Colrain', 'Concord', 'Conway', 'Cummington', 'Dalton', 'Danvers', 'Dartmouth', 'Dedham', 'Deerfield', 'Dennis', 'Dighton', 'Douglas', 'Dover', 'Dracut', 'Dudley', 'Dunstable', 'Duxbury', 'East Bridgewater', 'East Brookfield', 'East Longmeadow', 'Eastham', 'Easthampton', 'Easton', 'Edgartown', 'Egremont', 'Erving', 'Essex', 'Everett', 'Fairhaven', 'Fall River', 'Falmouth', 'Fitchburg', 'Florida', 'Foxborough', 'Framingham', 'Franklin', 'Freetown', 'Gardner', 'Georgetown', 'Gill', 'Gloucester', 'Goshen', 'Gosnold', 'Grafton', 'Granby', 'Granville', 'Great Barrington', 'Greenfield', 'Groton', 'Groveland', 'Hadley', 'Halifax', 'Hamilton', 'Hampden', 'Hancock', 'Hanover', 'Hanson', 'Hardwick', 'Harvard', 'Harwich', 'Hatfield', 'Haverhill', 'Hawley', 'Heath', 'Hingham', 'Hinsdale', 'Holbrook', 'Holden', 'Holland', 'Holliston', 'Holyoke', 'Hopedale', 'Hopkinton', 'Hubbardston', 'Hudson', 'Hull', 'Huntington', 'Ipswich', 'Kingston', 'Lakeville', 'Lancaster', 'Lanesborough', 'Lawrence', 'Lee', 'Leicester', 'Lenox', 'Leominster', 'Leverett', 'Lexington', 'Leyden', 'Lincoln', 'Littleton', 'Longmeadow', 'Lowell', 'Ludlow', 'Lunenburg', 'Lynn', 'Lynnfield', 'Malden', 'Manchester-by-the-Sea', 'Mansfield', 'Marblehead', 'Marion', 'Marlborough', 'Marshfield', 'Mashpee', 'Mattapoisett', 'Maynard', 'Medfield', 'Medford', 'Medway', 'Melrose', 'Mendon', 'Merrimac', 'Methuen', 'Middleborough', 'Middlefield', 'Middleton', 'Milford', 'Millbury', 'Millis', 'Millville', 'Milton', 'Monroe', 'Monson', 'Montague', 'Monterey', 'Montgomery', 'Mount Washington', 'Nahant', 'Nantucket', 'Natick', 'Needham', 'New Ashford', 'New Bedford', 'New Braintree', 'New Marlborough', 'New Salem', 'Newbury', 'Newburyport', 'Newton', 'Norfolk', 'North Adams', 'North Andover', 'North Attleborough', 'North Brookfield', 'North Reading', 'Northampton', 'Northborough', 'Northbridge', 'Northfield', 'Norton', 'Norwell', 'Norwood', 'Oak Bluffs', 'Oakham', 'Orange', 'Orleans', 'Otis', 'Oxford', 'Palmer', 'Paxton', 'Peabody', 'Pelham', 'Pembroke', 'Pepperell', 'Peru', 'Petersham', 'Phillipston', 'Pittsfield', 'Plainfield', 'Plainville', 'Plymouth', 'Plympton', 'Princeton', 'Provincetown', 'Quincy', 'Randolph', 'Raynham', 'Reading', 'Rehoboth', 'Revere', 'Richmond', 'Rochester', 'Rockland', 'Rockport', 'Rowe', 'Rowley', 'Royalston', 'Russell', 'Rutland', 'Salem', 'Salisbury', 'Sandisfield', 'Sandwich', 'Saugus', 'Savoy', 'Scituate', 'Seekonk', 'Sharon', 'Sheffield', 'Shelburne', 'Sherborn', 'Shirley', 'Shrewsbury', 'Shutesbury', 'Somerset', 'Somerville', 'South Hadley', 'Southampton', 'Southborough', 'Southbridge', 'Southwick', 'Spencer', 'Springfield', 'Sterling', 'Stockbridge', 'Stoneham', 'Stoughton', 'Stow', 'Sturbridge', 'Sudbury', 'Sunderland', 'Sutton', 'Swampscott', 'Swansea', 'Taunton', 'Templeton', 'Tewksbury', 'Tisbury', 'Tolland', 'Topsfield', 'Townsend', 'Truro', 'Tyngsborough', 'Tyringham', 'Upton', 'Uxbridge', 'Wakefield', 'Wales', 'Walpole', 'Waltham', 'Ware', 'Wareham', 'Warren', 'Warwick', 'Washington', 'Watertown', 'Wayland', 'Webster', 'Wellesley', 'Wellfleet', 'Wendell', 'Wenham', 'West Boylston', 'West Bridgewater', 'West Brookfield', 'West Newbury', 'West Springfield', 'West Stockbridge', 'West Tisbury', 'Westborough', 'Westfield', 'Westford', 'Westhampton', 'Westminster', 'Weston', 'Westport', 'Westwood', 'Weymouth', 'Whately', 'Whitman', 'Wilbraham', 'Williamsburg', 'Williamstown', 'Wilmington', 'Winchendon', 'Winchester', 'Windsor', 'Winthrop', 'Woburn', 'Worcester', 'Worthington', 'Wrentham', 'Yarmouth']
w = WordMarkov.new(towns)
100.times do
word = w.word
puts word unless towns.include? word
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment