Skip to content

Commit

Permalink
avoid repetition in multirandom #196
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 9, 2023
1 parent 7b69ec0 commit f054b4c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/Basic Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Prompting is, primarily, just text input. However, there are some special option
- An entry can contain the syntax of eg `1-5` to automatically select a number from 1 to 5. For example, `<random:1-3, blue>` will give back any of: `1`, `2`, `3`, or `blue`.
- You can repeat random choices via `<random[1-3]:red, blue, purple>` which might return for example `red blue` or `red blue purple` or `blue`.
- You can use a comma at the end like `random[1-3,]` to specify the output should have a comma eg `red, blue`.
- This will avoid repetition, unless you have a large count than number of options.
- You can use the syntax `<wildcard:my/wildcard/name>` to randomly select from a wildcard file, which is basically a pre-saved text file of random options, 1 per line.
- Edit these in the UI at the bottom in the "Wildcards" tab.
- You can also import wildcard files from other UIs (ie text file collections) by just adding them into `Data/Wildcards` folder.
Expand Down
29 changes: 25 additions & 4 deletions src/Text2Image/T2IParamInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,31 @@ static T2IParamInput()
return null;
}
string separator = data.Contains("||") ? "||" : (data.Contains('|') ? "|" : ",");
string[] vals = data.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (vals.Length == 0)
string[] rawVals = data.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (rawVals.Length == 0)
{
Logs.Warning($"Random input '{data}' is empty and will be ignored.");
return null;
}
string result = "";
List<string> vals = rawVals.ToList();
for (int i = 0; i < count; i++)
{
string choice = vals[context.Random.Next(vals.Length)];
int index = context.Random.Next(vals.Count);
string choice = vals[index];
if (TryInterpretNumberRange(choice, out string number))
{
return number;
}
result += context.Parse(choice).Trim() + partSeparator;
if (vals.Count == 1)
{
vals = rawVals.ToList();
}
else
{
vals.RemoveAt(index);
}
}
return result.Trim();
};
Expand All @@ -142,13 +152,24 @@ static T2IParamInput()
Logs.Warning($"Wildcard input '{data}' does not match any wildcard file and will be ignored.");
return null;
}
WildcardsHelper.Wildcard wildcard = WildcardsHelper.GetWildcard(card);
List<string> usedWildcards = context.Input.ExtraMeta.GetOrCreate("used_wildcards", () => new List<string>()) as List<string>;
usedWildcards.Add(card);
string result = "";
List<string> vals = wildcard.Options.ToList();
for (int i = 0; i < count; i++)
{
string choice = WildcardsHelper.PickRandom(card, context.Random);
int index = context.Random.Next(vals.Count);
string choice = vals[index];
result += context.Parse(choice).Trim() + partSeparator;
if (vals.Count == 1)
{
vals = wildcard.Options.ToList();
}
else
{
vals.RemoveAt(index);
}
}
return result.Trim();
};
Expand Down

0 comments on commit f054b4c

Please sign in to comment.