question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

replace() function doesn't replace specified text

See original GitHub issue

As a continuation of the project I referenced in #568 I’ve been trying to improve handling for certain input types. For the current case, I’m trying to convert nouns that can also be verbs into past tense verbs, but the replace() function doesn’t seem to work in this case when I would expect it to.

const nlp = require('compromise'); // tslint:disable-line

const examples = [
  ['eat a cookie', 'Have you eaten a cookie today?'],
  ['take a shower', 'Have you taken a shower today?'],
  ['watch the movie', 'Have you watched a movie today?'],
  ['smell a flower', 'Have you smelled a flower today?'],
  ['beat an egg', 'Have you beaten an egg today?'], //participle working
  ['shower', 'Have you showered today?'],
];

function toParticiple(action: string): string {
  const doc = nlp(action);
  let verb = doc.verbs(0);

  if (verb.length === 0) {
    // If there's only a noun, convert it to a verb gerund form
    doc.nouns(0).tag('Verb');
    verb = doc.verbs(0);
  }

  // If there's a verb, conjugate it
  const conjugations = verb.conjugate()[0]; //use the Participle, or the PastTense
  if (conjugations) {
    const form = conjugations.Participle || conjugations.PastTense;
    doc.match(verb).replace(form);
  }

  const parsed = doc
    .sentences()
    .prepend('Have you')
    .append('today')
    .toQuestion()
    .normalize();

  const outText: string = parsed.out();
  return outText.charAt(0).toUpperCase() + outText.slice(1);
}

function runParticipleTest(input: string, expectation: string): void {
  console.log('Test case');
  console.log(`\tE: ${expectation}`);
  console.log(`\tO: ${toParticiple(input)}`);
}

examples.map(([input, expectation]) => runParticipleTest(input, expectation));

The current code that I’ve been using to test looks like this; not all examples are working 100% perfectly yet, but generally replacement does work except in the last case where the input is exclusively “shower”. Not sure what’s happening here, exactly, but it’s possible that I’m misusing the API–some clarification would be great if possible.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
moberemkcommented, Mar 8, 2019

No worries about the delay man, I always appreciate your answers–no rush.

The core issue I’m running in to is that shower doesn’t get replaced with “showered” in the text. You can run this script to test it; shower is the only case where it breaks, which is why I was focusing on it.

I’ll give it a try with replaceWith after I get back from lunch, see if that makes a difference!

0reactions
moberemkcommented, Mar 14, 2019

Sounds good; if there’s any way I could help improve the replace() function to speed along that release (maybe written up in an issue somewhere?) I’d be happy to take a look!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replace function not replacing text - python - Stack Overflow
So a template mad libs text file containing: The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected...
Read more >
Find and replace text - Microsoft Support
Go to Home > Replace. Enter the word or phrase you want to replace in Find what. Enter your new text in Replace...
Read more >
How to Replace Text in Excel with the REPLACE function (2022)
The REPLACE function substitutes a text string with another text string. Learn all the steps here + a bonus method (sample file included)....
Read more >
Change Specific Text within a Cell in Excel - TeachExcel.com
Change or replace text in a cell with other text you can replace a single character ... SUBSTITUTE() Function - Change Specific Text...
Read more >
Using Excel REPLACE and SUBSTITUTE functions - Ablebits
The SUBSTITUTE function in Excel replaces one or more instances of a given character or text string with a specified character(s).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found