langcode-search / app.py
cdleong's picture
Update app.py
67daf03
raw
history blame
1.26 kB
import streamlit as st
import langcodes
# https://huggingface.co/blog/streamlit-spaces
langtext = st.text_input("language code", "en")
if langcodes.tag_is_valid(langtext):
st.write(f"{langtext} is already a valid BCP-47 language tag")
else:
st.write(f"{langtext} is not already a valid BCP-47 language tag")
try:
lang = langcodes.Language.get(langtext)
st.write(f"{lang} is the BCP-47 tag.")
if "unknown" in lang.display_name().lower():
lang = None
except langcodes.LanguageTagError as e:
st.write(f"Could not simply instantiate, trying to look up instead.")
lang = None
if lang is None:
try:
found = langcodes.find(langtext)
lang = found
st.write(f"natural language search found the following BCP-47 tag: {lang}")
except LookupError as e:
st.write(f"Unable to look up language code.")
lang = None
#st.write(f"langcodes found the following tag: {type(found)}") # a Language object
if lang is not None:
display = lang.display_name()
st.write(f"Correct BCP-47 tag for {langtext}, according to the langcodes library: {lang}")
st.write(f"Display name for {lang}: {lang.display_name()}")
st.write(f"Autonym for {lang}: {lang.autonym()}")
st.write(f"Alpha3 version (deprecated): {lang.to_alpha3()}")