splintering 1.0.0__tar.gz → 1.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {splintering-1.0.0 → splintering-1.0.2}/PKG-INFO +12 -10
- {splintering-1.0.0 → splintering-1.0.2}/README.md +9 -8
- {splintering-1.0.0 → splintering-1.0.2}/pyproject.toml +3 -2
- {splintering-1.0.0 → splintering-1.0.2}/scripts/demo.py +15 -17
- splintering-1.0.2/scripts/experiments.py +14 -0
- splintering-1.0.2/scripts/hebrew/cognitive_evaluation.py +22 -0
- {splintering-1.0.0 → splintering-1.0.2}/scripts/hebrew/compare_vocabs.py +18 -20
- splintering-1.0.2/scripts/hebrew/compare_vocabs_for_dicta_bert.py +20 -0
- splintering-1.0.2/scripts/hebrew/trigram_lm.py +9 -0
- {splintering-1.0.0 → splintering-1.0.2}/scripts/logger.py +5 -4
- splintering-1.0.2/scripts/main.py +84 -0
- splintering-1.0.2/scripts/params.py +88 -0
- {splintering-1.0.0 → splintering-1.0.2}/scripts/train_tokenizer.py +5 -7
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/experiments/cognitive_evaluation.py +11 -12
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/experiments/compare_vocabs.py +3 -3
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/experiments/compare_vocabs_for_dicta_bert.py +5 -5
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/experiments/trigram_lm.py +7 -7
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/trainer.py +16 -20
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/utils/corpus_tools.py +20 -22
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/utils/languages.py +16 -11
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/utils/logger.py +3 -3
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/utils/misc.py +11 -11
- splintering-1.0.2/src/splintering/utils/paths.py +76 -0
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/utils/static_checks.py +6 -6
- splintering-1.0.0/scripts/hebrew/cognitive_evaluation.py +0 -25
- splintering-1.0.0/scripts/hebrew/compare_vocabs_for_dicta_bert.py +0 -26
- splintering-1.0.0/scripts/hebrew/trigram_lm.py +0 -15
- splintering-1.0.0/scripts/main.py +0 -82
- splintering-1.0.0/scripts/params.py +0 -90
- splintering-1.0.0/src/splintering/utils/path_utils.py +0 -62
- {splintering-1.0.0 → splintering-1.0.2}/.github/workflows/python-publish.yml +0 -0
- {splintering-1.0.0 → splintering-1.0.2}/.gitignore +0 -0
- {splintering-1.0.0 → splintering-1.0.2}/LICENSE +0 -0
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/__init__.py +0 -0
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/experiments/__init__.py +0 -0
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/text_processors.py +0 -0
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/utils/__init__.py +0 -0
- {splintering-1.0.0 → splintering-1.0.2}/src/splintering/utils/format_static_checks_results_file.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: splintering
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.2
|
|
4
4
|
Summary: Splinter is a text pre-processing step which rearranges characters into a linear form that better represents nonconcatenative morphologies, enabling meaningful contiguous segments to be found by a subword tokenizer.
|
|
5
5
|
Project-URL: Source, https://github.com/MeLeLBGU/Splintering
|
|
6
6
|
Project-URL: Issues, https://github.com/MeLeLBGU/Splintering/issues
|
|
@@ -11,8 +11,9 @@ Classifier: Operating System :: OS Independent
|
|
|
11
11
|
Classifier: Programming Language :: Python :: 3
|
|
12
12
|
Requires-Python: >=3.10
|
|
13
13
|
Requires-Dist: datasets
|
|
14
|
+
Requires-Dist: langcodes[data]
|
|
14
15
|
Requires-Dist: nltk
|
|
15
|
-
Requires-Dist: numpy
|
|
16
|
+
Requires-Dist: numpy
|
|
16
17
|
Requires-Dist: openpyxl~=3.1.5
|
|
17
18
|
Requires-Dist: pandas
|
|
18
19
|
Requires-Dist: scikit-learn
|
|
@@ -35,17 +36,18 @@ The following example trains Splinter on Hebrew Wikipedia, encodes the training
|
|
|
35
36
|
# Training:
|
|
36
37
|
from splintering.trainer import SplinterTrainer
|
|
37
38
|
from splintering.text_processors import TextProcessorWithEncoding
|
|
38
|
-
from splintering.utils.languages import LanguageUtilsFactory
|
|
39
|
-
from splintering.utils.
|
|
39
|
+
from splintering.utils.languages import LanguageUtilsFactory, Language
|
|
40
|
+
from splintering.utils.paths import SplinterPaths
|
|
40
41
|
from splintering.utils.misc import get_corpus_name
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
folder = SplinterPaths("example")
|
|
44
|
+
language = Language.find("Hebrew")
|
|
43
45
|
train_dataset_path = 'wikimedia/wikipedia'
|
|
44
46
|
train_dataset_name = f'20231101.{language}'
|
|
45
47
|
language_utils = LanguageUtilsFactory.get_by_language(language)
|
|
46
48
|
|
|
47
49
|
# - Create the reductions map, and map the reductions in it into new Unicode characters
|
|
48
|
-
splinter_trainer = SplinterTrainer(language_utils)
|
|
50
|
+
splinter_trainer = SplinterTrainer(language_utils, folder)
|
|
49
51
|
reductions_map, new_unicode_chars_map, _ = splinter_trainer.train(train_dataset_path, train_dataset_name, None)
|
|
50
52
|
|
|
51
53
|
# Application:
|
|
@@ -54,14 +56,14 @@ text_processor = TextProcessorWithEncoding(language_utils, reductions_map, new_u
|
|
|
54
56
|
text_processor.save_corpus_as_text_file(train_dataset_path, train_dataset_name)
|
|
55
57
|
|
|
56
58
|
# - Train tokenizer on the splintered corpus
|
|
57
|
-
tokenizer_corpus_path = get_corpus_path(get_corpus_name(train_dataset_path, train_dataset_name))
|
|
59
|
+
tokenizer_corpus_path = folder.get_corpus_path(get_corpus_name(train_dataset_path, train_dataset_name))
|
|
58
60
|
tokenizer_type = 'bpe'
|
|
59
61
|
vocab_size = 128_000
|
|
60
62
|
|
|
61
63
|
from scripts.train_tokenizer import train_tokenizer # Thin wrapper around SentencePiece; you can use any package that takes a string corpus.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
|
|
65
|
+
tokenizer_path = folder.get_tokenizer_path(tokenizer_type=tokenizer_type, vocab_size=vocab_size)
|
|
66
|
+
train_tokenizer(folder, tokenizer_type=tokenizer_type, vocab_size=vocab_size, input_path=tokenizer_corpus_path, output_path=tokenizer_path)
|
|
65
67
|
```
|
|
66
68
|
|
|
67
69
|
## Citation
|
|
@@ -11,17 +11,18 @@ The following example trains Splinter on Hebrew Wikipedia, encodes the training
|
|
|
11
11
|
# Training:
|
|
12
12
|
from splintering.trainer import SplinterTrainer
|
|
13
13
|
from splintering.text_processors import TextProcessorWithEncoding
|
|
14
|
-
from splintering.utils.languages import LanguageUtilsFactory
|
|
15
|
-
from splintering.utils.
|
|
14
|
+
from splintering.utils.languages import LanguageUtilsFactory, Language
|
|
15
|
+
from splintering.utils.paths import SplinterPaths
|
|
16
16
|
from splintering.utils.misc import get_corpus_name
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
folder = SplinterPaths("example")
|
|
19
|
+
language = Language.find("Hebrew")
|
|
19
20
|
train_dataset_path = 'wikimedia/wikipedia'
|
|
20
21
|
train_dataset_name = f'20231101.{language}'
|
|
21
22
|
language_utils = LanguageUtilsFactory.get_by_language(language)
|
|
22
23
|
|
|
23
24
|
# - Create the reductions map, and map the reductions in it into new Unicode characters
|
|
24
|
-
splinter_trainer = SplinterTrainer(language_utils)
|
|
25
|
+
splinter_trainer = SplinterTrainer(language_utils, folder)
|
|
25
26
|
reductions_map, new_unicode_chars_map, _ = splinter_trainer.train(train_dataset_path, train_dataset_name, None)
|
|
26
27
|
|
|
27
28
|
# Application:
|
|
@@ -30,14 +31,14 @@ text_processor = TextProcessorWithEncoding(language_utils, reductions_map, new_u
|
|
|
30
31
|
text_processor.save_corpus_as_text_file(train_dataset_path, train_dataset_name)
|
|
31
32
|
|
|
32
33
|
# - Train tokenizer on the splintered corpus
|
|
33
|
-
tokenizer_corpus_path = get_corpus_path(get_corpus_name(train_dataset_path, train_dataset_name))
|
|
34
|
+
tokenizer_corpus_path = folder.get_corpus_path(get_corpus_name(train_dataset_path, train_dataset_name))
|
|
34
35
|
tokenizer_type = 'bpe'
|
|
35
36
|
vocab_size = 128_000
|
|
36
37
|
|
|
37
38
|
from scripts.train_tokenizer import train_tokenizer # Thin wrapper around SentencePiece; you can use any package that takes a string corpus.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
|
|
40
|
+
tokenizer_path = folder.get_tokenizer_path(tokenizer_type=tokenizer_type, vocab_size=vocab_size)
|
|
41
|
+
train_tokenizer(folder, tokenizer_type=tokenizer_type, vocab_size=vocab_size, input_path=tokenizer_corpus_path, output_path=tokenizer_path)
|
|
41
42
|
```
|
|
42
43
|
|
|
43
44
|
## Citation
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "splintering"
|
|
3
|
-
version = "1.0.
|
|
3
|
+
version = "1.0.2"
|
|
4
4
|
requires-python = ">= 3.10"
|
|
5
5
|
|
|
6
6
|
authors = [
|
|
@@ -18,7 +18,8 @@ classifiers = [
|
|
|
18
18
|
readme = "README.md"
|
|
19
19
|
|
|
20
20
|
dependencies = [
|
|
21
|
-
"numpy
|
|
21
|
+
"numpy",
|
|
22
|
+
"langcodes[data]",
|
|
22
23
|
"scipy",
|
|
23
24
|
"scikit-learn",
|
|
24
25
|
"pandas",
|
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
from scripts.params import
|
|
1
|
+
from scripts.params import get_dummy_experiment, Language
|
|
2
2
|
from scripts.logger import get_logger, initialize_logger
|
|
3
3
|
|
|
4
|
-
import os
|
|
5
4
|
import sentencepiece as spm
|
|
6
5
|
|
|
7
6
|
from splintering.text_processors import TextProcessorForDemo
|
|
8
7
|
from splintering.utils.languages import LanguageUtilsFactory
|
|
9
8
|
from splintering.utils.languages import LanguageUtilsInterface
|
|
10
|
-
from splintering.utils.
|
|
9
|
+
from splintering.utils.paths import SplinterPaths
|
|
11
10
|
from splintering.utils.misc import (get_reductions_map_from_file, get_new_unicode_chars_map_from_file,
|
|
12
11
|
get_new_unicode_chars_inverted_map_from_file)
|
|
13
12
|
|
|
14
13
|
|
|
15
|
-
def demo(text: str, language_utils: LanguageUtilsInterface) -> None:
|
|
14
|
+
def demo(home: SplinterPaths, text: str, language_utils: LanguageUtilsInterface) -> None:
|
|
16
15
|
get_logger().info(f'\nOriginal text: \n{text}')
|
|
17
|
-
text_processor = get_processor(language_utils)
|
|
16
|
+
text_processor = get_processor(home, language_utils)
|
|
18
17
|
non_encoded_text, encoded_text = text_processor.process(text)
|
|
19
18
|
get_logger().info(f'\n\nNon-encoded splintered text: \n{non_encoded_text}')
|
|
20
19
|
get_logger().info(f'\n\nEncoded splintered text: \n{encoded_text}')
|
|
@@ -23,26 +22,25 @@ def demo(text: str, language_utils: LanguageUtilsInterface) -> None:
|
|
|
23
22
|
get_logger().info(f'\n\nBack to original text: \n{original_text}')
|
|
24
23
|
|
|
25
24
|
|
|
26
|
-
def get_processor(language_utils: LanguageUtilsInterface) -> TextProcessorForDemo:
|
|
27
|
-
reductions_map = get_reductions_map_from_file()
|
|
28
|
-
new_unicode_chars_map = get_new_unicode_chars_map_from_file()
|
|
29
|
-
new_unicode_chars_inverted_map = get_new_unicode_chars_inverted_map_from_file()
|
|
25
|
+
def get_processor(home: SplinterPaths, language_utils: LanguageUtilsInterface) -> TextProcessorForDemo:
|
|
26
|
+
reductions_map = get_reductions_map_from_file(home)
|
|
27
|
+
new_unicode_chars_map = get_new_unicode_chars_map_from_file(home)
|
|
28
|
+
new_unicode_chars_inverted_map = get_new_unicode_chars_inverted_map_from_file(home)
|
|
30
29
|
text_processor = TextProcessorForDemo(language_utils, reductions_map, new_unicode_chars_map, new_unicode_chars_inverted_map)
|
|
31
30
|
return text_processor
|
|
32
31
|
|
|
33
32
|
|
|
34
|
-
def tokenize_example(text: str, tokenizer_type: str, vocab_size: int) -> None:
|
|
35
|
-
tokenizer_path =
|
|
33
|
+
def tokenize_example(home: SplinterPaths, text: str, tokenizer_type: str, vocab_size: int) -> None:
|
|
34
|
+
tokenizer_path = home.get_tokenizers_dir() / f'{tokenizer_type}_{vocab_size}.model'
|
|
36
35
|
sp_tokenizer = spm.SentencePieceProcessor(model_file=tokenizer_path)
|
|
37
36
|
encoded_text = " ".join([str(token_id) for token_id in sp_tokenizer.encode(text)])
|
|
38
37
|
get_logger().info(f'tokenized example: {encoded_text}')
|
|
39
38
|
|
|
40
39
|
|
|
41
40
|
if __name__ == '__main__':
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
get_logger().info(f'Logs will be saved here: {get_logs_dir()}')
|
|
41
|
+
language = Language.find("Hebrew")
|
|
42
|
+
experiment = get_dummy_experiment(language, 'hebrew-letters_subset')
|
|
43
|
+
initialize_logger(experiment)
|
|
44
|
+
get_logger().info(f'Logs will be saved here: {experiment.PATHS.get_logs_dir()}')
|
|
47
45
|
# tokenize_example("עבודה לעבוד במעבדה", "bpe", 128000)
|
|
48
|
-
demo("נגרר", LanguageUtilsFactory.get_by_language(
|
|
46
|
+
demo(experiment.PATHS, "נגרר", LanguageUtilsFactory.get_by_language(language))
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from scripts.params import split_to_separate_runs, create_experiment_allletters, create_experiment_someletters, \
|
|
2
|
+
create_experiment_baseline, SplinterPaths, SplinterExperiment, Language
|
|
3
|
+
|
|
4
|
+
LANGUAGE = Language.find("Hebrew")
|
|
5
|
+
VOCAB_SIZES = [128000, 64000, 32000, 10000, 2000, 1000, 800]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def create_all_experiments(home: SplinterPaths) -> list[SplinterExperiment]:
|
|
9
|
+
experiments = []
|
|
10
|
+
experiments.extend(split_to_separate_runs(create_experiment_allletters(LANGUAGE, home)))
|
|
11
|
+
if LANGUAGE == Language.find("Hebrew"):
|
|
12
|
+
experiments.extend(split_to_separate_runs(create_experiment_someletters(LANGUAGE, home)))
|
|
13
|
+
experiments.extend(split_to_separate_runs(create_experiment_baseline(LANGUAGE, home)))
|
|
14
|
+
return experiments
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from scripts.experiments import create_all_experiments
|
|
2
|
+
from scripts.logger import get_logger, initialize_logger
|
|
3
|
+
|
|
4
|
+
from splintering.utils.misc import add_static_result_to_file
|
|
5
|
+
from splintering.experiments.cognitive_evaluation import tokenize_cognitive_evaluation_file, run_cognitive_evaluation
|
|
6
|
+
from splintering.utils.paths import SplinterPaths
|
|
7
|
+
|
|
8
|
+
experiment = create_all_experiments(SplinterPaths("cognitive_eval"))[0]
|
|
9
|
+
experiment.PATHS.create_experiment_dirs()
|
|
10
|
+
initialize_logger(experiment)
|
|
11
|
+
get_logger().info(f'Experiment started')
|
|
12
|
+
try:
|
|
13
|
+
for tokenizer_type in experiment.TOKENIZERS_TYPES:
|
|
14
|
+
for vocab_size in experiment.TOKENIZERS_VOCAB_SIZES:
|
|
15
|
+
tokenizer_path = experiment.PATHS.get_tokenizer_path(tokenizer_type=tokenizer_type, vocab_size=vocab_size)
|
|
16
|
+
tokenize_cognitive_evaluation_file(experiment.PATHS, tokenizer_path, tokenizer_type, vocab_size)
|
|
17
|
+
cognitive_evaluation = run_cognitive_evaluation(experiment.PATHS, tokenizer_type, vocab_size)
|
|
18
|
+
add_static_result_to_file(experiment.PATHS, cognitive_evaluation)
|
|
19
|
+
except Exception as e:
|
|
20
|
+
get_logger().exception(f'Experiment failed with error: \n{e}')
|
|
21
|
+
exit(1)
|
|
22
|
+
get_logger().info(f'Experiment finished')
|
|
@@ -1,28 +1,23 @@
|
|
|
1
|
-
from scripts.params import
|
|
1
|
+
from scripts.params import create_experiment_allletters, Language
|
|
2
2
|
|
|
3
3
|
from splintering.utils.languages import LanguageUtilsFactory
|
|
4
|
-
from splintering.utils.
|
|
4
|
+
from splintering.utils.paths import SplinterPaths
|
|
5
5
|
from splintering.experiments.compare_vocabs import list_all_vocab_files, decode_splintered_vocab, compare_vocabs
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
def main(all_letters_vocabs_file_path: str, baseline_vocabs_file_path: str):
|
|
9
|
-
experiment =
|
|
10
|
-
experiment
|
|
11
|
-
experiment['TOKENIZERS_VOCAB_SIZES'] = [128000, 64000, 32000, 10000, 2000, 1000, 800]
|
|
12
|
-
set_run_params(experiment)
|
|
13
|
-
|
|
14
|
-
import os
|
|
15
|
-
os.makedirs(get_logs_dir(), exist_ok=True)
|
|
8
|
+
def main(lang: Language, home: SplinterPaths, all_letters_vocabs_file_path: str, baseline_vocabs_file_path: str):
|
|
9
|
+
experiment = create_experiment_allletters(lang, home)
|
|
10
|
+
experiment.TOKENIZERS_VOCAB_SIZES = [128000, 64000, 32000, 10000, 2000, 1000, 800]
|
|
16
11
|
|
|
17
12
|
# clean old runs results
|
|
18
|
-
output_path =
|
|
19
|
-
if
|
|
20
|
-
|
|
13
|
+
output_path = home.get_logs_dir() / "vocabs_comparison.txt"
|
|
14
|
+
if output_path.exists():
|
|
15
|
+
output_path.unlink()
|
|
21
16
|
|
|
22
17
|
all_letters_vocab_files = list_all_vocab_files(all_letters_vocabs_file_path)
|
|
23
18
|
baseline_vocab_files = list_all_vocab_files(baseline_vocabs_file_path)
|
|
24
19
|
|
|
25
|
-
language_utils = LanguageUtilsFactory.get_by_language(
|
|
20
|
+
language_utils = LanguageUtilsFactory.get_by_language(experiment.LANGUAGE)
|
|
26
21
|
for vocab_file in all_letters_vocab_files.keys():
|
|
27
22
|
baseline_vocab_file = baseline_vocab_files.get(vocab_file)
|
|
28
23
|
with open(baseline_vocab_file, 'r', encoding='utf-8') as file:
|
|
@@ -32,9 +27,9 @@ def main(all_letters_vocabs_file_path: str, baseline_vocabs_file_path: str):
|
|
|
32
27
|
splinter_vocab_file = all_letters_vocab_files.get(vocab_file)
|
|
33
28
|
with open(splinter_vocab_file, 'r', encoding='utf-8') as file:
|
|
34
29
|
splinter_vocab = [line.rstrip("\n") for line in file]
|
|
35
|
-
splinter_decoded_vocab = decode_splintered_vocab(splinter_vocab, language_utils)
|
|
30
|
+
splinter_decoded_vocab = decode_splintered_vocab(home, splinter_vocab, language_utils)
|
|
36
31
|
|
|
37
|
-
with open(
|
|
32
|
+
with open(home.get_logs_dir() / f'{vocab_file}_decoded.txt', 'w', encoding='utf-8') as file:
|
|
38
33
|
file.write("\n".join(splinter_decoded_vocab))
|
|
39
34
|
|
|
40
35
|
results = compare_vocabs(baseline_vocab, splinter_decoded_vocab)
|
|
@@ -43,7 +38,10 @@ def main(all_letters_vocabs_file_path: str, baseline_vocabs_file_path: str):
|
|
|
43
38
|
file.write("\n".join(results))
|
|
44
39
|
|
|
45
40
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
)
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
main(
|
|
43
|
+
Language.find("Hebrew"),
|
|
44
|
+
SplinterPaths("compare_vocabs"), # TODO: The below paths are hardcoded and therefore bad and wrong.
|
|
45
|
+
all_letters_vocabs_file_path='./experiments/2025-01-21-hebrew-all_letters/results/tokenizers',
|
|
46
|
+
baseline_vocabs_file_path='./experiments/2025-01-21-hebrew-baseline/results/tokenizers'
|
|
47
|
+
)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from scripts.params import create_experiment_allletters, Language
|
|
2
|
+
|
|
3
|
+
from splintering.utils.paths import SplinterPaths
|
|
4
|
+
from splintering.utils.languages import HebrewUtils
|
|
5
|
+
from splintering.experiments.compare_vocabs_for_dicta_bert import get_dicta_bert_tokens, compare_vocabs
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
experiment = create_experiment_allletters(Language.find("Hebrew"), SplinterPaths("compare_vocabs_dicta_bert"))
|
|
9
|
+
experiment.TOKENIZERS_VOCAB_SIZES = [128000, 64000, 32000, 10000, 2000, 1000, 800]
|
|
10
|
+
|
|
11
|
+
language_utils = HebrewUtils()
|
|
12
|
+
output_path = experiment.PATHS.get_logs_dir() / "vocabs_comparison.txt"
|
|
13
|
+
baseline_vocab, splinter_decoded_vocab = get_dicta_bert_tokens(experiment.PATHS, language_utils)
|
|
14
|
+
|
|
15
|
+
with open(experiment.PATHS.get_logs_dir() / 'SplinterDictaBert - Vocab - decoded.txt', 'w', encoding='utf-8') as file:
|
|
16
|
+
file.write("\n".join(splinter_decoded_vocab))
|
|
17
|
+
|
|
18
|
+
results = compare_vocabs(baseline_vocab, splinter_decoded_vocab)
|
|
19
|
+
with open(output_path, 'a') as file:
|
|
20
|
+
file.write("\n".join(results))
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from scripts.params import get_dummy_experiment
|
|
2
|
+
from scripts.logger import get_logger, initialize_logger
|
|
3
|
+
|
|
4
|
+
from splintering.experiments.trigram_lm import test_perplexity
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
experiment = get_dummy_experiment('trigram_lm_perplexity') # TODO: dummy experiment probably indicates bad code design.
|
|
8
|
+
initialize_logger(experiment)
|
|
9
|
+
perplexity = test_perplexity(experiment.PATHS, corpus_name='hebrew_diacritized_pre_modern_unigram_10000', train_model=True)
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
from scripts.params import SplinterExperiment
|
|
1
2
|
import json
|
|
3
|
+
from dataclasses import asdict
|
|
2
4
|
|
|
3
|
-
from scripts.params import get_all_run_params
|
|
4
5
|
from splintering.utils.logger import initialize_logger as _init, get_logger
|
|
5
6
|
|
|
6
7
|
|
|
7
|
-
def initialize_logger():
|
|
8
|
-
_init()
|
|
8
|
+
def initialize_logger(experiment: SplinterExperiment):
|
|
9
|
+
_init(experiment.PATHS)
|
|
9
10
|
logger = get_logger()
|
|
10
11
|
|
|
11
12
|
# log run params
|
|
12
13
|
logger.info("********** RUN PARAMETERS **********")
|
|
13
|
-
run_params_str = json.dumps(
|
|
14
|
+
run_params_str = json.dumps(asdict(experiment), indent='\t')
|
|
14
15
|
logger.info(f"\n{run_params_str}\n\n\n")
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
from scripts.params import SplinterExperiment, PATH_DATA
|
|
2
|
+
from scripts.logger import get_logger, initialize_logger
|
|
3
|
+
from scripts.experiments import create_all_experiments
|
|
4
|
+
|
|
5
|
+
from splintering.utils.corpus_tools import CorpusTokenizer
|
|
6
|
+
from splintering.trainer import SplinterTrainer
|
|
7
|
+
from splintering.text_processors import TextProcessorBaseline, TextProcessorWithEncoding
|
|
8
|
+
from splintering.utils.languages import LanguageUtilsFactory
|
|
9
|
+
from splintering.utils.static_checks import run_static_checks, run_types_length_distribution
|
|
10
|
+
from scripts.train_tokenizer import train_tokenizer
|
|
11
|
+
from splintering.utils.paths import SplinterPaths
|
|
12
|
+
from splintering.utils.misc import add_static_result_to_file, get_corpus_name
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def run(experiment: SplinterExperiment):
|
|
16
|
+
language_utils = LanguageUtilsFactory.get_by_language(experiment.LANGUAGE)
|
|
17
|
+
train_dataset_path = experiment.SPLINTER_TRAINING_CORPUS_PATH
|
|
18
|
+
train_dataset_name = experiment.SPLINTER_TRAINING_CORPUS_NAME
|
|
19
|
+
letters_subset = experiment.SPLINTER_LETTERS_SUBSET
|
|
20
|
+
paths = experiment.PATHS
|
|
21
|
+
|
|
22
|
+
if experiment.SAVE_CORPORA_INTO_FILE:
|
|
23
|
+
# this is a splinter experiment
|
|
24
|
+
if experiment.IS_ENCODED:
|
|
25
|
+
splinter_trainer = SplinterTrainer(language_utils, home=paths)
|
|
26
|
+
reductions_map, new_unicode_chars_map, _ = splinter_trainer.train(train_dataset_path, train_dataset_name, letters_subset)
|
|
27
|
+
text_processor = TextProcessorWithEncoding(language_utils, reductions_map, new_unicode_chars_map)
|
|
28
|
+
|
|
29
|
+
# this is a baseline experiment - no splinter
|
|
30
|
+
else:
|
|
31
|
+
text_processor = TextProcessorBaseline(language_utils)
|
|
32
|
+
|
|
33
|
+
# save entire corpora as text files, encoded or not
|
|
34
|
+
text_processor.save_corpus_as_text_file(paths, train_dataset_path, train_dataset_name)
|
|
35
|
+
language_utils.save_additional_corpora_for_evaluation(paths, text_processor)
|
|
36
|
+
|
|
37
|
+
# train tokenizers of different type (unigram / bpe), resulting in different vocab size
|
|
38
|
+
if experiment.TRAIN_TOKENIZERS:
|
|
39
|
+
tokenizer_corpus_path = paths.get_corpus_path(get_corpus_name(train_dataset_path, train_dataset_name))
|
|
40
|
+
for tokenizer_type in experiment.TOKENIZERS_TYPES:
|
|
41
|
+
for vocab_size in experiment.TOKENIZERS_VOCAB_SIZES:
|
|
42
|
+
tokenizer_path = paths.get_tokenizer_path(tokenizer_type=tokenizer_type, vocab_size=vocab_size)
|
|
43
|
+
train_tokenizer(paths, tokenizer_type=tokenizer_type, vocab_size=vocab_size, input_path=tokenizer_corpus_path, output_path=tokenizer_path, is_encoded=experiment.IS_ENCODED)
|
|
44
|
+
|
|
45
|
+
# for each trained tokenizer
|
|
46
|
+
for tokenizer_type in experiment.TOKENIZERS_TYPES:
|
|
47
|
+
for vocab_size in experiment.TOKENIZERS_VOCAB_SIZES:
|
|
48
|
+
# tokenize corpora into files
|
|
49
|
+
if experiment.TOKENIZE_CORPORA:
|
|
50
|
+
tokenizer_path = paths.get_tokenizer_path(tokenizer_type=tokenizer_type, vocab_size=vocab_size)
|
|
51
|
+
for corpus_name in experiment.STATIC_CHECKS_CORPORA:
|
|
52
|
+
corpus_path = paths.get_corpus_path(corpus_name)
|
|
53
|
+
tokenized_corpus_path = paths.get_tokenized_corpus_path(corpus_name, tokenizer_type, vocab_size)
|
|
54
|
+
CorpusTokenizer().tokenize_corpus_into_file(tokenizer_path, corpus_path, tokenized_corpus_path)
|
|
55
|
+
|
|
56
|
+
# run static checks on tokenized corpora
|
|
57
|
+
if experiment.RUN_STATIC_CHECKS:
|
|
58
|
+
types_length_distribution = run_types_length_distribution(paths, tokenizer_type, vocab_size)
|
|
59
|
+
add_static_result_to_file(paths, types_length_distribution)
|
|
60
|
+
|
|
61
|
+
for corpus_name in experiment.STATIC_CHECKS_CORPORA:
|
|
62
|
+
corpus_path = paths.get_corpus_path(corpus_name)
|
|
63
|
+
tokenized_corpus_path = paths.get_tokenized_corpus_path(corpus_name, tokenizer_type, vocab_size)
|
|
64
|
+
tokenizer_path = paths.get_tokenizer_path(tokenizer_type=tokenizer_type, vocab_size=vocab_size)
|
|
65
|
+
results = run_static_checks(corpus_path, tokenized_corpus_path, tokenizer_path, vocab_size)
|
|
66
|
+
add_static_result_to_file(paths, results)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
if __name__ == '__main__':
|
|
70
|
+
import os
|
|
71
|
+
slurm_array_task_id = os.getenv('SLURM_ARRAY_TASK_ID')
|
|
72
|
+
task_id = (int(slurm_array_task_id) - 1) if slurm_array_task_id else 0
|
|
73
|
+
|
|
74
|
+
paths = SplinterPaths(experiment_name="main", task_id=str(task_id), base_directory=PATH_DATA)
|
|
75
|
+
experiment = create_all_experiments(paths)[task_id]
|
|
76
|
+
paths.create_experiment_dirs()
|
|
77
|
+
initialize_logger(experiment)
|
|
78
|
+
get_logger().info(f'Experiment {task_id + 1} started')
|
|
79
|
+
try:
|
|
80
|
+
run(experiment)
|
|
81
|
+
except Exception as e:
|
|
82
|
+
get_logger().exception(f'Experiment {task_id + 1} failed with error: \n{e}')
|
|
83
|
+
exit(1)
|
|
84
|
+
get_logger().info(f'Experiment {task_id + 1} finished')
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from langcodes import Language
|
|
6
|
+
import copy
|
|
7
|
+
|
|
8
|
+
from splintering.utils.paths import SplinterPaths
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
PATH_DATA = Path(__file__).resolve().parent.parent / "data"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class SplinterExperiment:
|
|
16
|
+
PATHS: SplinterPaths
|
|
17
|
+
|
|
18
|
+
RUN_STATIC_CHECKS: bool
|
|
19
|
+
STATIC_CHECKS_CORPORA: list[str]
|
|
20
|
+
TOKENIZE_CORPORA: bool
|
|
21
|
+
|
|
22
|
+
LANGUAGE: Language
|
|
23
|
+
SPLINTER_TRAINING_CORPUS_PATH: str
|
|
24
|
+
SPLINTER_TRAINING_CORPUS_NAME: str
|
|
25
|
+
SPLINTER_LETTERS_SUBSET: Optional[list[str]]
|
|
26
|
+
SAVE_CORPORA_INTO_FILE: bool
|
|
27
|
+
IS_ENCODED: bool
|
|
28
|
+
|
|
29
|
+
TRAIN_TOKENIZERS: bool
|
|
30
|
+
TOKENIZERS_TYPES: list[str]
|
|
31
|
+
TOKENIZERS_VOCAB_SIZES: list[int]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def get_experiment_default(lang: Language, paths: SplinterPaths) -> SplinterExperiment:
|
|
35
|
+
return SplinterExperiment(
|
|
36
|
+
PATHS=paths,
|
|
37
|
+
RUN_STATIC_CHECKS=True,
|
|
38
|
+
STATIC_CHECKS_CORPORA=['hebrew_diacritized_pre_modern', 'HeNLP_HeDC4'] if lang.to_tag() == "he" else [f'wikimedia_wikipedia_20231101_{lang.to_tag()}'],
|
|
39
|
+
|
|
40
|
+
TOKENIZE_CORPORA=False,
|
|
41
|
+
LANGUAGE=lang,
|
|
42
|
+
SPLINTER_TRAINING_CORPUS_PATH='wikimedia/wikipedia',
|
|
43
|
+
SPLINTER_TRAINING_CORPUS_NAME=f'20231101.{lang.to_tag()}',
|
|
44
|
+
SPLINTER_LETTERS_SUBSET=None,
|
|
45
|
+
SAVE_CORPORA_INTO_FILE=False,
|
|
46
|
+
IS_ENCODED=True,
|
|
47
|
+
|
|
48
|
+
TRAIN_TOKENIZERS=False,
|
|
49
|
+
TOKENIZERS_TYPES=['unigram', 'bpe'],
|
|
50
|
+
TOKENIZERS_VOCAB_SIZES=[128_000]
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def get_dummy_experiment(lang: Language, experiment_name: str) -> SplinterExperiment:
|
|
55
|
+
return get_experiment_default(lang, SplinterPaths(experiment_name, '1000000', base_directory=PATH_DATA))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def create_experiment_baseline(lang: Language, paths: SplinterPaths) -> SplinterExperiment:
|
|
59
|
+
baseline_template = get_experiment_default(lang, paths)
|
|
60
|
+
baseline_template.EXPERIMENT_NAME = f'{lang.language_name()}-baseline' # baseline
|
|
61
|
+
baseline_template.IS_ENCODED = False # False = baseline experiment
|
|
62
|
+
baseline_template.SPLINTER_LETTERS_SUBSET = None
|
|
63
|
+
return baseline_template
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def create_experiment_allletters(lang: Language, paths: SplinterPaths) -> SplinterExperiment:
|
|
67
|
+
all_letters_template = get_experiment_default(lang, paths)
|
|
68
|
+
all_letters_template.EXPERIMENT_NAME = f'{lang.language_name()}-all_letters' # all letters
|
|
69
|
+
all_letters_template.IS_ENCODED = True
|
|
70
|
+
all_letters_template.SPLINTER_LETTERS_SUBSET = None # None = all letters
|
|
71
|
+
return all_letters_template
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def create_experiment_someletters(lang: Language, paths: SplinterPaths) -> SplinterExperiment:
|
|
75
|
+
letters_subset_template = get_experiment_default(lang, paths)
|
|
76
|
+
letters_subset_template.EXPERIMENT_NAME = f'{lang.language_name()}-letters_subset' # letters subset
|
|
77
|
+
letters_subset_template.IS_ENCODED = True
|
|
78
|
+
letters_subset_template.SPLINTER_LETTERS_SUBSET = ["א", "ב", "ה", "ו", "י", "כ", "ל", "מ", "נ", "ש", "ת"]
|
|
79
|
+
return letters_subset_template
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def split_to_separate_runs(template: SplinterExperiment) -> list[SplinterExperiment]:
|
|
83
|
+
runs = []
|
|
84
|
+
for vocab_size in template.TOKENIZERS_VOCAB_SIZES:
|
|
85
|
+
run = copy.deepcopy(template)
|
|
86
|
+
run.TOKENIZERS_VOCAB_SIZES = [vocab_size]
|
|
87
|
+
runs.append(run)
|
|
88
|
+
return runs
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
from scripts.params import get_run_params
|
|
2
|
-
|
|
3
1
|
import sentencepiece as spm
|
|
4
2
|
|
|
5
3
|
from splintering.utils.logger import get_logger
|
|
6
|
-
from splintering.utils.path_utils import get_logs_dir
|
|
7
4
|
from splintering.utils.misc import decode_tokens_vocab_file
|
|
5
|
+
from splintering.utils.paths import SplinterPaths
|
|
8
6
|
|
|
9
7
|
|
|
10
|
-
def train_tokenizer(tokenizer_type, vocab_size, input_path, output_path):
|
|
11
|
-
log_path =
|
|
8
|
+
def train_tokenizer(paths: SplinterPaths, tokenizer_type, vocab_size, input_path, output_path, is_encoded: bool=True):
|
|
9
|
+
log_path = paths.get_logs_dir() / f"SentencePieceTrainer_{tokenizer_type}_{vocab_size}.log"
|
|
12
10
|
get_logger().info(f'Start training tokenizer {tokenizer_type}_{vocab_size}. Training logs are in logs dir.')
|
|
13
11
|
_train_tokenizer(
|
|
14
12
|
input_file=input_path,
|
|
@@ -20,8 +18,8 @@ def train_tokenizer(tokenizer_type, vocab_size, input_path, output_path):
|
|
|
20
18
|
get_logger().info(f'Finished training tokenizer {tokenizer_type}_{vocab_size}.')
|
|
21
19
|
|
|
22
20
|
# this is done only for the encoded tokenizer
|
|
23
|
-
if
|
|
24
|
-
decode_tokens_vocab_file(output_path)
|
|
21
|
+
if is_encoded:
|
|
22
|
+
decode_tokens_vocab_file(paths, output_path)
|
|
25
23
|
|
|
26
24
|
|
|
27
25
|
def _train_tokenizer(input_file, output_path, tokenizer_type, vocab_size, log_path):
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import json
|
|
2
|
-
import os
|
|
3
2
|
|
|
4
3
|
import pandas as pd
|
|
5
4
|
import sentencepiece as spm
|
|
@@ -7,12 +6,12 @@ from scipy.stats import pearsonr
|
|
|
7
6
|
|
|
8
7
|
from splintering.utils.corpus_tools import TextProcessorInterface
|
|
9
8
|
from splintering.utils.logger import get_logger
|
|
10
|
-
from splintering.utils.
|
|
9
|
+
from splintering.utils.paths import SplinterPaths
|
|
11
10
|
|
|
12
11
|
|
|
13
|
-
def save_cognitive_evaluation_corpus(text_processor: TextProcessorInterface):
|
|
12
|
+
def save_cognitive_evaluation_corpus(home: SplinterPaths, text_processor: TextProcessorInterface):
|
|
14
13
|
get_logger().info(f'Start saving HeLP_The_Hebrew_Lexicon_Project corpus as text file with {text_processor.__class__.__name__}')
|
|
15
|
-
data = pd.read_excel(
|
|
14
|
+
data = pd.read_excel(home.get_raw_data_dir() / "HeLP_The_Hebrew_Lexicon_Project" / "raw_LD_data.xlsx")
|
|
16
15
|
filtered_data = filter_data(data) # do the same filtering as in the original HeLP paper
|
|
17
16
|
|
|
18
17
|
aggregated_by_stimuli = filtered_data.groupby("Stimulus", as_index=False).agg(
|
|
@@ -24,7 +23,7 @@ def save_cognitive_evaluation_corpus(text_processor: TextProcessorInterface):
|
|
|
24
23
|
)
|
|
25
24
|
|
|
26
25
|
aggregated_by_stimuli["Stimulus"] = [text_processor.process(stimulus) for stimulus in aggregated_by_stimuli["Stimulus"]]
|
|
27
|
-
aggregated_by_stimuli.to_csv(
|
|
26
|
+
aggregated_by_stimuli.to_csv(home.get_corpora_dir() / "HeLP_cognitive_evaluation.txt", index=False)
|
|
28
27
|
get_logger().info(f'Finished saving HeLP_The_Hebrew_Lexicon_Project corpus')
|
|
29
28
|
|
|
30
29
|
|
|
@@ -51,11 +50,11 @@ def filter_data(data):
|
|
|
51
50
|
return filtered_data
|
|
52
51
|
|
|
53
52
|
|
|
54
|
-
def tokenize_cognitive_evaluation_file(tokenizer_path, tokenizer_type, vocab_size):
|
|
55
|
-
corpus_path = get_corpus_path(
|
|
56
|
-
tokenized_corpus_path = get_tokenized_corpus_path(
|
|
57
|
-
if
|
|
58
|
-
|
|
53
|
+
def tokenize_cognitive_evaluation_file(home: SplinterPaths, tokenizer_path, tokenizer_type, vocab_size):
|
|
54
|
+
corpus_path = home.get_corpus_path("HeLP_cognitive_evaluation")
|
|
55
|
+
tokenized_corpus_path = home.get_tokenized_corpus_path("HeLP_cognitive_evaluation", tokenizer_type, vocab_size)
|
|
56
|
+
if tokenized_corpus_path.exists():
|
|
57
|
+
tokenized_corpus_path.unlink()
|
|
59
58
|
|
|
60
59
|
get_logger().info(f'cognitive_evaluation: start tokenizing corpus with {tokenizer_type} tokenizer, vocab size {vocab_size}.')
|
|
61
60
|
sp_tokenizer = spm.SentencePieceProcessor(model_file=f'{tokenizer_path}.model')
|
|
@@ -66,8 +65,8 @@ def tokenize_cognitive_evaluation_file(tokenizer_path, tokenizer_type, vocab_siz
|
|
|
66
65
|
get_logger().info('cognitive_evaluation: finished tokenizing corpus')
|
|
67
66
|
|
|
68
67
|
|
|
69
|
-
def run_cognitive_evaluation(tokenizer_type, vocab_size):
|
|
70
|
-
tokenized_corpus_path = get_tokenized_corpus_path('HeLP_cognitive_evaluation', tokenizer_type, vocab_size)
|
|
68
|
+
def run_cognitive_evaluation(home: SplinterPaths, tokenizer_type, vocab_size):
|
|
69
|
+
tokenized_corpus_path = home.get_tokenized_corpus_path('HeLP_cognitive_evaluation', tokenizer_type, vocab_size)
|
|
71
70
|
data = pd.read_csv(tokenized_corpus_path)
|
|
72
71
|
data = data.dropna()
|
|
73
72
|
data["wordiness"] = data.apply(get_wordiness, axis=1)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import os
|
|
2
2
|
|
|
3
3
|
from splintering.utils.languages import LanguageUtilsInterface
|
|
4
|
-
from splintering.utils.misc import get_new_unicode_chars_map_from_file
|
|
4
|
+
from splintering.utils.misc import get_new_unicode_chars_map_from_file, SplinterPaths
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
def compare_vocabs(baseline_vocab: list[str], splinter_decoded_vocab: list[str]):
|
|
@@ -20,8 +20,8 @@ def compare_vocabs(baseline_vocab: list[str], splinter_decoded_vocab: list[str])
|
|
|
20
20
|
]
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
def decode_splintered_vocab(encoded_text, language_utils: LanguageUtilsInterface):
|
|
24
|
-
new_unicode_chars_map = get_new_unicode_chars_map_from_file()
|
|
23
|
+
def decode_splintered_vocab(home: SplinterPaths, encoded_text, language_utils: LanguageUtilsInterface):
|
|
24
|
+
new_unicode_chars_map = get_new_unicode_chars_map_from_file(home)
|
|
25
25
|
new_unicode_chars_inverted_map = {value: key for key, value in new_unicode_chars_map.items()}
|
|
26
26
|
decoded_tokens = list()
|
|
27
27
|
for encoded_token in encoded_text:
|