|
| 1 | +""" |
| 2 | +Copyright 2023 Nito T.M. |
| 3 | +Author URL: https://github.com/nitotm |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +import importlib.util |
| 19 | +import os |
| 20 | + |
| 21 | + |
| 22 | +class LanguageData: |
| 23 | + def __init__(self): |
| 24 | + from .resources.avg_score import avg_score |
| 25 | + self.avg_score = avg_score |
| 26 | + self.ngrams = {} |
| 27 | + self.lang_score = [] |
| 28 | + self.lang_codes = {} |
| 29 | + self.type = '' |
| 30 | + self.folder = os.path.dirname(__file__) + '/resources/ngrams/' |
| 31 | + |
| 32 | + """ |
| 33 | + ISO 639-1 codes, for the 60 languages set. |
| 34 | + ['am', 'ar', 'az', 'be', 'bg', 'bn', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'gu', |
| 35 | + 'he', 'hi', 'hr', 'hu', 'hy', 'is', 'it', 'ja', 'ka', 'kn', 'ko', 'ku', 'lo', 'lt', 'lv', 'ml', 'mr', 'ms', 'nl', |
| 36 | + 'no', 'or', 'pa', 'pl', 'pt', 'ro', 'ru', 'sk', 'sl', 'sq', 'sr', 'sv', 'ta', 'te', 'th', 'tl', 'tr', 'uk', 'ur', |
| 37 | + 'vi', 'yo', 'zh'] |
| 38 | + |
| 39 | + ['Amharic', 'Arabic', 'Azerbaijani (Latin)', 'Belarusian', 'Bulgarian', 'Bengali', 'Catalan', 'Czech', 'Danish', |
| 40 | + 'German', 'Greek', 'English', 'Spanish', 'Estonian', 'Basque', 'Persian', 'Finnish', 'French', 'Gujarati', 'Hebrew', |
| 41 | + 'Hindi', 'Croatian', 'Hungarian', 'Armenian', 'Icelandic', 'Italian', 'Japanese', 'Georgian', 'Kannada', 'Korean', |
| 42 | + 'Kurdish (Arabic)', 'Lao', 'Lithuanian', 'Latvian', 'Malayalam', 'Marathi', 'Malay (Latin)', 'Dutch', 'Norwegian', |
| 43 | + 'Oriya', 'Punjabi', 'Polish', 'Portuguese', 'Romanian', 'Russian', 'Slovak', 'Slovene', 'Albanian', |
| 44 | + 'Serbian (Cyrillic)', 'Swedish', 'Tamil', 'Telugu', 'Thai', 'Tagalog', 'Turkish', 'Ukrainian', 'Urdu', 'Vietnamese', |
| 45 | + 'Yoruba', 'Chinese'] |
| 46 | + """ |
| 47 | + |
| 48 | + def load_ngrams(self, subset_file=''): |
| 49 | + if subset_file == '': |
| 50 | + from .resources.ngrams.ngramsM60 import ngrams_data |
| 51 | + else: |
| 52 | + # module = importlib.import_module('.ngrams.' + subset_file) |
| 53 | + file_path = self.folder + subset_file + '.py' |
| 54 | + if not os.path.exists(file_path): |
| 55 | + file_path = self.folder + 'subset/' + subset_file + '.py' |
| 56 | + spec = importlib.util.spec_from_file_location(subset_file, file_path) |
| 57 | + module = importlib.util.module_from_spec(spec) |
| 58 | + spec.loader.exec_module(module) |
| 59 | + ngrams_data = module.ngrams_data |
| 60 | + |
| 61 | + self.ngrams = ngrams_data['ngrams'] |
| 62 | + self.lang_score = [0] * (max(ngrams_data['languages'].keys()) + 1) |
| 63 | + self.type = ngrams_data['type'] |
| 64 | + self.lang_codes = ngrams_data['languages'] |
| 65 | + |
| 66 | + |
| 67 | +languageData = LanguageData() |
0 commit comments