Ejemplo n.º 1
0
def create_or_update_weights_map_main(base_dir: str,
                                      prep_name: str,
                                      weights_prep_name: str,
                                      template_map: Optional[str] = None):
    prep_dir = get_prepared_dir(base_dir, prep_name)
    assert os.path.isdir(prep_dir)
    orig_prep_dir = get_prepared_dir(base_dir, weights_prep_name)
    assert os.path.isdir(orig_prep_dir)

    logger = init_logger()
    add_console_out_to_logger(logger)
    logger.info(f"Creating/updating weights map for {weights_prep_name}...")

    if template_map is not None:
        _template_map = SymbolsMap.load(template_map)
    else:
        _template_map = None

    if weights_map_exists(prep_dir, weights_prep_name):
        existing_map = load_weights_map(prep_dir, weights_prep_name)
    else:
        existing_map = None

    weights_map, symbols = create_or_update_weights_map(
        orig=load_prep_symbol_converter(orig_prep_dir).get_all_symbols(),
        dest=load_prep_symbol_converter(prep_dir).get_all_symbols(),
        existing_map=existing_map,
        template_map=_template_map,
    )

    save_weights_map(prep_dir, weights_prep_name, weights_map)
    save_weights_symbols(prep_dir, weights_prep_name, symbols)
Ejemplo n.º 2
0
def create_or_update_inference_map_main(base_dir: str,
                                        prep_name: str,
                                        template_map: Optional[str] = None):
    logger = init_logger()
    add_console_out_to_logger(logger)
    logger.info("Creating/updating inference map...")
    prep_dir = get_prepared_dir(base_dir, prep_name)
    assert os.path.isdir(prep_dir)

    all_symbols = get_all_symbols(prep_dir)

    if template_map is not None:
        _template_map = SymbolsMap.load(template_map)
    else:
        _template_map = None

    if infer_map_exists(prep_dir):
        existing_map = load_infer_map(prep_dir)
    else:
        existing_map = None

    infer_map, symbols = create_or_update_inference_map(
        orig=load_prep_symbol_converter(prep_dir).get_all_symbols(),
        dest=all_symbols,
        existing_map=existing_map,
        template_map=_template_map,
    )

    save_infer_map(prep_dir, infer_map)
    save_infer_symbols(prep_dir, symbols)
Ejemplo n.º 3
0
 def test_save_load_symbols_map(self):
   path = tempfile.mktemp()
   symbols_map = SymbolsMap({
     "b": "a",
     "c": "b",
     "x": "y",
   })
   symbols_map.save(path)
   res = SymbolsMap.load(path)
   os.remove(path)
   self.assertEqual(3, len(res))
   self.assertEqual("a", res["b"])
   self.assertEqual("b", res["c"])
   self.assertEqual("y", res["x"])
Ejemplo n.º 4
0
def map_text(base_dir: str,
             prep_name: str,
             text_name: str,
             symbols_map_path: str,
             ignore_arcs: bool = True):
    prep_dir = get_prepared_dir(base_dir, prep_name, create=False)
    text_dir = get_text_dir(prep_dir, text_name, create=False)
    if not os.path.isdir(text_dir):
        print("Please add text first.")
    else:
        symbol_ids, updated_sentences = sents_map(
            sentences=load_text_csv(text_dir),
            text_symbols=load_text_symbol_converter(text_dir),
            symbols_map=SymbolsMap.load(symbols_map_path),
            ignore_arcs=ignore_arcs)

        print("\n" + updated_sentences.get_formatted(
            symbol_id_dict=symbol_ids,
            accent_id_dict=load_prep_accents_ids(prep_dir)))
        _save_text_csv(text_dir, updated_sentences)
        save_text_symbol_converter(text_dir, symbol_ids)
        _accent_template(base_dir, prep_name, text_name)
        _check_for_unknown_symbols(base_dir, prep_name, text_name)
Ejemplo n.º 5
0
def try_load_symbols_map(symbols_map_path: str) -> Optional[SymbolsMap]:
    symbols_map = SymbolsMap.load(
        symbols_map_path) if symbols_map_path else None
    return symbols_map
Ejemplo n.º 6
0
def load_weights_map(prep_dir: str, orig_prep_name: str) -> SymbolsMap:
    path = os.path.join(prep_dir, f"{orig_prep_name}.json")
    return SymbolsMap.load(path)
Ejemplo n.º 7
0
def load_infer_map(prep_dir: str) -> SymbolsMap:
    return SymbolsMap.load(get_infer_map_path(prep_dir))