-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from ku-nlp/add-whitespace-token
Add whitespace token
- Loading branch information
Showing
38 changed files
with
808 additions
and
989 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
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
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import logging | ||
from argparse import ArgumentParser | ||
from collections import Counter | ||
from pathlib import Path | ||
from typing import Dict | ||
|
||
from rhoknp import Document | ||
from transformers import AutoTokenizer | ||
|
||
from kwja.utils.kanjidic import KanjiDic | ||
from kwja.utils.reading_prediction import ReadingAligner | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def main(): | ||
parser = ArgumentParser() | ||
parser.add_argument("-m", "--model-name-or-path", type=str, help="model_name_or_path") | ||
parser.add_argument("-k", "--kanji-dic", type=str, help="path to kanji dic file") | ||
parser.add_argument("-i", "--in-dir", type=Path, help="path to input directory") | ||
args = parser.parse_args() | ||
|
||
tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path) | ||
kanji_dic = KanjiDic(args.kanji_dic) | ||
reading_aligner = ReadingAligner(tokenizer, kanji_dic) | ||
|
||
reading_counter: Dict[str, int] = Counter() | ||
for path in args.in_dir.glob("**/*.knp"): | ||
logger.info(f"processing {path}") | ||
with path.open() as f: | ||
document = Document.from_knp(f.read()) | ||
try: | ||
for reading in reading_aligner.align(document.morphemes): | ||
reading_counter[reading] += 1 | ||
except ValueError: | ||
logger.warning(f"skip {document.doc_id} for an error") | ||
for subreading, count in sorted( | ||
sorted(reading_counter.items(), key=lambda pair: pair[0]), key=lambda pair: pair[1], reverse=True | ||
): | ||
print(f"{subreading}\t{count}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.