Reputation: 1394
I'm new to the world of NLP but would like to know if there's currently any easy way (using a service or OSS etc) of changing the subject of a bulk of text using NLP where the original subject is known (and preferably if such a method is available in multiple languages?) (Equivalent to the method sentences().toPastTense() detailed / available here: https://nlp-compromise.github.io )
Lets say that the original text is written about "you", and you know that to always be the case, but you want to automatically generate a version of the text that would be changed to be about "your brother"
(quite nonsensical) example:
"You should go down the hall, as you reach the corner you are done."
Would turn into
"Your brother should go down the hall, as he reaches the corner he is done."
As far as I understand, this type of text transformation is reliant on lemmatisation (as demonstrated in this post https://towardsdatascience.com/introduction-to-natural-language-processing-for-text-df845750fb63), but as I've been researching text transformation methods I haven't seen any methods pertaining to the subject of sentences?
Upvotes: 0
Views: 350
Reputation: 1199
I don't know of any off hand, but it can certainly be done. For example, using TextBlob you could try to come up with a function using parts-of-speech. Obviously you'd need more than this little snippet here, like a function that checks subject/verb agreement, but it's an example of an approach, and hopefully it's food for thought.
from textblob import TextBlob
from textblob.taggers import NLTKTagger
from textblob import Word
def lil_subj_replacer(phrase,input_subj,input_prp):
nltk_tagger = NLTKTagger()
blob = TextBlob(phrase,pos_tagger=nltk_tagger)
subject = True
for i,keyval in enumerate(blob.pos_tags):
key = keyval[0]
value = keyval[1]
if (value == 'PRP'):
if subject:
blob.words[i] = input_subj
subject = False
else:
blob.words[i] = input_prp
blob.words[i+1] = Word(blob.words[i+1]).lemmatize('v')
return ' '.join(blob.words)
my_phrase = 'You should go down the hall, as you reach the corner you are done.'
print(my_phrase)
print(lil_subj_replacer(phrase=my_phrase,input_subj='Your brother',input_prp='he'))
original: You should go down the hall, as you reach the corner you are done.
no lemmatize: Your brother should go down the hall as he reach the corner he are done
lemmatized verbs: Your brother should go down the hall as he reach the corner he be done
edit: added example with lemmaz, since you mentioned it.
Upvotes: 1