| 1 | --- |
| 2 | name: opencc-fix-translation-workflow |
| 3 | description: OpenCC translation fix and complete release workflow |
| 4 | tags: [opencc, workflow, debugging] |
| 5 | --- |
| 6 | |
| 7 | # OpenCC Translation Fix Standard Operating Procedure |
| 8 | |
| 9 | This skill describes the complete lifecycle for fixing OpenCC conversion errors (such as "方程式" becoming "方程序"), including core dictionary correction, testing, and verification. |
| 10 | |
| 11 | ## 1. Problem Diagnosis |
| 12 | |
| 13 | When a conversion error is discovered (e.g., A is incorrectly converted to B): |
| 14 | |
| 15 | 1. **Search for existing mappings**: |
| 16 | Use `grep` to search for the error source in `data/dictionary`. |
| 17 | ```bash |
| 18 | grep "error_term" data/dictionary/*.txt |
| 19 | ``` |
| 20 | 2. **Identify the interference source**: |
| 21 | Usually because in Maximum Forward Matching (MaxMatch), a "longer word" contains the target word, or a "shorter word" mapping causes the incorrect result. |
| 22 | *Example*: "方程式" is incorrectly converted to "方程序" because the mapping "程式" → "程序" exists, and "方程式" is not defined as a proper noun, causing it to be segmented as "方" + "程式". |
| 23 | |
| 24 | ## 2. Fix Solution (Explicit Mapping) |
| 25 | |
| 26 | If the error originates from segmentation logic (as in the example above), the most robust fix is to **add an Explicit Mapping**. |
| 27 | |
| 28 | 1. **Select the correct dictionary file**: |
| 29 | - For Simplified-to-Traditional base phrase conversion and segmentation: `STPhrases.txt`. |
| 30 | - For Taiwan regional vocabulary / terminology choices used by `s2twp` and `tw2sp`: `TWPhrases.txt` and `TWPhrasesRev.txt`. |
| 31 | - For Taiwan character-variant phrase exceptions used by `s2tw`, `s2twp`, and `t2tw`: `TWVariantsPhrases.txt`. |
| 32 | - Do **not** put word-level regional vocabulary translations in `TWVariantsPhrases.txt`; that file is for phrase-level overrides to character variant conversion. Example: US state or territory names such as `特拉華 -> 德拉瓦`, `新澤西 -> 紐澤西`, and `美屬維爾京羣島 -> 美屬維京群島` belong in `TWPhrases.txt`. |
| 33 | - For `s2twp`, the conversion chain is `STPhrases/STCharacters -> TWPhrases -> TWVariantsPhrases/TWVariants`. If the Simplified input must survive as one phrase until the `TWPhrases` stage, add the needed Simplified-to-Traditional entry in `STPhrases.txt` for segmentation. |
| 34 | |
| 35 | 2. **Add the mapping**: |
| 36 | Map the vocabulary to itself to prevent incorrect segmentation or conversion. |
| 37 | ```text |
| 38 | 方程式 方程式 |
| 39 | ``` |
| 40 | *Note*: Maintain bidirectional consistency for `TWPhrases.txt` and `TWPhrasesRev.txt` when adding regional vocabulary mappings. |
| 41 | |
| 42 | 3. **Sort edited dictionaries**: |
| 43 | Use the project script instead of hand-sorting. |
| 44 | ```bash |
| 45 | python3 data/scripts/sort.py data/dictionary/TWPhrases.txt data/dictionary/TWPhrases.txt |
| 46 | python3 data/scripts/sort.py data/dictionary/TWPhrasesRev.txt data/dictionary/TWPhrasesRev.txt |
| 47 | python3 data/scripts/sort.py data/dictionary/STPhrases.txt data/dictionary/STPhrases.txt |
| 48 | ``` |
| 49 | |
| 50 | ## 3. Test-Driven (Test Cases) |
| 51 | |
| 52 | Before the modification takes effect, create test cases to ensure the fix and prevent regression. |
| 53 | |
| 54 | 1. **Core tests**: |
| 55 | Edit `test/testcases/testcases.json`. |
| 56 | ```json |
| 57 | { |
| 58 | "id": "case_XXX", |
| 59 | "input": "方程式", |
| 60 | "expected": { |
| 61 | "tw2sp": "方程式" |
| 62 | } |
| 63 | } |
| 64 | ``` |
| 65 | Test the config that actually uses the dictionary. `TWPhrases.txt` mappings should normally be asserted under `s2twp` / `tw2sp`, not `s2tw`. |
| 66 | |
| 67 | ## 4. Build and Verify |
| 68 | |
| 69 | OpenCC uses the CMake/Make system to build dictionaries. |
| 70 | |
| 71 | 1. **Rebuild dictionaries**: |
| 72 | ```bash |
| 73 | cd build/dbg # or your build directory |
| 74 | make Dictionaries |
| 75 | ``` |
| 76 | This step regenerates the `.ocd2` binary dictionaries. |
| 77 | |
| 78 | 2. **Manual verification**: |
| 79 | Test directly using the generated `opencc` tool. |
| 80 | ```bash |
| 81 | echo "方程式" | ./src/tools/opencc -c root/share/opencc/tw2sp.json |
| 82 | # Expected output: 方程式 |
| 83 | ``` |
| 84 | |
| 85 | 3. **Automated testing** (optional but recommended): |
| 86 | Run focused dictionary and command-line tests. For Bazel: |
| 87 | ```bash |
| 88 | bazel test //data/dictionary:dictionary_TWPhrases_test \ |
| 89 | //data/dictionary:dictionary_TWPhrasesRev_test \ |
| 90 | //data/dictionary:dictionary_TWPhrases_reverse_mapping_test \ |
| 91 | //data/dictionary:dictionary_STPhrases_test \ |
| 92 | //test:command_line_converter_test |
| 93 | ``` |
| 94 | Use the relevant subset when only some dictionaries changed. |
| 95 | |
| 96 | |
| 97 | ## 5. Commit |
| 98 | |
| 99 | When committing, it is recommended to clearly separate or combine, but must include: |
| 100 | - Dictionary text file changes (`.txt`) |
| 101 | - Core test changes (`test/testcases/testcases.json`) |
| 102 | |
| 103 | Follow the project commit style: action-verb first line, no conventional-commit |
| 104 | prefix (no `Fix:` / `feat:` etc.), and no `Co-Authored-By` trailers. |
| 105 | |
| 106 | ```bash |
| 107 | git add data/dictionary/TWPhrases.txt test/testcases/testcases.json |
| 108 | git commit -m "Correct s2twp conversion for 方程式" |
| 109 | ``` |