Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mention_extractor.apply with clear=True fails if it's not the first run #424

Merged
merged 3 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Fixed
^^^^^
* `@kaikun213`_: Fix bug in table range difference calculations.
(`#420 <https://github.com/HazyResearch/fonduer/pull/420>`_)
* `@HiromuHota`_: mention_extractor.apply with clear=True now works even if it's not the first run.
(`#424 <https://github.com/HazyResearch/fonduer/pull/424>`_)

0.8.2_ - 2020-04-28
-------------------
Expand Down
3 changes: 2 additions & 1 deletion src/fonduer/candidates/mentions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from fonduer.candidates.models.temporary_context import TemporaryContext
from fonduer.parser.models import Context, Document, Sentence
from fonduer.utils.udf import UDF, UDFRunner
from fonduer.utils.utils import get_dict_of_stable_id

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -566,7 +567,7 @@ def apply(self, doc: Document, **kwargs: Any) -> Document:
:param doc: A document to process.
"""
# Get a dict of stable_id of contexts.
dict_of_stable_id: Dict[str, Context] = {}
dict_of_stable_id: Dict[str, Context] = get_dict_of_stable_id(doc)

# Iterate over each mention class
for i, mention_class in enumerate(self.mention_classes):
Expand Down
28 changes: 27 additions & 1 deletion src/fonduer/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from builtins import range
from typing import TYPE_CHECKING, Dict, Iterator, List, Set, Tuple, Type, Union

from fonduer.parser.models import Document, Sentence
from fonduer.parser.models import Context, Document, Sentence

if TYPE_CHECKING: # to prevent circular imports
from fonduer.candidates.models import Candidate
Expand Down Expand Up @@ -64,3 +64,29 @@ def get_set_of_stable_ids(
)
)
return set_of_stable_ids


def get_dict_of_stable_id(doc: Document) -> Dict[str, Context]:
"""Return a mapping of a stable_id to its context."""
return {
doc.stable_id: doc,
**{
c.stable_id: c
for a in [
"sentences",
"paragraphs",
"captions",
"cells",
"tables",
"sections",
"figures",
]
for c in getattr(doc, a)
},
**{
c.stable_id: c
for s in doc.sentences
for a in ["spans", "implicit_spans"]
for c in getattr(s, a)
},
}
6 changes: 6 additions & 0 deletions tests/e2e/test_incremental.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def test_incremental():
assert session.query(Part).count() == 11
assert session.query(Temp).count() == 8

# Test if clear=True works
mention_extractor.apply(docs, parallelism=PARALLEL, clear=True)

assert session.query(Part).count() == 11
assert session.query(Temp).count() == 8

# Candidate Extraction
PartTemp = candidate_subclass("PartTemp", [Part, Temp])

Expand Down