Love letter algo
This commit is contained in:
parent
953780d09e
commit
e30d4097dd
@ -410,7 +410,7 @@ function long() {
|
||||
}
|
||||
````
|
||||
|
||||
To decide if an element (```adjective``` and ```adverb```) should be there or not we will write a ```maybe``` function which will randomly choose true or false if the element should be chose from an array.
|
||||
To decide if an element (```adjective``` or ```adverb```) should be part of the sentence or not we need to write a ```maybe``` function which randomly chooses true or false if the element should be chosen from a given array.
|
||||
|
||||
```javascript
|
||||
// function that randomly decides if an element from an array should be selected or not
|
||||
@ -425,15 +425,142 @@ function maybe(array_to_choose_from) {
|
||||
}
|
||||
}
|
||||
````
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
"determiner + adjective + noun + adverb + verb + determiner + adjective + noun". Based on this, as an example, your script could output a sentence such as the following: "MY SWEET LONGING BREATHLESSLY ADORES YOUR ANXIOUS WISH". Furthermore, your script should allow for variations of this sentence structure, that is, exclude (or not) certain components depending on randomness and yet keep the sentence grammatically correct. For example the sentence above could become: "MY SWEET LONGING ADORES YOUR WISH".
|
||||
|
||||
With this ```maybe``` function we now have a way to diversify the letter's sentences and thus create more variation on our generated long forms.
|
||||
|
||||
How about the short forms then?
|
||||
|
||||
YOU ARE MY + ```adjective``` + ```noun``` + .
|
||||
MY + ```adjective``` + ```noun``` + .
|
||||
|
||||
We can simply write a ```short``` function like:
|
||||
|
||||
```javascript
|
||||
// function that randomly generates a short form sentence
|
||||
function short() {
|
||||
return choice(adjectives) + ' ' + choice(nouns) + '. ';
|
||||
}
|
||||
```
|
||||
|
||||
And prefix it with YOU ARE MY or MY when we are in the midst of writing the sequence of sentences. This might become clearer below.
|
||||
|
||||
How about the opening? Notice the ```first``` and ```second``` in the vocabulary. These are exactly what we need:
|
||||
|
||||
```javascript
|
||||
// function that randomly an opening
|
||||
function opening() {
|
||||
return choice(first) + ' ' + choice(second);
|
||||
}
|
||||
```
|
||||
|
||||
And the closing in form YOURS + ```adverb```? Simple:
|
||||
|
||||
```javascript
|
||||
// function that randomly a closing
|
||||
function closing() {
|
||||
return YOURS + ' ' + choice(adverbs);
|
||||
}
|
||||
```
|
||||
|
||||
Ok, we have most of the ingredients in place with the functions ```choice```, ```maybe```, ```long```, ```short```, ```opening```, ```closing``` right?
|
||||
|
||||
How about sequencing ```long``` and ```short``` forms? Remember what we observed above (i.e. 4 to 5 sentences, short form MY + ```adjective``` + ```noun``` + . always follows the other short form, etc.)? Following these observation here is a skeleton of an algorithm doing just that:
|
||||
|
||||
```javascript
|
||||
// start with an empty text
|
||||
var text = "";
|
||||
|
||||
// did we just wrote the short form with "YOU ARE MY"
|
||||
var YOU_ARE = false;
|
||||
|
||||
// loop 5 times to generate 5 sentences
|
||||
for(let i = 0; i < 5; i++) {
|
||||
|
||||
// choose if the next sentence is a short or long
|
||||
let sentence_form = choice(['short', 'long']);
|
||||
|
||||
if(sentence_form == 'short') {
|
||||
// there's two types of short to switch from
|
||||
|
||||
// the short form MY + ```adjective``` + ```noun``` + .
|
||||
// always follows the YOU ARE MY + ```adjective``` + ```noun``` + . one
|
||||
// and
|
||||
// the short form YOU ARE MY + ```adjective``` + ```noun``` + . one
|
||||
// can never follow itself
|
||||
|
||||
// this means we need keep track of when "YOU ARE MY" is generated
|
||||
// and switch to the other form next time. This is what the variable
|
||||
// YOU_ARE is doing
|
||||
|
||||
if(YOU_ARE) {
|
||||
// the ": MY" can only follow a "YOU ARE MY"
|
||||
text += ": MY " + short();
|
||||
YOU_ARE = false;
|
||||
} else {
|
||||
text += ": MY " + short();
|
||||
// the ": MY" can only follow a "YOU ARE MY"
|
||||
YOU_ARE = true;
|
||||
}
|
||||
} else if(sentence_form == 'long') {
|
||||
text += long();
|
||||
// make sure the next sentence is not ": MY"
|
||||
YOU_ARE = false
|
||||
}
|
||||
}
|
||||
````
|
||||
|
||||
|
||||
|
||||
### Putting it all together
|
||||
|
||||
|
||||
|
||||
```javascript
|
||||
function write_letter(){
|
||||
var text = "";
|
||||
|
||||
// write opening
|
||||
text += opening();
|
||||
text += "\n"; // this is a new line!
|
||||
|
||||
var YOU_ARE = false;
|
||||
// write 5 sentences
|
||||
for(let i = 0; i < 5; i++) {
|
||||
|
||||
// choose if the next sentence is a short or long
|
||||
let sentence_form = choice(['short', 'long']);
|
||||
|
||||
if(sentence_form == 'short') {
|
||||
// there's two types of short to switch from
|
||||
if(YOU_ARE) {
|
||||
// the ": MY" can only follow a "YOU ARE MY"
|
||||
text += ": MY " + short();
|
||||
YOU_ARE = false;
|
||||
} else {
|
||||
text += ": MY " + short();
|
||||
// the ": MY" can only follow a "YOU ARE MY"
|
||||
YOU_ARE = true;
|
||||
}
|
||||
} else if(sentence_form == 'long') {
|
||||
text += long();
|
||||
// make sure the next sentence is not ": MY"
|
||||
YOU_ARE = false
|
||||
}
|
||||
}
|
||||
|
||||
text += "\n"; // this is a new line!
|
||||
|
||||
// write closing
|
||||
text += " " + closing();
|
||||
|
||||
text += "\n"; // this is a new line!
|
||||
|
||||
// sign
|
||||
text += "MUC";
|
||||
|
||||
// return the letter
|
||||
return text;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user