Skip to content

Commit

Permalink
Merge pull request #1 from pzzhang/fix_pronoun_and_of
Browse files Browse the repository at this point in the history
update parser and test examples
  • Loading branch information
pzzhang committed Jan 22, 2024
2 parents 2825dba + ab4be2e commit a29c8c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions example/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def main():
demo('The woman is a pianist.')
demo('A giraffe grazing a tree in the wildness with other wildlife.')
demo('Cow standing on sidewalk in city area near shops.')
demo('She is playing the piano.') # wrong, contain "she"
demo('A woman is next to a piano.') # wrong
demo('A woman is in front of a piano.') # wrong
demo('A woman is standing next to a piano.') # correct
demo('A bowl of rice, noodle and grains.') # wrong

print('Input your own sentence. Type q to quit.')
while True:
Expand Down
16 changes: 12 additions & 4 deletions sng_parser/backends/spacy_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def parse(self, sentence, doc=None, return_doc=False):
entity_chunks = list()
for entity in doc.noun_chunks:
# Ignore pronouns such as "it".
if entity.root.lemma_ == '-PRON-':
if entity.root.pos_ == "PRON":
continue

ent = dict(
Expand Down Expand Up @@ -174,14 +174,14 @@ def dfs(node):
'relation': entity.root.head.text,
'lemma_relation': entity.root.head.lemma_
}
# E.g., A [woman] in front of a [piano].
# E.g., A [woman] (is) in front of a [piano].
elif (
entity.root.head.head.dep_ == 'pobj' and
database.is_phrasal_prep(doc[entity.root.head.head.head.i:entity.root.head.i + 1].text.lower())
):
fake_noun_marks.add(entity.root.head.head.i)
relation = {
'subject': entity.root.head.head.head.head.i,
'subject': relation_subj[entity.root.head.head.head.head.i] if entity.root.head.head.head.head.pos_ == 'AUX' else entity.root.head.head.head.head.i,
'object': entity.root.i,
'relation': doc[entity.root.head.head.head.i:entity.root.head.i + 1].text,
'lemma_relation': doc[entity.root.head.head.head.i:entity.root.head.i].lemma_
Expand All @@ -202,6 +202,14 @@ def dfs(node):
'relation': entity.root.head.head.text + ' ' + entity.root.head.text,
'lemma_relation': entity.root.head.head.lemma_ + ' ' + entity.root.head.lemma_
}
# E.g., A [piano] is next to a [woman].
elif entity.root.head.head.dep_ == 'acomp' and entity.root.head.head.head.pos_ == 'AUX':
relation = {
'subject': relation_subj[entity.root.head.head.head.i],
'object': entity.root.i,
'relation': entity.root.head.head.text + ' ' + entity.root.head.text,
'lemma_relation': entity.root.head.head.lemma_ + ' ' + entity.root.head.lemma_
}
# E.g., A [woman] standing next to a [piano].
elif entity.root.head.head.dep_ in ('amod', 'advmod') and entity.root.head.head.head.pos_ == 'VERB' and entity.root.head.head.head.i in relation_subj:
relation = {
Expand Down Expand Up @@ -272,5 +280,5 @@ def __flatten_conjunction(node):
yield node
for c in node.children:
if c.dep_ == 'conj':
yield c
yield from SpacyParser.__flatten_conjunction(c)

0 comments on commit a29c8c1

Please sign in to comment.