/    Sign up×
Community /Pin to ProfileBookmark

build a sentence (dynamically)

I’m after some advice, whether i’m going down the right path. I wish to build sentences dynamically, which i want to change the wording based on a quantity value.

For example, the quantity value will equal the amount of people’s names shown.

So if quantity equals 1 it might be like this: “Paul goes down to the shop”

However if quantity equals 2, i want to change the sentence to: “Paul & Ringo both go down to the shop”

switching the word “GOES” to “BOTH GO”

Or perhaps if quantity equals 3, it might be: “Paul, Ringo & John all go down to the shop”

switching “GOES” to “ALL GO”

I’m not just looking at building 1 sentence, i’m trying to build several and then 1 is chosen at random and then displays based on pre-said quantity.

Currently, I have a json array of sentences, like the above.

And my current thought process is choosing a array item at random, and based on a random quantity, and by using some mapping and replace regex, for example:

`var str = “I have a cat, a dog, and a goat.”;
var mapObj = {cat:”dog”,dog:”goat”,goat:”cat”};

var re = new RegExp(Object.keys(mapObj).join(“|”),”gi”);
str = str.replace(re, function(matched){
return mapObj[matched];
});`

the string would be a sentence out of my json array, i welcome any thoughts?

Is there another way I can look at doing this? Or shall I continue?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERAug 19.2019 — I'm not interested in creating your problem from scratch,

but I would like to look at what you have done and perhaps offer some suggestions.

Do you have a link to what you have thus far?

Can you provide a brief example of your JSON file?

Additional questions:
  • 1. How do you determine which words will be proper names?

  • 2. Which verbs are you planning on changing, ie: go, both go, all go, and possibly goes, went (etc.)


  • Is this like an adventure game where the description changes with the number of players active

    or the player has come across during the game activities? Trying to discern the purpose of request.
    Copy linkTweet thisAlerts:
    @JMRKERAug 19.2019 — Without any response yet to my last question I just started playing around with a test scenario.

    May not meet any of your needs, but seems to be an interesting project for my idle hands.

    <i>
    </i>&lt;!DOCTYPE html&gt;
    &lt;html lang="en"&gt;
    &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/&gt;
    &lt;title&gt; Noun/Verb Substitutions (attempts) &lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;pre id="demo"&gt;&lt;/pre&gt;

    &lt;script&gt;
    // From: https://www.webdeveloper.com/d/385466-build-a-sentence-dynamically

    //var txt = // desired results
    //<span><code>Paul goes down to the shop.
    // Paul &amp;amp; Ringo both go down to the shop.
    // Paul, Ringo &amp;amp; John all go down to the shop.
    // John, Paul, George and Ringo went to the shop.</code></span>


    // Minimal version of sprintf
    function sprintf() {
    var args = arguments,
    string = args[0],
    i = 1;
    return string.replace(/%((%)|s|d)/g, function (m) {
    // var x = string.replace(/%((%)|s|d)/g, function (m) {
    // m is the matched format, e.g. %s, %d
    var val = null;
    if (m[2]) {
    val = m[2];
    } else {
    val = args[i];
    // A switch statement so that the formatter can be extended. Default is %s
    switch (m) {
    case '%d':
    val = parseFloat(val);
    if (isNaN(val)) { val = 0; }
    break;
    }
    i++;
    }
    return val;
    });
    }
    /* */

    var noun = '', verb = '', where = 'down to the shop';

    const obj = [
    {'noun' : 'Paul', 'verb' : 'goes'},
    {'noun' : 'Paul &amp; Ringo', 'verb' : 'go'},
    {'noun' : 'Paul, Ringo and John', 'verb' : 'all go'},
    {'noun' : 'John, Paul, George &amp; Ringo', 'verb' : 'went'}
    ];
    <br/>
    for (var q=0; q&lt;obj.length; q++) {
    document.getElementById('demo').innerHTML
    += sprintf('nn%s %s %s', obj[q].noun, obj[q].verb, where);
    }
    &lt;/script&gt;

    &lt;/body&gt;
    &lt;/html&gt;
    ×

    Success!

    Help @rbrown spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...