Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I like Markov chains. Last time I used one, I made one that generates words generates words. This time, I made one that generates sentences, given a set of words and valid connections. This time, it's not weighted and it's reading from a JSON file, so the code is a bit simpler:

require 'json'
data = JSON[ARGV[0]]
CONNECTIONS = data['connections']
WORDS = data['words']
sentence = []
current_pos = 'start'
until current_pos == 'end'
 current_pos = CONNECTIONS[current_pos].sample
 sentence << WORDS[current_pos].sample
end
puts sentence.join(' ')

If we use this as the input data:

{
 "connections": {
 "start": ["article"],
 "article": ["adjective", "noun"],
 "adjective": ["adjective", "noun"],
 "noun": ["verb", "end"],
 "verb": ["article"],
 "end": []
 },
 "words": {
 "article": ["the"],
 "noun": ["dog", "cat", "penny", "truck"],
 "verb": ["ate", "stole", "helped", "cuddled"],
 "adjective": ["red", "big", "fluffy", "honorable"],
 "end": [""]
 }
}

which gives some fantastic word salad like:

the fluffy penny ate the truck stole the penny cuddled the cat stole the big cat 
the red red dog helped the honorable honorable fluffy big big red big cat stole the truck 

And, my personal favorite so far:

the fluffy honorable cat stole the dog 

I'm looking for tips on:

  • Efficiency -- I think this is about as good as it'll get, but any suggestions are great.
  • Usability -- I like word salad. I want to share it with everyone. To that end, I want to make this as easy and simple-to-use as possible.
  • Readbility -- Because I like word salad (it's very nutritious) I want people to easily understand how to make it. Again, I think this is about as it's gonna get, but any suggestions are great.

I like Markov chains. Last time I used one, I made one that generates words. This time, I made one that generates sentences, given a set of words and valid connections. This time, it's not weighted and it's reading from a JSON file, so the code is a bit simpler:

require 'json'
data = JSON[ARGV[0]]
CONNECTIONS = data['connections']
WORDS = data['words']
sentence = []
current_pos = 'start'
until current_pos == 'end'
 current_pos = CONNECTIONS[current_pos].sample
 sentence << WORDS[current_pos].sample
end
puts sentence.join(' ')

If we use this as the input data:

{
 "connections": {
 "start": ["article"],
 "article": ["adjective", "noun"],
 "adjective": ["adjective", "noun"],
 "noun": ["verb", "end"],
 "verb": ["article"],
 "end": []
 },
 "words": {
 "article": ["the"],
 "noun": ["dog", "cat", "penny", "truck"],
 "verb": ["ate", "stole", "helped", "cuddled"],
 "adjective": ["red", "big", "fluffy", "honorable"],
 "end": [""]
 }
}

which gives some fantastic word salad like:

the fluffy penny ate the truck stole the penny cuddled the cat stole the big cat 
the red red dog helped the honorable honorable fluffy big big red big cat stole the truck 

And, my personal favorite so far:

the fluffy honorable cat stole the dog 

I'm looking for tips on:

  • Efficiency -- I think this is about as good as it'll get, but any suggestions are great.
  • Usability -- I like word salad. I want to share it with everyone. To that end, I want to make this as easy and simple-to-use as possible.
  • Readbility -- Because I like word salad (it's very nutritious) I want people to easily understand how to make it. Again, I think this is about as it's gonna get, but any suggestions are great.

I like Markov chains. Last time I used one, I made one that generates words. This time, I made one that generates sentences, given a set of words and valid connections. This time, it's not weighted and it's reading from a JSON file, so the code is a bit simpler:

require 'json'
data = JSON[ARGV[0]]
CONNECTIONS = data['connections']
WORDS = data['words']
sentence = []
current_pos = 'start'
until current_pos == 'end'
 current_pos = CONNECTIONS[current_pos].sample
 sentence << WORDS[current_pos].sample
end
puts sentence.join(' ')

If we use this as the input data:

{
 "connections": {
 "start": ["article"],
 "article": ["adjective", "noun"],
 "adjective": ["adjective", "noun"],
 "noun": ["verb", "end"],
 "verb": ["article"],
 "end": []
 },
 "words": {
 "article": ["the"],
 "noun": ["dog", "cat", "penny", "truck"],
 "verb": ["ate", "stole", "helped", "cuddled"],
 "adjective": ["red", "big", "fluffy", "honorable"],
 "end": [""]
 }
}

which gives some fantastic word salad like:

the fluffy penny ate the truck stole the penny cuddled the cat stole the big cat 
the red red dog helped the honorable honorable fluffy big big red big cat stole the truck 

And, my personal favorite so far:

the fluffy honorable cat stole the dog 

I'm looking for tips on:

  • Efficiency -- I think this is about as good as it'll get, but any suggestions are great.
  • Usability -- I like word salad. I want to share it with everyone. To that end, I want to make this as easy and simple-to-use as possible.
  • Readbility -- Because I like word salad (it's very nutritious) I want people to easily understand how to make it. Again, I think this is about as it's gonna get, but any suggestions are great.
Source Link
anon
anon

Markov chain-based word salad generator

I like Markov chains. Last time I used one, I made one that generates words. This time, I made one that generates sentences, given a set of words and valid connections. This time, it's not weighted and it's reading from a JSON file, so the code is a bit simpler:

require 'json'
data = JSON[ARGV[0]]
CONNECTIONS = data['connections']
WORDS = data['words']
sentence = []
current_pos = 'start'
until current_pos == 'end'
 current_pos = CONNECTIONS[current_pos].sample
 sentence << WORDS[current_pos].sample
end
puts sentence.join(' ')

If we use this as the input data:

{
 "connections": {
 "start": ["article"],
 "article": ["adjective", "noun"],
 "adjective": ["adjective", "noun"],
 "noun": ["verb", "end"],
 "verb": ["article"],
 "end": []
 },
 "words": {
 "article": ["the"],
 "noun": ["dog", "cat", "penny", "truck"],
 "verb": ["ate", "stole", "helped", "cuddled"],
 "adjective": ["red", "big", "fluffy", "honorable"],
 "end": [""]
 }
}

which gives some fantastic word salad like:

the fluffy penny ate the truck stole the penny cuddled the cat stole the big cat 
the red red dog helped the honorable honorable fluffy big big red big cat stole the truck 

And, my personal favorite so far:

the fluffy honorable cat stole the dog 

I'm looking for tips on:

  • Efficiency -- I think this is about as good as it'll get, but any suggestions are great.
  • Usability -- I like word salad. I want to share it with everyone. To that end, I want to make this as easy and simple-to-use as possible.
  • Readbility -- Because I like word salad (it's very nutritious) I want people to easily understand how to make it. Again, I think this is about as it's gonna get, but any suggestions are great.
lang-rb

AltStyle によって変換されたページ (->オリジナル) /