this post was submitted on 19 Aug 2024
3 points (100.0% liked)

Perchance - Create a Random Text Generator

549 readers
8 users here now

⚄︎ Perchance

This is a Lemmy Community for perchance.org, a platform for sharing and creating random text generators.

Feel free to ask for help, share your generators, and start friendly discussions at your leisure :)

This community is mainly for discussions between those who are building generators. For discussions about using generators, especially the popular AI ones, the community-led Casual Perchance forum is likely a more appropriate venue.

See this post for the Complete Guide to Posting Here on the Community!

Rules

1. Please follow the Lemmy.World instance rules.

2. Be kind and friendly.

  • Please be kind to others on this community (and also in general), and remember that for many people Perchance is their first experience with coding. We have members for whom English is not their first language, so please be take that into account too :)

3. Be thankful to those who try to help you.

  • If you ask a question and someone has made a effort to help you out, please remember to be thankful! Even if they don't manage to help you solve your problem - remember that they're spending time out of their day to try to help a stranger :)

4. Only post about stuff related to perchance.

  • Please only post about perchance related stuff like generators on it, bugs, and the site.

5. Refrain from requesting Prompts for the AI Tools.

  • We would like to ask to refrain from posting here needing help specifically with prompting/achieving certain results with the AI plugins (text-to-image-plugin and ai-text-plugin) e.g. "What is the good prompt for X?", "How to achieve X with Y generator?"
  • See Perchance AI FAQ for FAQ about the AI tools.
  • You can ask for help with prompting at the 'sister' community Casual Perchance, which is for more casual discussions.
  • We will still be helping/answering questions about the plugins as long as it is related to building generators with them.

6. Search through the Community Before Posting.

  • Please Search through the Community Posts here (and on Reddit) before posting to see if what you will post has similar post/already been posted.

founded 2 years ago
MODERATORS
3
submitted 5 months ago* (last edited 5 months ago) by NickMueh to c/perchance
 

So I'm trying to create a javascript that will scan the User's description/role for the following fields: Last_Name, Race, Political Entity, Rank, Position to be used in Character description/personality/instruction/role and Initial chat messages, But it wouldn't work. So I came up with a twork around where its stored in the javascript. I require help in getting a javascript code that scans User's description/role.

Original

function extractInfo(description) {
    const fields = {
        lastName: description.split('Last Name:')[1].split('\n')[0].trim(),
        race: description.split('Race:')[1].split('\n')[0].trim(),
        politicalEntity: description.split('Political Entity:')[1].split('\n')[0].trim(),
        rank: description.split('Rank:')[1].split('\n')[0].trim(),
        position: description.split('Position:')[1].split('\n')[0].trim()
    };

    return {
        getLastName: () => fields.lastName,
        getRace: () => fields.race,
        getPoliticalEntity: () => fields.politicalEntity,
        getRank: () => fields.rank,
        getPosition: () => fields.position
    };
}

const info = extractInfo(user.description);
console.log(info.getLastName()); // Should output the last name
console.log(info.getRace()); // Should output the race
console.log(info.getPoliticalEntity()); // Should output the political entity
console.log(info.getRank()); // Should output the rank
console.log(info.getPosition()); // Should output the position

Retrieve each field by calling info.getLastName(), info.getRace(), and so on. Problem is replacing user.description with the actual string containing the user's description.

Work Around

function extractInfo(description) {
    const namePattern = /(?:(?:\b\w+\s){2,})(?:\b\w+\b)/;
    const lastName = description.match(namePattern)[1];
    const racePattern = /\bRace\b\s*:\s*([\w\s]+)/;
    const race = description.match(racePattern)?.length > 0 ? description.match(racePattern)[1] : "Unknown";
    const politicalEntityPattern = /\bPolitical Entity\b\s*:\s*([\w\s]+)/;
    const politicalEntity = description.match(politicalEntityPattern)?.length > 0 ? description.match(politicalEntityPattern)[1] : "Unknown";
    const rankPattern = /\bRank\b\s*:\s*([\w\s]+)/;
    const rank = description.match(rankPattern)?.length > 0 ? description.match(rankPattern)[1] : "Unknown";
    const positionPattern = /\bPosition\b\s*:\s*([\w\s]+)/;
    const position = description.match(positionPattern)?.length > 0 ? description.match(positionPattern)[1] : "Unknown";

    return {
        lastName: <lastName>,
        race: <race>,
        politicalEntity: <politicalEntity,>
        rank: <rank>,
        position: <position>
    };
}

const updatedDescription = "<short description>";
const updatedResult = extractInfo(updatedDescription);
console.log(updatedResult);

Where you can call each field individually by accessing the properties of the returned object, like result.lastName, result.race, result.politicalEntity, and result.rank.

you are viewing a single comment's thread
view the rest of the comments
[–] perchance 1 points 5 months ago* (last edited 5 months ago) (1 children)

As a general strategy for parsing stuff out of free-form/natural language, I'd definitely recommend using the AI text generation rather than regex, since natural language is so diverse that regex will fail a lot of the time. In most cases there are too many different ways that things can be said to be able to capture them all with regex.

I assume you're talking about perchance.org/ai-character-chat custom code, and if so, you could do something like this:

function scanDescription(description) {
  let result = await oc.getInstructCompletion({ // oc.getInstructCompletion is the /ai-character-chat custom code equivalent to perchance's ai text plugin
    instruction: `Convert the following description into a JSON object like this: { lastName, race, politicalEntity, rank, position }\nDESCRIPTION: ${description}\n\nRespond with just the one-line JSON object, and nothing more.`,
    startWith: `{ "lastName":`, // make it start off with the correct format (to ensure it doesn't e.g. say "sure! I can help you with that...")
    stopSequences: ["}"], // force it to stop at the end of the JSON, just in case it doesn't stop by itself (e.g. tries to say "there you go!" or something)
  });
  return JSON.parse(result.text);
}

// Example usage:

let info = "Nick, a 49-year-old man, is from a small farming town in Iowa. His last name is Young, he is human, and he holds the rank of Captain in the local police force. His political entity is the United States.";

let result = await scanDescription(info);

console.log(result.politicalEntity);
console.log(result.lastName);
// ...

More info on custom code for perchance.org/ai-character-chat is here: https://rentry.org/82hwif

[–] NickMueh 1 points 5 months ago* (last edited 5 months ago)

Does not seem to work from the Custom JavaScript code field, if I put into the chatbox then it works after words except of course Initial chat messages