MontrealCorpusTools / Montreal-Forced-Aligner

Command line utility for forced alignment using Kaldi
https://montrealcorpustools.github.io/Montreal-Forced-Aligner/
MIT License
1.34k stars 246 forks source link

[BUG] Unable to add words to existing dictionary #606

Closed treya-lin closed 1 year ago

treya-lin commented 1 year ago

Debugging checklist

[ ] Have you updated to latest MFA version? yes, mfa version 2.2.7 installed from source with commands

git clone https://ghproxy.com/https://github.com/MontrealCorpusTools/Montreal-Forced-Aligner.git
conda env create -n env_mfa -f environment.yml
conda activate env_mfa
python -m pip install -e .

[ ] Have you tried rerunning the command with the --clean flag? yes I did

Describe the issue I am following the guide in https://montreal-forced-aligner.readthedocs.io/en/latest/first_steps/index.html#first-steps to generate sounds for oov words and add them to the dictionary. I am just doing testing to get familiar with mfa so I only used 2 audios and the corresponding transcriptions.

$ ls data/test/mfa/
pronunciation_tr_1.lab  pronunciation_tr_1.wav  pronunciation_tr_2.lab  pronunciation_tr_2.wav

$ head data/test/mfa/*.lab
==> data/test/mfa/pronunciation_tr_1.lab <==
yarın pazar erken kalkmam

==> data/test/mfa/pronunciation_tr_2.lab <==
başına bir hâl gelmek

I used the g2p model to transcribe OOV with command

 mfa g2p --dictionary_path local/resources/mfa_tr_dict.txt data/test/mfa/ ~/Documents/MFA/pretrained_models/g2p/turkish_mfa/ exp/oov/test_g2pped.txt

There is only one word to be transcribe actually, and it generated a file test_g2pped.txt with three entries of potential pronunciations.

cat exp/oov/test_g2pped.txt 
hâl     h a a ɫ
hâl     h a ɫ
hâl     h a ʎ

Afterwards, I tried to add it back to the original dictionary with command ( adding or removing --clean and no_use_mp flags have no effect on this error)

mfa model add_words --no_use_mp --clean ~/Documents/MFA/pretrained_models/dictionary/turkish_mfa.dict exp/oov/test_g2pped.txt

but it is not adding them to the dictionary properly, giving error message DETAIL: Key (id)=(43400) already exists. Does it mean it doesn't accept words with multiple sounds? That is unlikely isn it? The original dictionary have words with multiple sounds too. The full log is pasted below.

For Reproducing your issue Please fill out the following:

  1. Corpus structure
    • What language is the corpus in? Turkish
    • How many files/speakers? 2
    • Are you using lab files or TextGrid files for input? lab
  2. Dictionary
    • Are you using a dictionary from MFA? If so, which one?

Yes.

Log file

mfa model add_words --no_use_mp --clean ~/Documents/MFA/pretrained_models/dictionary/turkish_mfa.dict exp/oov/test_g2pped.txt
waiting for server to start.... done
server started
 INFO     Stopping the global MFA database server...                                                                                                                                                                            
Exception ignored in atexit callback: <bound method ExitHooks.history_save_handler of <montreal_forced_aligner.command_line.mfa.ExitHooks object at 0x7fd19b180b20>>
Traceback (most recent call last):
  File "/home/user/workspace/tts_space/Montreal-Forced-Aligner/montreal_forced_aligner/command_line/mfa.py", line 102, in history_save_handler
    raise self.exception
  File "/home/user/anaconda3/envs/env_mfa/bin/mfa", line 8, in <module>
    sys.exit(mfa_cli())
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/rich_click/rich_group.py", line 21, in main
    rv = super().main(*args, standalone_mode=False, **kwargs)
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/user/workspace/tts_space/Montreal-Forced-Aligner/montreal_forced_aligner/command_line/model.py", line 177, in add_words_cli
    base_dictionary.add_words(new_words)
  File "/home/user/workspace/tts_space/Montreal-Forced-Aligner/montreal_forced_aligner/dictionary/multispeaker.py", line 1294, in add_words
    session.bulk_insert_mappings(Pronunciation, pronunciation_mapping)
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 4472, in bulk_insert_mappings
    self._bulk_save_mappings(
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 4559, in _bulk_save_mappings
    with util.safe_reraise():
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 4548, in _bulk_save_mappings
    bulk_persistence._bulk_insert(
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/orm/bulk_persistence.py", line 176, in _bulk_insert
    result = persistence._emit_insert_statements(
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/orm/persistence.py", line 1020, in _emit_insert_statements
    result = connection.execute(
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1414, in execute
    return meth(
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 486, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1638, in _execute_clauseelement
    ret = self._execute_context(
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1837, in _execute_context
    return self._exec_insertmany_context(
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2103, in _exec_insertmany_context
    self._handle_dbapi_exception(
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2326, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2100, in _exec_insertmany_context
    dialect.do_execute(cursor, sub_stmt, sub_params, context)
  File "/home/user/anaconda3/envs/env_mfa/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 748, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "pronunciation_pkey"
DETAIL:  Key (id)=(43400) already exists.

[SQL: INSERT INTO pronunciation (id, pronunciation, generated_by_rule, count, word_id) VALUES (%(id__0)s, %(pronunciation__0)s, %(generated_by_rule__0)s, %(count__0)s, %(word_id__0)s), (%(id__1)s, %(pronunciation__1)s, %(generated_by_rule__1)s, %(count__1)s, %(word_id__1)s), (%(id__2)s, %(pronunciation__2)s, %(generated_by_rule__2)s, %(count__2)s, %(word_id__2)s)]
[parameters: {'id__0': 43400, 'generated_by_rule__0': False, 'pronunciation__0': 'h a a ɫ', 'word_id__0': 43400, 'count__0': 0, 'id__1': 43401, 'generated_by_rule__1': False, 'pronunciation__1': 'h a ɫ', 'word_id__1': 43400, 'count__1': 0, 'id__2': 43402, 'generated_by_rule__2': False, 'pronunciation__2': 'h a ʎ', 'word_id__2': 43400, 'count__2': 0}]
(Background on this error at: https://sqlalche.me/e/20/gkpj)

Desktop (please complete the following information):

Additional context Besides, it seems for this version (2.2.7), mfa g2p does not have optional argument of --num_pronunciations so I am not sure how to specify the number of wanted pronunciations?

So any idea what caused this duplicate key value violates unique constraint error and how to fix it? Many thanks!

lifeiteng commented 1 year ago

same problem

lifeiteng commented 1 year ago

@treya-lin I fixed it in https://github.com/MontrealCorpusTools/Montreal-Forced-Aligner/pull/634

treya-lin commented 1 year ago

@treya-lin I fixed it in #634

Heyy Thanks!

joseangl commented 1 year ago

@lifeiteng It seems that the issue with the optional parameter --num_pronunciations is still not solver in the current dev version. Could it be?