# any efficient way to find surrounding adjective/verbs with respect to the target phrase in python [updated]?

I am doing sentiment analysis on given documents. My goal is to find out the closest or surrounding adjective words with respect to the target phrase in my sentences. I do have an idea how to extract surrounding words with respect to target phrases. But how do I find out relatively close or closest adjective or NNP or VBN or other POS tag with respect to target phrase ?

Here is the sketch idea of how I may get surrounding words with respect to my target phrase.

sentence_List = {
"Obviously one of the most important features of any computer is the human interface.",
"Good for everyday computing and web browsing.",
"My problem was with DELL Customer Service",
"I play a lot of casual games online[comma] and the touchpad is very responsive"
}

target_phraseList = {
"human interface",
"everyday computing",
"DELL Customer Service",
}


Note that my original dataset was given as DataFrame where the list of the sentences and respective target phrases were given. Here I just simulated data as follows:

import pandas as pd
df=pd.Series(sentence_List, target_phraseList)
df=pd.DataFrame(df)


Here, I tokenize the sentence as follow:

from nltk.tokenize import word_tokenize
tokenized_sents = [word_tokenize(i) for i in sentence_List]
tokenized=[i for i in tokenized_sents]


Then I try to find out surrounding words with respect to my target phrases by using this loot at here. However, I want to find out relatively closer or closest adjective, or verbs or VBN with respect to my target phrase.

How can I make this happen? Any idea to get this done? Thanks

• Take a look at spacy, they have models already trained to POS tag a text. – skd Nov 20 '18 at 17:13

POS-tagging consist of qualifying words by attaching a Part-Of-Speech to it. Part-Of-Speech is a tag that indicates the role of a word in a sentence (e.g. a noun, a transitive verb, a comparative adjective, etc.). You need this to know if a word is an adjective, and it is easily done with the nltk package you are using [source]:

>> nltk.pos_tag("The grand jury")
>> ('The', 'AT'), ('grand', 'JJ'), ('jury', 'NN')


Here, JJ means "Adjective" and "NN" means "Common Noun".

In your case, you are interested in neighbors adjectives. Does that mean "the closest adjective" in the sentence ? Or adjectives within a radius of the target, if any? Depending on the definition, the way to do it differs.

For adjectives within a radius, as you have already selected words within a radius using the snippet you mentioned, you can POS-tag them and then select only those with a tag that indicates an adjective tag.

>> adjective_tags = ["JJ", "JJR", "JJS"]
>> close_adjectives_list = [a[0] for a in nltk.pos_tag(" ".join(close_words_list)) if a[1] in adjective_tags ]


You can look at this answer that list most of existing POS-tags.