chartbeat-labs / textacy

NLP, before and after spaCy
https://textacy.readthedocs.io
Other
2.21k stars 249 forks source link

How to limit subject-verb-object extraction to one output per row? #188

Open clhl opened 6 years ago

clhl commented 6 years ago

Hi,

I am trying to extract subject-verb-object triplets from my data and then attach an ID like this:

#Make dataframe with SVO extraction
count = 0;
df2 = pd.DataFrame();
for row in df1.iterrows():
  doc = nlp(unicode(row));
  text_ext = textacy.extract.subject_verb_object_triples(doc);
  tweetID = df1['id'].tolist();
  mylist = list(text_ext)
  count = count + 1;
  if (mylist):
        df2 = df2.append(mylist, ignore_index=True)
  else:
        df2=df2.append([['0','0','0']],ignore_index=True)

#Join dataframe to attach ID
df2.columns = ['Subject', 'Verb', 'Object']
df3 = pd.concat([df2, df1], axis=1)
print df3

However for some rows more than one SVO is being extracted which is throwing the output out of line with the ID.

Is there someway to overcome this (even if it is by dropping any SVOs more than the first one per row).

Thank you!