Do not change tense within quote

80 Views Asked by At

I am using Tenseflow's change_tense method to change a present tense to past tense.

But verbs within double quotes are getting changed. They should remain the same.

Example:

  • Input: Robin will say “help” to Alexa when he encounters a problem
  • Output: Robin said “helped” to Alexa when he encountered a problem

Here "help" should not be changed.

My code:

import tenseflow

def get(self, request):
    response = {}
    sentance = request.data.get("sentance")
    if sentance:
        result = tenseflow.change_tense(sentance, "past")
        response["status_code"] = 200
        if result:
            response["past_tense"] = result
        return Response(response)

Any suggestion is highly appreciated, thanks!

1

There are 1 best solutions below

0
Aditya Kumar On BEST ANSWER

This my initial solution, I guess this is not the optimal way, but it solves the problem, any comment is welcome, Thanks!

import re
from tenseflow import change_tense as tenseflow_change_tense

def change_tense(sentence, tense, ignore_doublequote_word=False):
    """Change the tense of the sentence to the specified tense except quoted words."""
    quoted_words = []
    if ignore_doublequote_word:
        quoted_words = re.findall(r'"([^"]*)"', sentence)
    changed_sentence = tenseflow_change_tense(sentence, tense)
    if quoted_words:
        for match in re.finditer(r'"([^"]*)"', changed_sentence):
            changed_sentence = changed_sentence.replace(match.group(0), f' "{quoted_words[0]}"')
            quoted_words.pop(0)
    return changed_sentence