Skip to content

Commit

Permalink
Sort variants topologically
Browse files Browse the repository at this point in the history
  • Loading branch information
mhosken committed Nov 19, 2024
1 parent e9c3f38 commit e6c0ebe
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/langtag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
except NameError:
FileNotFoundError = IOError

# 10 means infinity
variantorders = {"1994": 3, "alalc97": 10, "biske": 2, "fonipa": 10, "fonkirsh": 10,
"fonnapa": 10, "fonupa": 10, "fonxsamp": 10, "grclass": 2, "grital": 2,
"grmistr": 2, "heploc": 2, "lipaw": 2, "njiva": 2, "osojs": 2, "simple": 10,
"solba": 2}

class _Singleton(type):
'''Manage singletons by fname parameter'''
Expand Down Expand Up @@ -195,7 +200,7 @@ def open_through_cache(srcpath=None):
if useurl is None:
useurl = "https://ldml.api.sil.org/langtags.json"
try:
with resources.as_file(resources.files(__name__) / 'langtags.json') as srcpath:
with resources.as_file(resources.files("langtag") / 'langtags.json') as srcpath:
inf = open_through_cache(srcpath)
except FileNotFoundError:
inf = open_through_cache()
Expand Down Expand Up @@ -494,12 +499,14 @@ def allTags(self, use639=True):

def _make_variant(self, vs):
'''Return a copy tagset changing all tags to add the variants vs.'''
def varsort(v):
return variantorders.get(v, 0)
d = dict([(k, getattr(self, k, None)) for k in self._allkeys])
for k in ('tag', 'full'):
if k in d:
l = d[k]._replace(vars=sorted((d[k].vars or []) + vs))
l = d[k]._replace(vars=sorted((d[k].vars or []) + vs, key=varsort))
d[k] = l
d['tags'] = [t._replace(vars=sorted((t.vars or []) + vs)) for t in d['tags']]
d['tags'] = [t._replace(vars=sorted((t.vars or []) + vs, key=varsort)) for t in d['tags']]
return TagSet(**d)

def newFull(self, newfull):
Expand Down

0 comments on commit e6c0ebe

Please sign in to comment.