apertium / apertium-apy

📦 Apertium HTTP Server in Python
https://wiki.apertium.org/wiki/Apertium-apy
GNU General Public License v3.0
32 stars 42 forks source link

Implement language code fallback #181

Closed marcriera closed 2 years ago

marcriera commented 2 years ago

This pull request implements language/pair code fallback when an exact mode match is not found. The target language has priority over the source language when looking for a valid mode. All functions with language codes support this, except for translateChain (should not be impossible to implement, but it is harder). This should specially benefit the users of CAT tools such as OmegaT or Okapi Framework, where language-country codes are frequent.

When a request is received and a matching mode is not found, the API now checks a temporary list of equivalents. If there is no match there either, it strips variant codes one by one to try to find an installed mode. If there is a match, it is cached in the temporary list for faster retrieval later. If there is no match anywhere, the usual "This pair is not installed" message is returned.

In addition, this includes two other fixes:

  1. The mode search was truncating modes with more than one variant ("_"). This caused many modes to be unavailable (see #141).
  2. Many API functions were not handling 2-letter codes properly. All functions should now convert 2-letter codes to 3-letter codes if available (for example, when requesting analyze to a language pair such as es-ca, which will now become spa-cat.

Closes #141 Closes #174