-
-
Notifications
You must be signed in to change notification settings - Fork 17
CLDR-json data extraction script #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Januar;N;MASC;NOM;SG;INAN | ||
| Februar;N;MASC;NOM;SG;INAN | ||
| März;N;MASC;NOM;SG;INAN | ||
| April;N;MASC;NOM;SG;INAN | ||
| Mai;N;MASC;NOM;SG;INAN | ||
| Juni;N;MASC;NOM;SG;INAN | ||
| Juli;N;MASC;NOM;SG;INAN | ||
| August;N;MASC;NOM;SG;INAN | ||
| September;N;MASC;NOM;SG;INAN | ||
| Oktober;N;MASC;NOM;SG;INAN | ||
| November;N;MASC;NOM;SG;INAN | ||
| Dezember;N;MASC;NOM;SG;INAN | ||
| Sonntag;N;MASC;NOM;SG;INAN | ||
| Montag;N;MASC;NOM;SG;INAN | ||
| Dienstag;N;MASC;NOM;SG;INAN | ||
| Mittwoch;N;MASC;NOM;SG;INAN | ||
| Donnerstag;N;MASC;NOM;SG;INAN | ||
| Freitag;N;MASC;NOM;SG;INAN | ||
| Samstag;N;MASC;NOM;SG;INAN | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| January;N;MASC;NOM;SG;INAN | ||
| February;N;MASC;NOM;SG;INAN | ||
| March;N;MASC;NOM;SG;INAN | ||
| April;N;MASC;NOM;SG;INAN | ||
| May;N;MASC;NOM;SG;INAN | ||
| June;N;MASC;NOM;SG;INAN | ||
| July;N;MASC;NOM;SG;INAN | ||
| August;N;MASC;NOM;SG;INAN | ||
| September;N;MASC;NOM;SG;INAN | ||
| October;N;MASC;NOM;SG;INAN | ||
| November;N;MASC;NOM;SG;INAN | ||
| December;N;MASC;NOM;SG;INAN | ||
| Sunday;N;MASC;NOM;SG;INAN | ||
| Monday;N;MASC;NOM;SG;INAN | ||
| Tuesday;N;MASC;NOM;SG;INAN | ||
| Wednesday;N;MASC;NOM;SG;INAN | ||
| Thursday;N;MASC;NOM;SG;INAN | ||
| Friday;N;MASC;NOM;SG;INAN | ||
| Saturday;N;MASC;NOM;SG;INAN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| enero;N;MASC;NOM;SG;INAN | ||
| febrero;N;MASC;NOM;SG;INAN | ||
| marzo;N;MASC;NOM;SG;INAN | ||
| abril;N;MASC;NOM;SG;INAN | ||
| mayo;N;MASC;NOM;SG;INAN | ||
| junio;N;MASC;NOM;SG;INAN | ||
| julio;N;MASC;NOM;SG;INAN | ||
| agosto;N;MASC;NOM;SG;INAN | ||
| septiembre;N;MASC;NOM;SG;INAN | ||
| octubre;N;MASC;NOM;SG;INAN | ||
| noviembre;N;MASC;NOM;SG;INAN | ||
| diciembre;N;MASC;NOM;SG;INAN | ||
| domingo;N;MASC;NOM;SG;INAN | ||
| lunes;N;MASC;NOM;SG;INAN | ||
|
||
| martes;N;MASC;NOM;SG;INAN | ||
| miércoles;N;MASC;NOM;SG;INAN | ||
| jueves;N;MASC;NOM;SG;INAN | ||
| viernes;N;MASC;NOM;SG;INAN | ||
| sábado;N;MASC;NOM;SG;INAN | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| janvier;N;MASC;NOM;SG;INAN | ||
| février;N;MASC;NOM;SG;INAN | ||
| mars;N;MASC;NOM;SG;INAN | ||
| avril;N;MASC;NOM;SG;INAN | ||
| mai;N;MASC;NOM;SG;INAN | ||
| juin;N;MASC;NOM;SG;INAN | ||
| juillet;N;MASC;NOM;SG;INAN | ||
| août;N;MASC;NOM;SG;INAN | ||
| septembre;N;MASC;NOM;SG;INAN | ||
| octobre;N;MASC;NOM;SG;INAN | ||
| novembre;N;MASC;NOM;SG;INAN | ||
| décembre;N;MASC;NOM;SG;INAN | ||
| dimanche;N;MASC;NOM;SG;INAN | ||
| lundi;N;MASC;NOM;SG;INAN | ||
| mardi;N;MASC;NOM;SG;INAN | ||
| mercredi;N;MASC;NOM;SG;INAN | ||
| jeudi;N;MASC;NOM;SG;INAN | ||
| vendredi;N;MASC;NOM;SG;INAN | ||
| samedi;N;MASC;NOM;SG;INAN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| јануар;N;MASC;NOM;SG;INAN | ||
| фебруар;N;MASC;NOM;SG;INAN | ||
| март;N;MASC;NOM;SG;INAN | ||
| април;N;MASC;NOM;SG;INAN | ||
| мај;N;MASC;NOM;SG;INAN | ||
| јун;N;MASC;NOM;SG;INAN | ||
| јул;N;MASC;NOM;SG;INAN | ||
| август;N;MASC;NOM;SG;INAN | ||
| септембар;N;MASC;NOM;SG;INAN | ||
| октобар;N;MASC;NOM;SG;INAN | ||
| новембар;N;MASC;NOM;SG;INAN | ||
| децембар;N;MASC;NOM;SG;INAN | ||
| недеља;N;FEM;NOM;SG;INAN | ||
| понедељак;N;MASC;NOM;SG;INAN | ||
| уторак;N;MASC;NOM;SG;INAN | ||
| среда;N;FEM;NOM;SG;INAN | ||
| четвртак;N;MASC;NOM;SG;INAN | ||
| петак;N;MASC;NOM;SG;INAN | ||
| субота;N;FEM;NOM;SG;INAN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """ | ||
| Extracts data from CLDR-JSON repository, e.g. nouns like month or day names. | ||
| Script either creates a new inflection file, or appends data to existing one. | ||
| The nomenclature is taken from https://unimorph.github.io/doc/unimorph-schema.pdf (see Appendix) | ||
|
|
||
| Part of Speech;Gender;Case;Number;Animacy | ||
|
|
||
| Run script from data folder. | ||
|
|
||
| Before running the script clone cldr-json repository: | ||
|
|
||
| gh repo clone unicode-org/cldr-json | ||
|
|
||
| and install jsonpath-ng package: | ||
|
|
||
| pip install jsonpath-ng | ||
| """ | ||
|
|
||
| import argparse | ||
| import json | ||
| import os | ||
|
|
||
| from jsonpath_ng import jsonpath, parse | ||
|
|
||
|
|
||
| def load_json(filename): | ||
| """Loads JSON data from the specified file. | ||
|
|
||
| Args: | ||
| filename: The name of the JSON file. | ||
|
|
||
| Returns: | ||
| The parsed JSON data. | ||
| """ | ||
|
|
||
| try: | ||
| with open(filename, 'r', encoding='utf-8') as file: | ||
| return json.load(file) | ||
| except FileNotFoundError: | ||
| print(f"Error: File '{filename}' not found.") | ||
| return None | ||
|
|
||
|
|
||
| def write_to_lexicon(output_file, language, json_data): | ||
| """Extracts specified data from cldr-json file | ||
| and writes it to the lexicon file. | ||
|
|
||
| Args: | ||
| output_file: name of the lexicon. | ||
| language: cldr-json file language. | ||
| json_data: parsed cldr-json data. | ||
| """ | ||
| MONTH_NAMES_EXPRESSION = parse('main..dates.calendars.gregorian.months.format.wide.*') | ||
| DAY_NAMES_EXPRESSION = parse('main..dates.calendars.gregorian.days.format.wide.*') | ||
| EXPRESSIONS = [MONTH_NAMES_EXPRESSION, DAY_NAMES_EXPRESSION] | ||
|
|
||
| results = [] | ||
| for expression in EXPRESSIONS: | ||
| match = expression.find(json_data) | ||
| for m in match: | ||
| results.append(m.value + ';N;MASC;NOM;SG;INAN\n') | ||
|
|
||
| full_filename = os.path.join(language, output_file) | ||
| try: | ||
| os.makedirs(os.path.dirname(full_filename), exist_ok=True) | ||
| with open(full_filename, 'a', encoding='utf-8') as file: | ||
| file.writelines(results) | ||
| except FileNotFoundError: | ||
| print(f"Error: file '{output_file}' can't be created.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description='Load and process CLDR-JSON files for given languages.') | ||
| parser.add_argument('--cldr_root', help='The path to CLDR-JSON data.', default='../../cldr-json/cldr-json/cldr-dates-full/main') | ||
| parser.add_argument('--input_file', help='Data file to read from, e.g. ca-gregorian.json.', default='ca-gregorian.json') | ||
| parser.add_argument('--output_file', help='Data file to create/append to, e.g. lexicon.txt.', default='lexicon.txt') | ||
| parser.add_argument('--language_list', nargs='+', default=['sr', 'en', 'de', 'es', 'fr']) | ||
| args = parser.parse_args() | ||
|
|
||
| for language in args.language_list: | ||
| full_filename = os.path.join(args.cldr_root, language, args.input_file) | ||
| data = load_json(full_filename) | ||
|
|
||
| if data: | ||
| write_to_lexicon(args.output_file, language, data) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to use some other format? This is the kind of format that I typically use.
I omitted some mapping details between these entries, but that's mostly how I store this type of data. I also use an XML format that I convert to the syntax above. The XML structure is inverted to the style that I listed above.
Some other formats have been discussed in this group too.
As you expand to languages with numerous grammatical cases, it becomes hard to keep track of how these shouting abbreviations in uppercase map to a grammeme that has to be pronounced in a meeting or for a new person to interact with the data.
The nice feature about using XML is that it's easier to filter and annotate the data as necessary. This becomes important for languages that use bounded morphemes a lot (no whitespace separation) and use different values for the grammatical number for each morpheme. For example, "your book" in Arabic is a single word with 2 morphemes. The "your" can be singular, dual or plural. The "your" can be masculine or feminine for pronunciation, depending on who you're addressing. That possessive pronoun binds to the noun being possessed. The word "book" has a gender too, and it can be turned into singular, dual or plural.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going for a simple format that's easy to prototype. Should we use XML from the start? We would need to agree on the basic structure. I'll change the structure now and then we can iterate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed format to this for now. PTAL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me! Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you approve the PR?