-1

I am not sure what would I call this but I am trying to get a collection of substrings from a string. What I mean by that is, let's say I have a string, draft question and If I pass it in the function it will return me something like

["", "d", "dr", "dra", "draf", "draft", "draft ", "draft q", "draft qu", "draft que", "draft ques", "draft quest", "draft questi", "draft questio", "draft question", "q", "qu", "que", "ques", "quest", "questi", "questio", "question", "question ", "question d", "question dr", "question dra", "question draf", "question draft"]

I wrote a function which giving me a some extend to what I want but not exactly. Any idea how do I do this? Is it possible by regex? Please note that it can any number of words. Like generateKeywords('put returns between paragraphs').

var createKeywords = (name) => {
  const arrName = [];
  let curName = '';
  name.split('').forEach((letter) => {
    curName += letter;
    arrName.push(curName);
  });
  return arrName;
};
var generateKeywords = (names) => {
  const [first, middle, last = '', sfx] = names;
  const suffix = sfx && sfx.length > 0 ? ` ${sfx}.` : '';
  const keywordNameWithoutMiddleName = createKeywords(`${first} ${last}${suffix}`);
  const keywordFullName = createKeywords(`${first} ${middle} ${last}${suffix}`);
  const keywordLastNameFirst = createKeywords(`${last}, ${first} ${middle}${suffix}`);
  const middleInitial = middle.length > 0 ? ` ${middle[0]}.` : '';
  const keywordFullNameMiddleInitial = createKeywords(`${first}${middleInitial} ${last}${suffix}`);
  const keywordLastNameFirstMiddleInitial = createKeywords(`${last}, ${first} ${middleInitial}${suffix}`);
  return [
    ...new Set([
      '',
      ...keywordFullName,
      ...keywordLastNameFirst,
      ...keywordFullNameMiddleInitial,
      ...keywordLastNameFirstMiddleInitial,
      ...keywordNameWithoutMiddleName
    ])
  ];
};
Subhendu Kundu
  • 3,618
  • 6
  • 26
  • 57
  • You specify "a word" - usually means one, but your string is two words. What about if you expand this now to 3 or more words, would you expect it to output each possibility for all of the words? There has to be a limit or your program will crash. – ctwheels Nov 19 '19 at 18:29
  • Can you elaborate a bit on your goal? E.g., "raft" is not a wanted result? Also, "question draft" is not a substring of "draft question". – ASDFGerte Nov 19 '19 at 18:30
  • sorry about that, yes it will be a string. Which can have few words to it. – Subhendu Kundu Nov 19 '19 at 18:32
  • @SubhenduKundu what happens in the case of `here are four words`? – ctwheels Nov 19 '19 at 18:33
  • So this a technique I want to use to make keywords which will help me to get the content. Also we can limit to 10 so far now. Will it be possible? – Subhendu Kundu Nov 19 '19 at 18:34
  • @SubhenduKundu it depends, when you say 10 words, do you expect each configuration as well? `1 2 3 4; 1 2 4 3; 1 3 2 4; 1 3 4 2; 1 4 2 3; 1 4 3 2; 2 1 3 4; etc.` – ctwheels Nov 19 '19 at 18:35
  • Just to give you an idea, if you had 10 words, and assuming an average of 5 characters per word, your function would have to generate around 180 million strings. That's very inefficient and I would suggest you find another way to do what you're trying to accomplish – ctwheels Nov 19 '19 at 18:42
  • Umm, i see what you saying, it would be too complex but the more combination i have the better or easier to find the word using the keywords. `["", "h", "he", "her", "here", "here ", "here a", "here ar", "here are", "here are ", "here are f", "here are fo", "here are fou", "here are four", "here are four ", "here are four w", "here are four wo", "here are four wor", "here are four word", "here are four words", "here are four words.", "a", "ar", "are", "are " ......]` with probable combination of `are four words here`. then `four words here are` and last `words here are four`. Something like. – Subhendu Kundu Nov 19 '19 at 18:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202669/discussion-between-subhendu-kundu-and-ctwheels). – Subhendu Kundu Nov 19 '19 at 18:45

1 Answers1

1

The posted createKeywords function is usable as it is for the creation of the substrings of length 1 to length of the full string. The posted generateKeywords function is way to byzantine for the general task, which involves generating the permutations of the words in the string. Fortunately, a suitable permute function is found in an answer to the question Recursively print all permutations of a string (Javascript), so I took it from there. Therewith, generateKeywords becomes quite simple:

var createKeywords = (name) => {
  const arrName = [];
  let curName = '';
  name.split('').forEach((letter) => {
    curName += letter;
    arrName.push(curName);
  });
  return arrName;
};

function *permute(a, n = a.length) {
  if (n <= 1) yield a.slice();
  else for (let i = 0; i < n; i++) {
    yield *permute(a, n - 1);
    const j = n % 2 ? 0 : i;
    [a[n-1], a[j]] = [a[j], a[n-1]];
  }
}

var generateKeywords = (names) =>
{
  collect = new Set([''])
  for (mutation of permute(names.split(" ")))
    for (substring of createKeywords(mutation.join(" "))) collect.add(substring)
  return [...collect]
};

console.log(generateKeywords('draft question'))
Armali
  • 18,255
  • 14
  • 57
  • 171