spencermountain / compromise

modest natural-language processing
http://compromise.cool
MIT License
11.49k stars 655 forks source link

Feature Request: toResponse() #1118

Closed Ocean-OS closed 4 months ago

Ocean-OS commented 5 months ago

We already have a way to detect if a group of sentences is a question. Is it possible to make a function that could take a question, and return the beginning of a response to the question? (eg "What was World War II?" becomes "World War II was").

MarketingPip commented 5 months ago

@Ocean-OS - you could... But it's the matter of how long you want to take to determine the structure of how questions can start etc as they are in various patterns... As well the patterns of what the responses should look like.....

Here is a VERY basic example of how you could do this.

import nlp from "https://esm.sh/compromise";

function generateResponse(question) {
  // Parse the question using compromise
  let doc = nlp(question);

  // Extract the main subject and verb from the question
  let questionWord = doc.match("#QuestionWord+").first().text();
  let secondPart = doc.match("(#Verb+|#Determiner+)").first().text();
  //console.log(verb)
  return (
    question.replace(questionWord, "").replace(secondPart, "").trim() +
    ` ${secondPart}`
  );
}

// Example usage
let question = "When was World War II";
let response = generateResponse(question);
console.log(response); // Output: "World War II was"

This would be a VERY tedious task. Tho I love Compromise.js and it's capabilities. I suggest you look at another tool / option if you are looking to do more than just VERY basic use cases.

Hope this answers your question.

ps; if it deal. Feel feel to close this issue to save some stress on Spencer!

spencermountain commented 5 months ago

hey ComputerGuy, yep I would start writing some templates, like jared has suggested. Could get you somewhere, if your question-answer forms were in well-understood formats:

let m = doc.match('what [<verb>(is|was)] [<subj>#Noun+]$')
if(m.found){
  return m.groups('subj').text() + ' ' + m.groups('verb').text() +' ...'
}

https://runkit.com/spencermountain/6674229afd6d9b00083261b3 but like Jared mentioned, it's also a very hard problem! cheers