-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build custom Tokenizer and custom Processor flows for wav2vec2 models. (
#7) * Add vocab Dataclass. * Add customer processor function to trainer. * Test that we properly generate output from the vocab list. * Add both training and test sets to vocabulary for tokenizer. * Update transformers version to fix empty transcription results.
- Loading branch information
1 parent
cff43a0
commit db11fe1
Showing
11 changed files
with
411 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
*.pyc | ||
testdir | ||
|
||
# Packages | ||
*.egg | ||
|
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from elpis.models.annotation import Annotation | ||
from elpis.models.elan_options import ElanOptions, ElanTierSelector | ||
from elpis.models.vocab import VOCAB_FILE, Vocab | ||
|
||
__all__ = ["Annotation", "ElanOptions", "ElanTierSelector"] | ||
__all__ = ["Annotation", "ElanOptions", "ElanTierSelector", "Vocab", "VOCAB_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,65 @@ | ||
import json | ||
from dataclasses import dataclass | ||
from functools import reduce | ||
from pathlib import Path | ||
from typing import Any, Dict, Iterable, Optional, Set | ||
|
||
VOCAB_FILE = "vocab.json" | ||
|
||
|
||
@dataclass | ||
class Vocab: | ||
"""A class which represents a dictionary of encountered tokens in a dataset.""" | ||
|
||
vocab: Dict[str, int] | ||
|
||
@property | ||
def symbols(self) -> Set[str]: | ||
return set(self.vocab.keys()) | ||
|
||
def merge(self, other: "Vocab") -> "Vocab": | ||
"""Creates a new Vocab which includes all symbols in the merged two.""" | ||
vocab = self.symbols | other.symbols | ||
return Vocab.from_set(vocab) | ||
|
||
def save(self, path: Path) -> None: | ||
"""Saves the vocab to the supplied path. | ||
If the path is a folder, saves as vocab.json, within it. | ||
""" | ||
if path.is_dir(): | ||
path /= VOCAB_FILE | ||
|
||
with open(path, "w") as out: | ||
json.dump(self.vocab, out) | ||
|
||
def add(self, char: str) -> None: | ||
"""Adds a new character into the vocab.""" | ||
if char in self.vocab: | ||
return | ||
|
||
self.vocab[char] = len(self.vocab) | ||
|
||
def replace(self, original: str, replacement: str) -> None: | ||
"""Replaces the supplied character mapping in the vocab.""" | ||
if original not in self.vocab: | ||
return | ||
|
||
self.vocab[replacement] = self.vocab[original] | ||
self.vocab.pop(original) | ||
|
||
@classmethod | ||
def from_set(cls, symbols: Set[str]) -> "Vocab": | ||
"""Builds a vocab from a set of symbols.""" | ||
vocab = {symbol: index for index, symbol in enumerate(sorted(symbols))} | ||
return cls(vocab=vocab) | ||
|
||
@classmethod | ||
def from_strings(cls, texts: Iterable[str]) -> "Vocab": | ||
"""Builds an vocab from a iterable text collection.""" | ||
|
||
def reducer(result: Set[str], text: str) -> Set[str]: | ||
return result | set(text) | ||
|
||
symbols = reduce(reducer, texts, set()) | ||
return cls.from_set(symbols) |
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
Oops, something went wrong.