Skip to content
Open

fix #64 #1618

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/pages/gen/help/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ const GenHelp = props => {
<h5 id='weights'>
Weights &ndash; <code>*</code>
</h5>
<Notice>This functionality has not yet been implemented.</Notice>
<p>
Weights can be added to certain choices using an asterisk <code>*</code>{' '}
and a number to improve the likelihood of it being chosen. For example,
Expand Down
35 changes: 28 additions & 7 deletions src/services/genService.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class GenService {
const resolveOpt = (subpatterns, option) => {
let letters = ''
for (let i = 0; i < option.length; i++) {
if (/[()[\]^*"]/.test(option[i])) {
if (/[()[\]^"]/.test(option[i])) {
// For now, ignore the characters that will be used for operations
continue
} else {
Expand All @@ -427,15 +427,36 @@ class GenService {
return letters
}

// Split all the Subpatterns into arrays based on '/'
// Parse weighted options and expand them
const parseWeightedOptions = str => {
const parts = str.split('/')
const expanded = []

for (const part of parts) {
const match = part.match(/^(.+)\*(\d+)$/)
if (match) {
const option = match[1]
const weight = Math.min(128, Math.max(1, parseInt(match[2], 10)))
for (let i = 0; i < weight; i++) {
expanded.push(option)
}
} else {
expanded.push(part)
}
}

return expanded
}

// Split all the Subpatterns into arrays based on '/' and expand weights
for (let i = 0; i < newData.subpatterns.length; i++) {
newData.subpatterns[i].subpattern = newData.subpatterns[
i
].subpattern.split('/')
newData.subpatterns[i].subpattern = parseWeightedOptions(
newData.subpatterns[i].subpattern
)
}

// Split the Pattern into an array based on '/'
const pattArr = newData.pattern.split('/')
// Split the Pattern into an array based on '/' and expand weights
const pattArr = parseWeightedOptions(newData.pattern)

// Generate the number of words requested in the settings
for (let i = 0; i < newData.words; i++) {
Expand Down