-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix failing tests on Windows * Style * Add windows-latest to workflows file * Fix workflows file
- Loading branch information
1 parent
e14d79e
commit ca6a77c
Showing
3 changed files
with
41 additions
and
30 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
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 |
---|---|---|
|
@@ -3,14 +3,30 @@ | |
|
||
from ufal.udpipe import InputFormat | ||
from ufal.udpipe import Model | ||
from ufal.udpipe import OutputFormat, ProcessingError, Sentence, Word | ||
from ufal.udpipe import OutputFormat, ProcessingError, Sentence | ||
|
||
from .utils import get_path | ||
|
||
NO_SPACE = "SpaceAfter=No" | ||
|
||
|
||
class PretokenizedInputFormat(object): | ||
def _default_model_meta(lang: str, name: str) -> Dict: | ||
return { | ||
"author": "Milan Straka & Jana Straková", | ||
"description": "UDPipe pretrained model.", | ||
"email": "[email protected]", | ||
"lang": f"udpipe_{lang}", | ||
"license": "CC BY-NC-SA 4.0", | ||
"name": name, | ||
"parent_package": "spacy_udpipe", | ||
"pipeline": [ | ||
"Tokenizer", "Tagger", "Lemmatizer", "Parser" | ||
], | ||
"source": "Universal Dependencies 2.5", | ||
"url": "http://ufal.mff.cuni.cz/udpipe", | ||
"version": "1.2.0" | ||
} | ||
|
||
|
||
class PretokenizedInputFormat: | ||
"""Dummy tokenizer for pretokenized input. | ||
Execution speed might be slow compared to other UDPipe tokenizers | ||
|
@@ -35,19 +51,19 @@ def nextSentence(self, sentence: Sentence, _: ProcessingError) -> bool: | |
line = next(self.lines) | ||
except StopIteration: | ||
return False | ||
|
||
tokens = line.split("\t") | ||
prev_word = Word() | ||
for token in tokens: | ||
num_tokens = len(tokens) | ||
for i, token in enumerate(tokens): | ||
word = sentence.addWord(token) | ||
if re.match(r"\W", token): | ||
# leave no space after previous token iff current token | ||
if i < num_tokens - 1 and re.match(r"\W", tokens[i + 1]): | ||
# leave no space after current token iff next token | ||
# is non-alphanumeric (i.e. punctuation) | ||
prev_word.misc = NO_SPACE | ||
prev_word = word | ||
word.setSpaceAfter(False) | ||
return True | ||
|
||
|
||
class UDPipeModel(object): | ||
class UDPipeModel: | ||
|
||
def __init__( | ||
self, | ||
|
@@ -64,20 +80,14 @@ def __init__( | |
path = path or get_path(lang=lang) | ||
self.model = Model.load(path) | ||
self._lang = lang.split("-")[0] | ||
self._meta = meta or {"author": "Milan Straka & Jana Straková", | ||
"description": "UDPipe pretrained model.", | ||
"email": "[email protected]", | ||
"lang": f"udpipe_{self._lang}", | ||
"license": "CC BY-NC-SA 4.0", | ||
"name": path.split("/")[-1], | ||
"parent_package": "spacy_udpipe", | ||
"pipeline": [ | ||
"Tokenizer", "Tagger", "Lemmatizer", "Parser" | ||
], | ||
"source": "Universal Dependencies 2.5", | ||
"url": "http://ufal.mff.cuni.cz/udpipe", | ||
"version": "1.2.0" | ||
} | ||
self._path = path | ||
self._meta = meta or _default_model_meta( | ||
self._lang, self._path.split("/")[-1] | ||
) | ||
|
||
def __reduce__(self): | ||
# required for multiprocessing on Windows | ||
return self.__class__, (self._lang, self._path, self._meta) | ||
|
||
def __call__( | ||
self, | ||
|