| 1 | # opencc_phrase_extract |
| 2 | |
| 3 | `opencc_phrase_extract` extracts likely word or phrase candidates from UTF-8 |
| 4 | text corpora. It is a developer tool for finding possible dictionary entries; |
| 5 | it does not convert text and it does not build `.ocd2` dictionaries. |
| 6 | |
| 7 | The tool reads one or more input files, concatenates their contents, runs |
| 8 | OpenCC's phrase extraction algorithm, and writes scored candidates to an output |
| 9 | file. |
| 10 | |
| 11 | ## Usage |
| 12 | |
| 13 | ```sh |
| 14 | opencc_phrase_extract INPUT... -o OUTPUT |
| 15 | ``` |
| 16 | |
| 17 | Examples: |
| 18 | |
| 19 | ```sh |
| 20 | opencc_phrase_extract corpus.txt -o phrases.txt |
| 21 | opencc_phrase_extract news-1.txt news-2.txt news-3.txt -o phrases.txt |
| 22 | ``` |
| 23 | |
| 24 | Arguments: |
| 25 | |
| 26 | - `INPUT...`: one or more UTF-8 text files. |
| 27 | - `-o, --output OUTPUT`: required output file path. |
| 28 | |
| 29 | Run `opencc_phrase_extract --help` to see the command-line parser help for the |
| 30 | installed binary. |
| 31 | |
| 32 | ## Output Format |
| 33 | |
| 34 | Each output line contains one extracted candidate and its scores: |
| 35 | |
| 36 | ```text |
| 37 | phrase frequency log_probability cohesion entropy prefix_entropy suffix_entropy |
| 38 | ``` |
| 39 | |
| 40 | Columns: |
| 41 | |
| 42 | - `phrase`: extracted UTF-8 word or phrase candidate. |
| 43 | - `frequency`: number of occurrences counted by the extractor. |
| 44 | - `log_probability`: natural log of `frequency / total_occurrences`. |
| 45 | - `cohesion`: minimum pointwise mutual information across possible internal |
| 46 | splits of the candidate. Higher values usually indicate that the characters |
| 47 | bind together more strongly. |
| 48 | - `entropy`: `prefix_entropy + suffix_entropy`. |
| 49 | - `prefix_entropy`: entropy of the candidate's left context. |
| 50 | - `suffix_entropy`: entropy of the candidate's right context. |
| 51 | |
| 52 | Higher cohesion and richer left/right entropy generally make a candidate more |
| 53 | likely to be a useful phrase. The output is intended for review, not direct |
| 54 | inclusion in OpenCC dictionaries. |
| 55 | |
| 56 | ## Extraction Behavior |
| 57 | |
| 58 | The command-line tool currently uses the following fixed settings: |
| 59 | |
| 60 | - candidate maximum length: 2 UTF-8 code points |
| 61 | - prefix context length: 1 UTF-8 code point |
| 62 | - suffix context length: 1 UTF-8 code point |
| 63 | |
| 64 | Candidates containing common punctuation are filtered out. The final selection |
| 65 | uses thresholds in `PhraseExtract::DefaultPostCalculationFilter`; these |
| 66 | thresholds are heuristic and may change with the implementation. |
| 67 | |
| 68 | For custom lengths, filters, or thresholds, use the `PhraseExtract` C++ class |
| 69 | directly instead of the command-line executable. |
| 70 | |
| 71 | ## Dictionary Workflow |
| 72 | |
| 73 | `opencc_phrase_extract` only discovers candidate phrases. A typical dictionary |
| 74 | maintenance workflow is: |
| 75 | |
| 76 | 1. Run `opencc_phrase_extract` on a relevant corpus. |
| 77 | 2. Review the scored output manually. |
| 78 | 3. Add accepted entries to the appropriate text dictionary under |
| 79 | `data/dictionary/`. |
| 80 | 4. Use `opencc_dict` or the normal build to generate `.ocd2` dictionaries. |
| 81 | 5. Run tests and conversion checks for the affected configuration. |
| 82 | |
| 83 | ## Notes |
| 84 | |
| 85 | - Input files should be UTF-8 encoded. |
| 86 | - The tool reads all input contents into memory before extraction. |
| 87 | - Multiple input files are concatenated in the order provided. |
| 88 | - Existing output files are overwritten. |
| 89 | - Empty or unreadable input files may produce no useful candidates; verify file |
| 90 | paths before running large jobs. |