Я написал часть функции, которая использует тегер NER SpaCy (только встроенные сущности, а не пользовательские), и он выполняет свою работу, но с точки зрения времени это чрезвычайно обременительно, особенно потому, что родительская программа должна использовать его напочти 400 000 строк данных. Большой блок утверждений elif особенно обременителен.
Это то, что я написал до сих пор.
# From this point, this part of the function is only utilized based
# on specific criteria matched above this line, which in this case is a string such as:
raw_string = "I'm heading to the store at around 8:30 to pick up some tannerite and other things before I go to the range"
ana = analyzer.polarity_scores(raw_string)
sentiment = ana['compound']
if (raw_string not in filt_tup) or (sentiment < -0.8):
doc = nlp(raw_string)
chunk = [(X.text, X.label_) for X in doc.ents]
person_list = set()
org_list = set()
nat_rel_list = set()
geo_pol_list = set()
obj_list = set()
date_list = set()
for i in chunk:
if i[1] == 'PERSON' and i[0] not in junk_terms + junk_names:
person_list.add(('Person: ', i[0]))
elif i[1] == 'ORG' and i[0] not in junk_terms + junk_orgs:
org_list.add(('Organization: ', i[0]))
elif i[1] == 'NORP' and i[0] not in junk_terms:
nat_rel_list.add(('Nationalities or religious or political groups: \n', i[0]))
elif i[1] == 'GPE' and i[0] not in junk_terms:
geo_pol_list.add(('Countries, cities, states: \n', i[0]))
elif i[1] == 'PRODUCT' and i[0] not in junk_terms:
obj_list.add(('Specific objects: \n', i[0]))
elif i[1] == 'DATE' and i[0] not in junk_terms + junk_orgs:
date_list.add(('Specific Dates: ', i[0]))
matched = True
print(matched, '\n', raw_string, '\n', sentiment, '\n', 'Entities:', '\n',
person_list, org_list, obj_list, nat_rel_list, geo_pol_list, date_list)
master_list = [matched, sentiment, raw_string, rule,
person_list, org_list, obj_list, nat_rel_list, geo_pol_list, date_list]