-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multilingual extension (German, Spanish, Romanian, Russian)
- Loading branch information
1 parent
6f576c6
commit 1853f53
Showing
12 changed files
with
720 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
r"""Command-line tool for running mERRANT. | ||
This script reads TSV data from stdin and writes formatted annotations to | ||
stdout. | ||
Example: | ||
echo -e "I goed to the storr.\tI went to the store." | \ | ||
python3 -m merrant.annotate | ||
Output (M2_CHAR format): | ||
S I goed to the storr. | ||
A 2 6|||R:VERB:INFL|||went|||REQUIRED|||-NONE-|||0 | ||
A 14 19|||R:SPELL|||store|||REQUIRED|||-NONE-|||0 | ||
""" | ||
|
||
import sys | ||
from typing import Sequence | ||
|
||
from absl import app | ||
from absl import flags | ||
|
||
from merrant import api | ||
from merrant import io | ||
|
||
_SPACY_MODEL = flags.DEFINE_string("spacy_model", "en_core_web_sm", | ||
"Tagging model.") | ||
|
||
_OUTPUT_FORMAT = flags.DEFINE_enum( | ||
"output_format", "M2_CHAR", ["M2_CHAR", "M2_TOK", "TSV_TAGGED_CORRUPTION"], | ||
"Tagging model.") | ||
|
||
|
||
def main(argv: Sequence[str]) -> None: | ||
if len(argv) > 1: | ||
raise app.UsageError("Too many command-line arguments.") | ||
|
||
annotator = api.Annotator(_SPACY_MODEL.value, | ||
aspell_lang=_SPACY_MODEL.value[:2]) | ||
annotator.initialize() | ||
formatter = io.make_formatter(_OUTPUT_FORMAT.value) | ||
for line in sys.stdin: | ||
parts = line.strip("\n").split("\t") | ||
annotation = annotator.annotate(parts[0], parts[1:]) | ||
print(formatter.format(annotation).decode("utf-8")) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(main) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Main API for mERRANT.""" | ||
|
||
from typing import Optional, Sequence | ||
|
||
from merrant import classification | ||
from merrant import utils | ||
|
||
|
||
class Annotator: | ||
"""Main interface to mERRANT. | ||
Example usage: | ||
annotator = api.Annotator("en_core_web_sm-3.0.0a1", aspell_lang="en") | ||
annotator.initialize() | ||
annotation = annotator.annotate("I goed to the storr." | ||
["I went to the store."]) | ||
The returned `utils.Annotation` contains tagged `utils.EditsSpans`. If | ||
`aspell_lang` is not set, no spell checker will be used. Edits can still be | ||
classified as `SPELL` based on character Levenshtein distance. | ||
""" | ||
|
||
def __init__(self, spacy_model: str, aspell_lang: Optional[str] = None): | ||
self._spacy_model = spacy_model | ||
self._aspell_lang = aspell_lang | ||
self._initialized = False | ||
self._nlp = None | ||
self._classifier = None | ||
|
||
def initialize(self): | ||
"""Initialized the interface. Must be called before `annotate()`.""" | ||
self._nlp = utils.load_spacy_from_google3(self._spacy_model) | ||
self._classifier = classification.GenericClassifier( | ||
aspell_lang=self._aspell_lang) | ||
self._classifier.initialize() | ||
self._initialized = True | ||
|
||
def annotate(self, source_sentence: str, | ||
target_sentences: Sequence[str]) -> utils.Annotation: | ||
"""Annotates the edits between a source- and a set of target sentences. | ||
Args: | ||
source_sentence: Untokenized source (original) sentence. | ||
target_sentences: A list of untokenized target (corrected) sentences. | ||
Returns: | ||
An `utils.Annotation` with tagged edit spans. | ||
""" | ||
if not self._initialized: | ||
raise ValueError("Annotator not initialized.") | ||
|
||
if isinstance(target_sentences, str): | ||
raise ValueError("target_sentences must be a list, not a string.") | ||
|
||
source_doc = self._nlp(source_sentence) | ||
annotation = utils.Annotation(source_doc=source_doc) | ||
for target_sentence in target_sentences: | ||
target_doc = self._nlp(target_sentence) | ||
annotation.target_sentences.append( | ||
utils.TargetSentence( | ||
doc=target_doc, | ||
edit_spans=self._classifier.classify(source_doc, target_doc))) | ||
return annotation | ||
|
Oops, something went wrong.