Как проверить наличие глагола, используя spacy и pandas? - PullRequest
0 голосов
/ 14 сентября 2018
import spacy, en_core_web_sm
nlp = en_core_web_sm.load()
doc = nlp(u"I will go to the mall")
chk_set = set(['VERB'])
print chk_set.issubset(t.pos_ for t in doc)

Приведенный выше код возвращает True if POS = verb.

Теперь я хочу расширить этот код, чтобы прочитать список предложений, сохраненных в листе Excel.Чтобы проверить наличие пунктуации в предложении, я могу добиться этого, используя приведенный ниже код.

Вопрос в том, как расширить этот код ниже, чтобы включить проверку глагола выше.

from pandas import read_excel
import pandas as pd
import xlsxwriter
my_sheet_name = 'Metrics' 
df = read_excel('sentence.xlsx', sheet_name = my_sheet_name)
df['.']=df['Sentence'].str.contains('.')
# df['VERB']=df['Sentence'].str.contains('.')
writer = pd.ExcelWriter('sentence.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Metrics')
writer.save()

Ожидаемый результат:

Sentence                            Verb
I will go to the mall               True
the mall                            False
I may be here tomorrow.             True  

1 Ответ

0 голосов
/ 14 сентября 2018

Вы можете сделать это, используя NLTK, как показано ниже:

import nltk
import pandas as pd

df = pd.DataFrame({'sent': ['I will go to the mall', 'the mall', 'I may be here tomorrow.']})

def tag_verb(sent):
    words = nltk.word_tokenize(sent)
    tags = nltk.pos_tag(words)
    for t in tags:
        if t[1] == 'VB':
            return True
    return False

df['verb'] = df['sent'].apply(lambda x: tag_verb(x))

Вывод:

    sent                       verb
0   I will go to the mall      True
1   the mall                   False
2   I may be here tomorrow.    True
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...