Skip to content

Commit

Permalink
load_model: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
adbar committed Mar 29, 2022
1 parent 091bb11 commit 3bdd682
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions py3langid/langid.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ def load_model(path=None):
@param path to model
"""
LOGGER.debug('initializing identifier')
global IDENTIFIER
if path is None:
return LanguageIdentifier.from_pickled_model(MODEL_FILE)
return LanguageIdentifier.from_modelpath(path)
IDENTIFIER = LanguageIdentifier.from_pickled_model(MODEL_FILE)
else:
IDENTIFIER = LanguageIdentifier.from_modelpath(path)


def set_languages(langs=None):
Expand All @@ -53,9 +55,8 @@ def set_languages(langs=None):
@param langs a list of language codes
"""
global IDENTIFIER
if IDENTIFIER is None:
IDENTIFIER = load_model()
load_model()
return IDENTIFIER.set_languages(langs)


Expand All @@ -68,9 +69,8 @@ def classify(instance):
@param instance a text string. Unicode strings will automatically be utf8-encoded
@returns a tuple of the most likely language and the confidence score
"""
global IDENTIFIER
if IDENTIFIER is None:
IDENTIFIER = load_model()
load_model()
return IDENTIFIER.classify(instance)


Expand All @@ -83,9 +83,8 @@ def rank(instance):
@param instance a text string. Unicode strings will automatically be utf8-encoded
@returns a list of tuples language and the confidence score, in descending order
"""
global IDENTIFIER
if IDENTIFIER is None:
IDENTIFIER = load_model()
load_model()
return IDENTIFIER.rank(instance)


Expand All @@ -98,9 +97,8 @@ def cl_path(path):
@param path path to file
@returns a tuple of the most likely language and the confidence score
"""
global IDENTIFIER
if IDENTIFIER is None:
IDENTIFIER = load_model()
load_model()
return IDENTIFIER.cl_path(path)


Expand All @@ -113,9 +111,8 @@ def rank_path(path):
@param path path to file
@returns a list of tuples language and the confidence score, in descending order
"""
global IDENTIFIER
if IDENTIFIER is None:
IDENTIFIER = load_model()
load_model()
return IDENTIFIER.rank_path(path)


Expand Down

0 comments on commit 3bdd682

Please sign in to comment.