def process_folders(config, format_func, user_vocabs=[], remove_page_info=True): in_folder = config['Exec']['input_folder'] out_folder = config['Exec']['output_folder'] tok_profile = config['Exec']['tok_profile'] suffix = config['Exec']['tokenized_suffix'] in_folder = Path(in_folder) out_folder = Path(out_folder) assert in_folder.is_dir() # check the input folder exists out_folder.mkdir(exist_ok=True) # ensure the output folder exists tok = BoTokenizer(tok_profile, user_word_list=user_vocabs) if bool(config['Exec']['rebuild_trie']): tok.tok.trie.rebuild_trie() in_files = in_folder.glob('*.txt') for f in in_files: content = f.read_text(encoding='utf-8-sig') if remove_page_info: content = clean_lines(content) tokens = tok.tokenize(content) out = format_func(tokens) out = ' '.join(out).replace('\n ', '\n') out_file = out_folder / str(f.name) out_file.write_text(out, encoding='utf-8-sig')
def tokenize_folder(config, user_vocabs=[]): in_folder = config['Exec']['input_folder'] out_folder = config['Exec']['output_folder'] tok_profile = config['Exec']['tok_profile'] suffix = config['Exec']['tokenized_suffix'] in_folder = Path(in_folder) out_folder = Path(out_folder) assert in_folder.is_dir() # check the input folder exists out_folder.mkdir(exist_ok=True) # ensure the output folder exists tok = BoTokenizer(tok_profile, toadd_filenames=user_vocabs) if bool(config['Exec']['rebuild_trie']): tok.tok.trie.rebuild_trie() in_files = in_folder.glob('*.txt') for f in in_files: content = f.read_text(encoding='utf-8-sig') tokens = tok.tokenize(content) out = get_tokenized_string(tokens) out = ' '.join(out).replace('\n ', '\n') out_file = out_folder / str(f.stem + suffix + f.suffix) out_file.write_text(out, encoding='utf-8-sig')
elif current_mark: mark_tokens(tmp, current_mark) tokens.extend(tmp) return tokens def get_vocab_files(vocab_folder): """helper function to get the absolute paths of all .txt files in a give dir""" files = Path(vocab_folder).glob('*.txt') abs_file_paths = [Path().cwd() / f for f in files] return abs_file_paths if __name__ == '__main__': in_str = '''ལག་ཁུག་མདར་བོ་་་༺ཟིག་།ཞིག་༻གི་ནང་༺ང་།ལ་༻གོན་༺གྱི་།རྒྱུ་༻བཞག་༺ཡོ་ཁུ་།ཡོད་ཀི༻ ༺དོ་ཚིགས་།ད་ལོ་༻ ༺ཟེ་།ཟེར་༻ནི་༺རེ་།རེད་༻༙པཱ་༙ ཁྱག་ནི་རེ། ༼དོགས་གཞིའི་ཚིག༽ (健康证) ༼དོགས་གཞིའི་ཚིག༽ ཀན་༺ན་།ལ་༻ངས་གཅིག་འདྲི་ཨ༻ ༼༽ སློབ་གྲྭ་ར་༺བ་།བར་༻བལྟས་ན་༺ཨུ་།ཨེ་༻ཡིན། ལྟ་༺གྱི་ཟིག་ཡོ་ཁུ་༻ཡ། ༺འདི་ཆེ་ཁ་ཟིག་རེ་དྲ་།འདི་ཡིས་ཆེ་ནི་འདྲ་ཞིག་རེད་དེ།༻ གང་གི་ལམ། ༺མོས་རས་།མ་གིའི་༻ལམ་༺ཟེ་༻ནི་༺ཨུ་༻ཡིན། ༺དུ་རེ་།དེ་རེད།༻ ༺རང་ང་།རང་བཞིན་༻ ༺ངེ་ཀི་།ངེད་ཀི་༻རྒན་མོ་བརྟག་དཔྱད་༺ཟིག་༻ ༺ཡེད་ཀི་།བྱེད་ཀི་༻འགྲོ་ནི་ཡིན། ༺སྒ་བ་།སྒལ་བ་༻ ༺ཟིག་༻ ༺ཁོས་འུ་།ན་གི་༺ཟེ་གི་༻ རྒྱ་གྲམ་དམར་༺རོ་།པོ་༻ ཨེ། ཇོ་བོ་རིན་པོ་༼མོ་མིང་༽༺ཨུ་༻ཡིན། ཨ་ལ། ༼༼ཧ་ཧ་ཧ་ཧ་༽༽ སྐྱེ་དངོས་སྲུང་སྐྱོབས་༺ཡེ་གྱི་བོ་༻ལོ་༺དུ་འུ་༻རིང་༺ང་༻སྲུང་སྐྱོབས་་་ ༺ཁ་ཕྱེ་སོ་།ཁ་ཕྱེ་ལུགས་༻མ་ཤེས་༙ཁྱིར་༙ སོང་༺ངིས་༻''' tok = BoTokenizer('GMD', user_word_list=get_vocab_files('vocabs')) corpus = CorpusTokenizer(tok) tokens = corpus.corpus_tokens(in_str) print('ok')
from pybo import BoPipeline, BoTokenizer tok = BoTokenizer('GMD') def bo_tok(text): return tok.tokenize(text) def pos_suggestions(tokens): output = [] idx = 0 for t in tokens: start = idx if t.type == 'syl': end = start + t.syls[-1][-1] + 1 if t.affixed: pos = t.pos else: pos = t.pos output.append([pos, start, end]) idx = start + len(t.content) return output pipeline = BoPipeline( 'dummy', # preprocessor bo_tok, # tokenizer pos_suggestions, # processor
def pybo_tok(text: str, profile: str) -> List[PyboToken]: tok = BoTokenizer(profile) return tok.tokenize(text)
from pybo import BoTokenizer from pathlib import Path def get_vocab_files(vocab_folder): """helper function to get the absolute paths of all .txt files in a give dir""" files = Path(vocab_folder).glob('*.txt') abs_file_paths = [Path().cwd() / f for f in files] return abs_file_paths if __name__ == '__main__': custom_vocab_filenames = get_vocab_files('vocabs') tok = BoTokenizer('POS', toadd_filenames=custom_vocab_filenames) in_str = 'བཀྲ་ཤིས་བདེ་ལེགས། ཀཀཀ། པཔཔ།' tokens = tok.tokenize(in_str) for t in tokens: print(f'{t.content}: {t.pos}') # བཀྲ་ཤིས་: NOUN # བདེ་ལེགས: NOUN # །: punct # ཀཀཀ: TEST # །: punct # པཔཔ: TEST # །: punct
elif current_mark: mark_tokens(tmp, current_mark) tokens.extend(tmp) return tokens def get_vocab_files(vocab_folder): """helper function to get the absolute paths of all .txt files in a give dir""" files = Path(vocab_folder).glob('*.txt') abs_file_paths = [Path().cwd() / f for f in files] return abs_file_paths if __name__ == '__main__': in_str = '''ལག་ཁུག་མདར་བོ་་་༺ཟིག་།ཞིག་༻གི་ནང་༺ང་།ལ་༻གོན་༺གྱི་།རྒྱུ་༻བཞག་༺ཡོ་ཁུ་།ཡོད་ཀི༻ ༺དོ་ཚིགས་།ད་ལོ་༻ ༺ཟེ་།ཟེར་༻ནི་༺རེ་།རེད་༻༙པཱ་༙ ཁྱག་ནི་རེ། ༼དོགས་གཞིའི་ཚིག༽ (健康证) ༼དོགས་གཞིའི་ཚིག༽ ཀན་༺ན་།ལ་༻ངས་གཅིག་འདྲི་ཨ༻ ༼༽ སློབ་གྲྭ་ར་༺བ་།བར་༻བལྟས་ན་༺ཨུ་།ཨེ་༻ཡིན། ལྟ་༺གྱི་ཟིག་ཡོ་ཁུ་༻ཡ། ༺འདི་ཆེ་ཁ་ཟིག་རེ་དྲ་།འདི་ཡིས་ཆེ་ནི་འདྲ་ཞིག་རེད་དེ།༻ གང་གི་ལམ། ༺མོས་རས་།མ་གིའི་༻ལམ་༺ཟེ་༻ནི་༺ཨུ་༻ཡིན། ༺དུ་རེ་།དེ་རེད།༻ ༺རང་ང་།རང་བཞིན་༻ ༺ངེ་ཀི་།ངེད་ཀི་༻རྒན་མོ་བརྟག་དཔྱད་༺ཟིག་༻ ༺ཡེད་ཀི་།བྱེད་ཀི་༻འགྲོ་ནི་ཡིན། ༺སྒ་བ་།སྒལ་བ་༻ ༺ཟིག་༻ ༺ཁོས་འུ་།ན་གི་༺ཟེ་གི་༻ རྒྱ་གྲམ་དམར་༺རོ་།པོ་༻ ཨེ། ཇོ་བོ་རིན་པོ་༼མོ་མིང་༽༺ཨུ་༻ཡིན། ཨ་ལ། ༼༼ཧ་ཧ་ཧ་ཧ་༽༽ སྐྱེ་དངོས་སྲུང་སྐྱོབས་༺ཡེ་གྱི་བོ་༻ལོ་༺དུ་འུ་༻རིང་༺ང་༻སྲུང་སྐྱོབས་་་ ༺ཁ་ཕྱེ་སོ་།ཁ་ཕྱེ་ལུགས་༻མ་ཤེས་༙ཁྱིར་༙ སོང་༺ངིས་༻''' tok = BoTokenizer('GMD', toadd_filenames=get_vocab_files('vocabs')) corpus = CorpusTokenizer(tok) tokens = corpus.corpus_tokens(in_str) print('ok')