replace() function doesn't replace specified text
See original GitHub issueAs 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:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!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!