skip to Main Content

I receive text from response:

"Generate something in 1:1 format for Birthday in an cheerful, celebratory mood. The image should be created in an Graffiti style. The text Happy Birthday! is at the top. At the bottom is the lettering: Liam. Graphic elements should visually reflect the terms stars, balloons."

and have string without values ready:

const sub = "Generate something in 1:1 format for in an mood. The image should be created in an style. The text is at the top. At the bottom is the lettering:. Graphic elements should visually reflect the terms.";

In every response words can differ, for example Birthday->Thank You, Liam->John Doe, stars, balloons->fireworks and many other combinations.

Also, basic strings, prompt and sub can be in different languages.

Is there a way to extract marked words (which will be used to prefil some input fields)?

I managed to do that only if the words are quoted like:

const sub = "Generate something in 1:1 format for in an mood. The image should be created in an style. The text is at the top. At the bottom is the lettering:. Graphic elements should visually reflect the terms."

const prompt = "Generate something in 1:1 format for 'Birthday' in an 'cheerful, celebratory' mood. The image should be created in an 'Graffiti' style. The text 'Happy Birthday!' is at the top. At the bottom is the lettering: 'Liam'. Graphic elements should visually reflect the terms 'stars, balloons'."

const resolvePrompt = (prompt, sub) => {
  return prompt
    .replaceAll(".", "")
    .split(" ")
    .filter((wd) => !sub.includes(wd))
    .join("")
    .split("''");
};
  
console.log(resolvePrompt(prompt, sub))

2

Answers


  1. You just need to use few regex, if you are new, you can ask AI to make it for you.

    Here:

    • We are using (.+?) where .+ together matches one or more than one character in sequence until default pattern follows, ? makes matching less greedy,i.e it will match least possible until it gets back to track.
    • .match(regex) return corresponding matches from which we extract content in order
    const responseText = "Generate something in 1:1 format for Birthday in an cheerful, celebratory mood. The image should be created in an Graffiti style. The text Happy Birthday! is at the top. At the bottom is the lettering: Liam. Graphic elements should visually reflect the terms stars, balloons."; // or use input
    
    
    const sub = "Generate something in 1:1 format for in an mood. The image should be created in an style. The text is at the top. At the bottom is the lettering:. Graphic elements should visually reflect the terms."; // Template string
    
    // You may extend this to full match,though it is unnecessary, like matching here is done only in necessary sentance,not from start of paragraph
    const regex = /for (.+?) in an (.+?) mood. The image should be created in an (.+?) style. The text (.+?) is at the top. At the bottom is the lettering: (.+?). Graphic elements should visually reflect the terms (.+?)./;
    
    const matches = responseText.match(regex);
    
    if (matches) {
      const [fullMatch, occasion, mood, style, textTop, textBottom, terms] = matches;
      console.log({
        occasion,
        mood,
        style,
        textTop,
        textBottom,
        terms
      });
    }
    Login or Signup to reply.
  2. If my guess is right, I think the problem could be solved like this:

    const variablePattern = /'([wds,.;!?\-+*=/_[](){}@#$%&]*)'/g;
    
    function calculateVariableString ( thePrompt )
    {
      let theIndex = 0;
      
      const variableMatches = thePrompt.matchAll(variablePattern);
      
      for ( const [ theVariable, theContent ] of variableMatches )
      {
        thePrompt = thePrompt.replace(theVariable, `'${${theIndex++}}'`);
      }
      
      return thePrompt;
    }
    
    function parseVariableString ( thePrompt, theKeyValuePairs )
    {
      for ( const theIndex in theKeyValuePairs )
      {
        thePrompt = thePrompt.replace(`${${theIndex}}`, theKeyValuePairs[theIndex]);
      }
      
      return thePrompt;
    }
    
    // TESTS
    
    var thePrompt = "Generate something in 1:1 format for 'Birthday' in an 'cheerful, celebratory' mood. The image should be created in an 'Graffiti' style. The text 'Happy Birthday!' is at the top. At the bottom is the lettering: 'Liam'. Graphic elements should visually reflect the terms 'stars, balloons'.";
    
    var newWords = ['Marriage', 'crazy, frenzy', 'Neon', 'CRAZIEST', 'WEDDING', 'dance, music, popularity, wetty'];
    
    var variablePrompt = calculateVariableString(thePrompt);
    
    var parsedPrompt = parseVariableString(variablePrompt, newWords);
    
    
    // LOGS
    
    console.log('newWords =', newWords);
    console.log('thePrompt =', thePrompt);
    console.log('variablePrompt =', variablePrompt);
    console.log('parsedPrompt =', parsedPrompt);
    
    // MORE TESTS
    
    var variableText = "This is ${Name}, ${Pronoun} works at the ${Workplace} near ${Nearplace}.";
    var variableMap =
    {
      Name: 'Anonymous',
      Pronoun: 'he',
      Workplace: 'bank',
      Nearplace: 'my house',
    }
    
    var parsedText = parseVariableString(variableText, variableMap);
    
    // MORE LOGS
    
    console.log('variableMap =', variableMap);
    console.log('variableText =', variableText);
    console.log('parsedText =', parsedText);

    NOTE that it would be useful to add as many punctuations in the following pattern as possible:

    const variablePattern = /'([wds,.;!?\-+*=/_[](){}@#$%&]*)'/g;
    

    IMPORTANT: the parseVariableString can use both an array or an object as theKeyValuePairs parameter.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search