Search code examples
pythontext-recognition

identify Adverbs in a sentences With python


I would like to Write a Python program to find all adverbs and their positions in a given sentence for English adverbs and French adverbs this is the code :

text = "My sister's eyes were clearly and clearly filled with affection for you."
for m in re.finditer(r"\w+ly", text):
    print('%d-%d: %s' % (m.start(), m.end(), m.group(0)))

text = "parle doucement"
for m in re.finditer(r"\w+ement", text):
    print('%d-%d: %s' % (m.start(), m.end(), m.group(0)))

except the program that I made does not take into consideration all the adverbs is there a method to do it and thank you very much


Solution

  • I would recommend using NLTK (https://www.nltk.org/)

    From this tutorial (https://www.nltk.org/book/ch05.html):

    text = word_tokenize("And now for something completely different")
    nltk.pos_tag(text)
    

    This will give you: Here we see that and is CC, a coordinating conjunction; now and completely are RB, or adverbs; for is IN, a preposition; something is NN, a noun; and different is JJ, an adjective.