Package: langstring
LangString Python LibraryThe LangString Python Library offers a powerful and intuitive way to represent strings with associated language metadata. Designed with the intent to simplify language-specific string management, this library encapsulates a string with its language information, allowing for advanced linguistic operations and clear representations.📦 PyPI Package:The library is convenientlyavailable as a PyPI package, allowing users to easily import it into other Python projects.📚 Documentation:For inquiries and further information, please refer to thecomprehensive docstring-generated documentationavailable for this project.ContentsLangString Python LibraryContentsLangString LibraryPurpose and ContextualizationKey ComponentsPractical Use CasesRelated Work and DifferencesInstallation and UseLangStringsLangStrings’ Methods__init__to_string__str____eq____hash__MultiLangStringsMultiLangStrings’ Methods__init__add_entryadd_langstringremove_entryremove_langget_langstringget_langstrings_langget_langstrings_allget_langstrings_pref_langget_strings_langget_strings_pref_langget_strings_allget_strings_langstring_langget_strings_langstring_pref_langget_strings_langstring_alllen_entries_alllen_entries_langlen_langs__repr____str____eq____hash__Control and FlagsFlagsENSURE_TEXTENSURE_ANY_LANGENSURE_VALID_LANGControlControl Methodsset_flagget_flagreset_flagsprint_flagsCode TestingVersion 2: Key Differences and ImprovementsHow to ContributeReporting IssuesCode ContributionsTest ContributionsGeneral GuidelinesDependenciesUsing PoetryUsingrequirements.txtLicenseAuthorLangString LibraryThe LangString Library emerges as a solution to the intricate challenge of handling multilingual text in software applications. In today's globalized world, where digital platforms often cater to a diverse, multilingual audience, the need for efficiently managing text in multiple languages is paramount. This library is motivated by the necessity to simplify the representation, storage, and manipulation of language-specific text data, thereby addressing a common problem in internationalized applications.Purpose and ContextualizationThe primary purpose of the LangString Library is to provide developers with robust tools for handling text in different languages, ensuring accurate and efficient processing of multilingual content. It aims to bridge the gap between the complexities of language-specific text management and the growing demand for applications that can seamlessly operate across linguistic boundaries.Key ComponentsThe library comprises two main classes, each serving distinct roles in the realm of multilingual text management:LangString: This class encapsulates a single string along with its language information. It is ideal for scenarios where individual text strings are tied to specific languages, such as user-generated content in different languages or displaying messages in a user's preferred language.MultiLangString: This class, on the other hand, is designed for handling text strings across multiple languages. It is particularly useful in applications that need to store and display the same content in several languages, like multilingual websites or global e-commerce platforms.Practical Use CasesLangString Use Case: In a multilingual customer support system, LangString can be used to store user queries in their original language, facilitating accurate translation and response by support staff.MultiLangString Use Case: For a global marketing campaign, MultiLangString can manage promotional content in various languages, enabling the campaign to resonate with a diverse international audience.In essence, the LangString Library is a strategic response to the challenges of modern, multilingual application development, offering essential tools for seamless and effective language-specific text handling.Related Work and DifferencesThe LangString Library offers unique functionalities for handling multilingual text in Python applications. While there are several libraries and tools available for internationalization, localization, and language processing, they differ from the LangString Library in scope and functionality. Below is an overview of related work and how they compare to the LangString Library:Babelhttps://pypi.org/project/Babel/Babel is a Python library for internationalization and localization. It primarily focuses on formatting dates, numbers, and currency values for different locales.Difference: Unlike Babel, the LangString Library specifically manages multilingual text strings, providing a more direct approach to handling language-specific text data.gettexthttps://pypi.org/project/python-gettext/gettext is a GNU system used for internationalizing applications. It allows for translating fixed strings in different languages using message catalogs.Difference: The LangString Library, in contrast, is designed for dynamic management of multilingual content, not just for translation of static strings.langcodeshttps://pypi.org/project/langcodes/langcodes provides tools for parsing and understanding language tags.Difference: While langcodes is useful for handling language codes, the LangString Library extends beyond this by managing actual multilingual text strings associated with these codes.Polyglothttps://pypi.org/project/polyglot/Polyglot is a natural language pipeline that supports multiple languages for various NLP tasks.Difference: Polyglot focuses on language processing rather than the structured management of multilingual text, which is the core functionality of the LangString Library.CLD3https://pypi.org/project/gcld3/Google's CLD3 is a model for language identification.Difference: CLD3 is specialized in detecting the language of a text, whereas the LangString Library is about storing and manipulating text in multiple languages.spaCyhttps://pypi.org/project/spacy/spaCy is a comprehensive NLP library that supports multiple languages.Difference: spaCy is geared towards analyzing text, not managing it. The LangString Library, on the other hand, is designed for the structured handling and storage of multilingual text.In summary, while these related tools and libraries offer valuable functionalities for internationalization, localization, and language processing, the LangString Library stands out for its specific focus on managing and manipulating multilingual text strings in a structured and efficient manner.Installation and UseInstall with:pipinstalllangstringThen, encapsulate strings with their language tags as shown in the examples above.After installation, you can use theLangStringandMultiLangStringclasses in your project. Simply import the classes and start encapsulating strings with their language tags.fromlangstringimportLangString,MultiLangString,LangStringControl,LangStringFlag,MultiLangStringControl,MultiLangStringFlagLangStringsTheLangStringclass is a fundamental component of the LangString Library, designed to encapsulate a single string along with its associated language information. It is primarily used in scenarios where the language context of a text string is crucial, such as in multilingual applications, content management systems, or any software that deals with language-specific data. The class provides a structured way to manage text strings, ensuring that each piece of text is correctly associated with its respective language.In the LangString class, the string representation format varies based on the presence of a language tag. When a language tag is provided, the format istext. Without a language tag, it is formatted as"text"@lang, where lang is the language code.LangStrings’ MethodsTheLangStringclass offers a range of methods to efficiently handle language-specific text. These methods enable the initialization, representation, comparison, and validation of text strings in their language context.__init__Initializes a new LangString object, encapsulating a given text string with an associated language tag. Thetextparameter is the string to be encapsulated, andlangis an optional parameter specifying the language tag (e.g., 'en' for English). This method forms the core of the LangString class, allowing for the creation of language-aware string objects.Example:# Import necessary classfromlangstringimportLangString# Creating a LangString instance without a language taggreeting=LangString("Hello")print(greeting)# Expected Output: Hello# Creating a LangString instance with a language tagfrench_greeting=LangString("Bonjour","fr")print(french_greeting)# Expected Output: "Bonjour"@frto_stringConverts the LangString object into a human-readable string representation, including the language tag if present. This method is particularly useful for displaying or logging LangString objects, providing a clear and concise view of their content and associated language.Example:# Import necessary classfromlangstringimportLangString# Creating and printing a LangString instancegreeting=LangString("Hello","en")print(greeting.to_string())# Expected Output: "Hello"@en__str__Defines how the LangString object is represented as a string when printed or converted to a string. This method returns the encapsulated text, optionally followed by the language tag, providing a standard way to view and understand the LangString's content.Example:# Import necessary classfromlangstringimportLangString# Creating and printing a LangString instancegreeting=LangString("Hello","en")print(greeting)# Expected Output: "Hello"@en__eq__Determines whether two LangString objects are equal by comparing both their text and language tags. This method is essential for identifying identical language-specific strings, ensuring accurate comparisons in contexts like sorting, filtering, or deduplication.Example:# Import necessary classfromlangstringimportLangString# Comparing two LangString instancesgreeting_en1=LangString("Hello","en")greeting_en2=LangString("Hello","en")print(greeting_en1==greeting_en2)# Expected Output: True# Comparing LangString instances with different texts or languagesgreeting_es=LangString("Hola","es")print(greeting_en1==greeting_es)# Expected Output: False__hash__Generates a unique hash value for the LangString object, enabling its use in hash-based collections like sets or dictionaries. This method ensures that LangString objects can be efficiently stored and retrieved in data structures that rely on hashing.Example:# Import necessary classfromlangstringimportLangString# Creating a LangString instance and printing its hash valuegreeting=LangString("Hello","en")print(hash(greeting))# Output: (hash value, e.g., 224086809330009634)MultiLangStringsTheMultiLangStringclass is a key component of the LangString Library, designed to manage and manipulate text strings across multiple languages. This class is particularly useful in applications that require handling of text in a multilingual context, such as websites, applications with internationalization support, and data processing tools that deal with multilingual data. The primary purpose ofMultiLangStringis to store, retrieve, and manipulate text entries in various languages, offering a flexible and efficient way to handle multilingual content.MultiLangStrings’ MethodsTheMultiLangStringclass provides a suite of methods to facilitate the management of multilingual text. These methods enable the addition, removal, retrieval, and manipulation of text entries in multiple languages, as well as setting and getting a preferred language for default text retrieval.For MultiLangString instances, the string representation also depends on whether a language tag is associated with each text entry. Entries without a language tag are simply displayed astext. Entries with a language tag are presented in the format"text"@lang, clearly indicating their language context.__init__Initializes a new MultiLangString object, optionally accepting a dictionary (mls_dict) representing the internal structure, where keys are language codes and values are sets of text entries. Thepref_langparameter sets the preferred language for default text retrieval. This method lays the foundation for managing and manipulating multilingual text strings.Example:# Import necessary classfromlangstringimportMultiLangString# Initializing with a dictionarymls=MultiLangString({"en":{"Hello","Good morning"}})print(mls)# Expected Output: "Good morning"@en, "Hello"@en# Initializing with a preferred languagemls=MultiLangString(pref_lang="en")print(mls)# Expected Output: nothing, as the created MultiLangString is empty.# Initializing MultiLangString with a dictionary and a preferred languagemls=MultiLangString({"en":{"Hello","Good morning"},"es":{"Hola","Buenos días"}},pref_lang="en")print(mls)# Expected Output: "Hello"@en, "Good morning"@en, "Hola"@es, "Buenos días"@es# Printing the preferred languageprint("Preferred language:",mls.preferred_lang)# Expected Output: Preferred language: enadd_entryAdds a new text entry to the MultiLangString under a specified language code. This method ensures that the added text complies with the set control flags, such as non-empty strings or valid language tags, facilitating the dynamic and controlled addition of multilingual content.Example:# Import necessary classesfromlangstringimportMultiLangString# Initialize MultiLangString with multiple languagesmls=MultiLangString({"en":{"Hello","Good morning"},"es":{"Hola","Buenos días"}})# Add a new entry in Frenchmls.add_entry("Bonjour","fr")print(mls.get_strings_all())# Expected Output: ['Hello', 'Good morning', 'Hola', 'Buenos días', 'Bonjour']add_langstringIncorporates a LangString object directly into the MultiLangString, allowing for the seamless integration of pre-existing LangString instances. This method enhances the flexibility of MultiLangString by enabling the addition of language-specific strings already encapsulated in LangString objects.Example:# Import necessary classesfromlangstringimportMultiLangString,LangString# Initialize MultiLangString and add a LangStringmls=MultiLangString()mls.add_langstring(LangString("Hola","es"))print(mls)# Expected Output: "Hola"@esremove_entryRemoves a specified text entry from the set associated with a given language in the MultiLangString. If the removal results in an empty set for that language, the language key is also removed from the internal dictionary, maintaining the integrity of the multilingual data.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString with multiple languagesmls=MultiLangString({"en":{"Hello","Good morning"},"es":{"Hola","Buenos días"}})# Remove an entry in Englishmls.remove_entry("Good morning","en")print(mls)# Expected Output: "Hello"@en, "Hola"@es, "Buenos días"@es# Remove the last entry in Englishmls.remove_entry("Hello","en")print(mls)# Expected Output: "Hola"@es, "Buenos días"@es# Check if the 'en' key is still present in the dictionaryprint("en"inmls.mls_dict)# Expected Output: False (The key 'en' is no longer in the dictionary)remove_langEliminates all text entries associated with a specific language from the MultiLangString. This method is useful for scenarios where an entire language's content needs to be cleared, ensuring that the MultiLangString only contains relevant and active language data.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and remove a languagemls=MultiLangString({"en":{"Hello","Good morning"},"es":{"Hola","Buenos días"}})mls.remove_lang("en")print(mls)# Expected Output: "Hola"@es, "Buenos días"@esget_langstringRetrieves a LangString object representing a specific text and language combination from the MultiLangString. This method is crucial for accessing individual language-specific strings within the MultiLangString, allowing users to work with or manipulate these strings as separate LangString instances.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve a LangStringmls=MultiLangString({"en":{"Hello","Good morning"}})lang_str=mls.get_langstring("Hello","en")print(lang_str)# Expected Output: "Hello"@enget_langstrings_langGenerates a list of LangString objects for all text entries associated with a specified language in the MultiLangString. This method is ideal for extracting all language-specific strings as separate LangString instances, facilitating operations like language-based filtering or processing.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve LangStrings for a languagemls=MultiLangString({"en":{"Hello","Good morning"}})lang_strings=mls.get_langstrings_lang("en")print(', '.join(str(elem)foreleminlang_strings))# Expected Output: "Hello"@en, "Good morning"@enget_langstrings_allCompiles a comprehensive list of all LangString objects contained within the MultiLangString, covering every language and text entry. This method provides a complete overview of the multilingual content, useful for scenarios requiring a full audit or export of the stored data.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve all LangStringsmls=MultiLangString({"en":{"Hello","Good morning"}})all_lang_strings=mls.get_langstrings_all()print(', '.join(str(elem)foreleminall_lang_strings))# Expected Output: "Hello"@en, "Good morning"@enget_langstrings_pref_langFetches all LangString objects corresponding to the preferred language set in the MultiLangString. This method streamlines access to the most relevant or frequently used language, simplifying tasks like default content display or language-specific analyses.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve LangStrings for the preferred languagemls=MultiLangString({"en":{"Hello","Good morning"}},pref_lang="en")pref_lang_strings=mls.get_langstrings_pref_lang()print(', '.join(str(elem)foreleminpref_lang_strings))# Expected Output: "Hello"@en, "Good morning"@enget_strings_langReturns a list of all text strings associated with a particular language code in the MultiLangString. This method is useful for scenarios where only the text content (without language tags) is needed for a specific language, such as generating language-specific reports or content.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve text entries for a languagemls=MultiLangString({"en":{"Hello","Good morning"}})texts=mls.get_strings_lang("en")print(texts)# Expected Output: ['Hello', 'Good morning']get_strings_pref_langGathers all text entries corresponding to the preferred language in the MultiLangString. This method is particularly beneficial for applications where a default language is frequently accessed, providing quick retrieval of all relevant text content.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve text entries for the preferred languagemls=MultiLangString({"en":{"Hello","Good morning"}},pref_lang="en")pref_texts=mls.get_strings_pref_lang()print(pref_texts)# Expected Output: ['Hello', 'Good morning']get_strings_allCollects every text entry from every language present in the MultiLangString. This method offers a complete aggregation of the multilingual content, ideal for comprehensive data analysis or creating a full backup of the text data.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve all text entriesmls=MultiLangString({"en":{"Hello","Good morning"}})all_texts=mls.get_strings_all()print(all_texts)# Expected Output: ['Hello', 'Good morning']get_strings_langstring_langProduces a list of formatted text entries for a specified language, with each entry followed by its language tag. This method is valuable for presenting or exporting language-specific data in a format that explicitly includes the language context.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve formatted text entries for a languagemls=MultiLangString({"en":{"Hello","Good morning"}})formatted_texts=mls.get_strings_langstring_lang("en")print(formatted_texts)# Expected Output: ['"Good morning"@en', '"Hello"@en']get_strings_langstring_pref_langGenerates a list of formatted text entries for the preferred language, each accompanied by the language tag. This method simplifies the process of accessing and displaying the default language content in a format that maintains language awareness.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve formatted text entries for the preferred languagemls=MultiLangString({"en":{"Hello","Good morning"}},pref_lang="en")formatted_pref_texts=mls.get_strings_langstring_pref_lang()print(formatted_pref_texts)# Expected Output: ['"Hello"@en', '"Good morning"@en']get_strings_langstring_allCompiles a comprehensive list of all text entries from the MultiLangString, each formatted with its respective language tag. This method is ideal for creating detailed reports or exports where maintaining the association between text and language is crucial.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and retrieve all formatted text entriesmls=MultiLangString({"en":{"Hello","Good morning"}})all_formatted_texts=mls.get_strings_langstring_all()print(all_formatted_texts)# Expected Output: ['"Hello"@en', '"Good morning"@en']len_entries_allCalculates the total number of text entries across all languages within the MultiLangString. This method provides a quick way to assess the volume of multilingual content stored, useful for data management and analysis purposes.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and get the total number of entriesmls=MultiLangString({"en":{"Hello","Good morning"}})total_entries=mls.len_entries_all()print(total_entries)# Expected Output: 2len_entries_langDetermines the number of text entries associated with a specific language in the MultiLangString. This method is useful for evaluating the extent of content available in a particular language, aiding in language-specific content planning and analysis.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and get the number of entries for a languagemls=MultiLangString({"en":{"Hello","Good morning"}})num_entries=mls.len_entries_lang("en")print(num_entries)# Expected Output: 2len_langsCounts the number of distinct languages represented in the MultiLangString. This method is essential for understanding the linguistic diversity of the stored content, providing insights into the range of languages covered in the multilingual data.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and get the number of languagesmls=MultiLangString({"en":{"Hello","Good morning"}})num_languages=mls.len_langs()print(num_languages)# Expected Output: 1__repr__Provides a detailed and unambiguous string representation of the MultiLangString object, including its internal dictionary structure and the preferred language. This method is particularly useful for debugging and logging purposes, as it gives a clear snapshot of the object's current state, showing all its contents and configurations in a format that is helpful for developers.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and print its representationmls=MultiLangString({"en":{"Hello","Good morning"}})print(mls.__repr__())# Expected Output: MultiLangString({'en': {'Hello', 'Good morning'}}, pref_lang='en')__str__Generates a user-friendly string representation of the MultiLangString object, listing each text entry along with its associated language tag. This method is designed for readability and ease of understanding, making it suitable for displaying the multilingual content in a concise and clear format. It's particularly useful for user interfaces, reports, or any scenario where a straightforward overview of the multilingual data is needed.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and print itmls=MultiLangString({"en":{"Hello","Good morning"}})print(mls)# Expected Output: "Good morning"@en, "Hello"@en# Adding new entry without language tag and printing the resultmls.add_entry("Olá")print(mls)# Expected Output: "Good morning"@en, "Hello"@en, Olá__eq__Determines whether two MultiLangString objects are equal by comparing their internal dictionaries (mls_dict). This method is crucial for assessing the equivalence of multilingual content, ignoring the preferred language settings. It ensures that two MultiLangString instances are considered equal only if they contain the same set of language strings, making it a vital tool for data comparison and deduplication processes.Example:# Import necessary classfromlangstringimportMultiLangString# Create two MultiLangString instances with the same content but different preferred languagesmls1=MultiLangString({"en":{"Hello","Good morning"},"es":{"Hola","Buenos días"}},pref_lang="en")mls2=MultiLangString({"en":{"Hello","Good morning"},"es":{"Hola","Buenos días"}},pref_lang="es")# Despite having different preferred languages, they are considered equal because their content (mls_dict) is the sameprint(mls1==mls2)# Expected Output: True# Change the content of mls2mls2.add_entry("Bonjour","fr")# Now mls1 and mls2 are not equal because their content differsprint(mls1==mls2)# Expected Output: False__hash__Computes a unique hash value for the MultiLangString object based on its internal dictionary of language strings. This method enables the use of MultiLangString instances in hash-based data structures like sets and dictionaries. By providing a consistent hash value, it ensures that MultiLangString objects can be efficiently used as keys or stored in collections that rely on hashing mechanisms.Example:# Import necessary classfromlangstringimportMultiLangString# Initialize MultiLangString and generate a hash valuemls=MultiLangString({"en":{"Hello","Good morning"}})print(hash(mls))# Expected Output: (hash value, e.g., 3786478500744739392)Control and FlagsThe Control and Flags system in the LangString Library plays a pivotal role in managing and configuring the behavior ofLangStringandMultiLangStringinstances.This system operates at a global, class-level context, meaning that the flags and controls applied have a uniform effect across all instances of these classes. In other words, when a flag is set or reset using the control classes, it impacts every instance ofLangStringandMultiLangStringthroughout the application. This ensures consistent behavior and validation rules across all instances, as individual instances cannot have differing flag values.In the following subsections, we will delve into the specifics of the available flags and the control methods. The flags define key aspects of howLangStringandMultiLangStringinstances handle multilingual text, including validation rules and representation formats. Understanding these flags is crucial for effectively utilizing the library in various scenarios, especially those involving multilingual content.The control methods, shared betweenLangStringControlandMultiLangStringControl, provide the mechanisms to set, retrieve, and reset these flags. These methods ensure that you can dynamically configure the behavior of the library to suit your application's needs. We will explore each method in detail, providing insights into their usage and impact on the library's functionality.FlagsThe LangString and MultiLangString classes use a set of flags to control various aspects of their behavior. These flags are managed byLangStringControlandMultiLangStringControlrespectively. The flags provide a flexible way to customize the behavior ofLangStringandMultiLangStringclasses according to the specific needs of your application. By adjusting these flags, you can enforce different levels of validation and control over the language data being processed. The available flags and their effects are as follows.ENSURE_TEXTThis flag ensures that the text provided toLangStringorMultiLangStringis not empty. When set toTrue, attempting to create aLangStringor add an entry toMultiLangStringwith an empty string will raise aValueError. This is useful for enforcing the presence of meaningful content.By default, the library hasTrueas default value, indicating that it requires that text strings are not empty.Example:fromlangstringimportLangString,LangStringControl,LangStringFlag# Enabling the ENSURE_TEXT flagLangStringControl.set_flag(LangStringFlag.ENSURE_TEXT,True)# Attempting to create a LangString with an empty string will raise an errortry:LangString("","en")exceptValueErrorase:print("Error with ENSURE_TEXT=True:",e)# Expected Output: Error message about empty text# Disabling the ENSURE_TEXT flagLangStringControl.set_flag(LangStringFlag.ENSURE_TEXT,False)# Creating a LangString with an empty string will now succeedtry:ls=LangString("","en")print("Created LangString with ENSURE_TEXT=False:",ls)# Expected Output: LangString object with empty textexceptValueErrorase:print("Error with ENSURE_TEXT=False:",e)ENSURE_ANY_LANGThis flag mandates the presence of a language tag inLangStringorMultiLangString. IfTrue, aValueErroris raised when aLangStringis created or an entry is added toMultiLangStringwithout a language tag. This flag is beneficial for scenarios where language context is crucial.HasFalseby default, indicating that the library does not require a language tag to be present.ExamplefromlangstringimportMultiLangString,MultiLangStringControl,MultiLangStringFlag# Enabling the ENSURE_ANY_LANG flagMultiLangStringControl.set_flag(MultiLangStringFlag.ENSURE_ANY_LANG,True)# Attempting to add an entry to MultiLangString without a language tag will raise an errormls=MultiLangString()try:mls.add_entry("Hello")exceptValueErrorase:print("Error with ENSURE_ANY_LANG=True:",e)# Expected Output: Error message about missing language tag# Disabling the ENSURE_ANY_LANG flagMultiLangStringControl.set_flag(MultiLangStringFlag.ENSURE_ANY_LANG,False)# Adding an entry without a language tag will now succeedmls.add_entry("Hello")print("Added entry with ENSURE_ANY_LANG=False:",mls)# Expected Output: MultiLangString object with the entry without a language tagENSURE_VALID_LANGThis flag ensures that the language tags used inLangStringorMultiLangStringare valid according to standard language codes (e.g., ISO 639-1).The LangString Library uses thelangcodes packageto validate language tags. When enabled, creating aLangStringor adding an entry toMultiLangStringwith an invalid language tag results in aValueError. This flag is essential for maintaining consistency and accuracy in language-specific data.HasFalseby default, indicating that the library does not enforce the validity of language tags.Example:fromlangstringimportLangStringControl,LangStringFlag,LangString# Enabling the ENSURE_VALID_LANG flagLangStringControl.set_flag(LangStringFlag.ENSURE_VALID_LANG,True)# Attempting to create a LangString with an invalid language tag will raise an errortry:LangString("Hello","invalid-lang-code")exceptValueErrorase:print("Error with ENSURE_VALID_LANG=True:",e)# Expected Output: Error message about invalid language tag# Disabling the ENSURE_VALID_LANG flagLangStringControl.set_flag(LangStringFlag.ENSURE_VALID_LANG,False)# Creating a LangString with an invalid language tag will now succeedls=LangString("Hello","invalid-lang-code")print("Created LangString with ENSURE_VALID_LANG=False:",ls)# Expected Output: LangString object with an invalid language tagControlThe Control classes, namelyLangStringControlandMultiLangStringControl, act as static managers for the flags. They provide methods to set, retrieve, and reset the states of these flags, ensuring consistent behavior across all instances ofLangStringandMultiLangString.Control Methodsset_flagTheset_flagmethod is used to enable or disable a specific flag for either LangString or MultiLangString. This method allows for dynamic configuration of behavior, such as enforcing non-empty text or valid language tags.Example:fromlangstringimportLangStringControl,LangStringFlag# Setting a flag in LangStringControlLangStringControl.set_flag(LangStringFlag.ENSURE_TEXT,True)print("ENSURE_TEXT flag set to:",LangStringControl.get_flag(LangStringFlag.ENSURE_TEXT))# Expected Output: True# Resetting the flag to a different valueLangStringControl.set_flag(LangStringFlag.ENSURE_TEXT,False)print("ENSURE_TEXT flag reset to:",LangStringControl.get_flag(LangStringFlag.ENSURE_TEXT))# Expected Output: Falseget_flagTheget_flagmethod retrieves the current state (enabled or disabled) of a specified flag. It is useful for checking the configuration status of LangString or MultiLangString instances.Example:fromlangstringimportMultiLangStringControl,MultiLangStringFlag# Setting the ENSURE_TEXT flag to a known stateMultiLangStringControl.set_flag(MultiLangStringFlag.ENSURE_TEXT,False)# Retrieving the state of the ENSURE_TEXT flag in MultiLangStringControlis_ensure_text_enabled=MultiLangStringControl.get_flag(MultiLangStringFlag.ENSURE_TEXT)print("ENSURE_TEXT flag is:",is_ensure_text_enabled)# Expected Output: Falsereset_flagsThereset_flagsmethod resets all flags to their default values. This is particularly useful for restoring the default behavior after temporary changes to the configuration flags.Example:fromlangstringimportLangStringControl,LangStringFlag# Setting the flags to a non-default stateLangStringControl.set_flag(LangStringFlag.ENSURE_TEXT,False)LangStringControl.set_flag(LangStringFlag.ENSURE_ANY_LANG,True)LangStringControl.set_flag(LangStringFlag.ENSURE_VALID_LANG,True)# Resetting all flags to default in LangStringControlLangStringControl.reset_flags()print("Flags after reset:")LangStringControl.print_flags()# Expected Output: Default states of all flags, as follows:# LangStringFlag.ENSURE_TEXT = True# LangStringFlag.ENSURE_ANY_LANG = False# LangStringFlag.ENSURE_VALID_LANG = Falseprint_flagsTheprint_flagsmethod prints the current state of all configuration flags to the console. It is a convenient tool for debugging or monitoring the current flag settings.Example:fromlangstringimportMultiLangStringControl# Resetting flags to ensure a known stateMultiLangStringControl.reset_flags()# Printing the current state of all configuration flags in MultiLangStringControlprint("Current flag states in MultiLangStringControl:")MultiLangStringControl.print_flags()# Expected Output: Default states of all flags, as follows:# MultiLangStringFlag.ENSURE_TEXT = True# MultiLangStringFlag.ENSURE_ANY_LANG = False# MultiLangStringFlag.ENSURE_VALID_LANG = FalseCode TestingThe code provided has undergone rigorous testing to ensure its reliability and correctness. The tests can be found in the 'tests' directory of the project. To run the tests, navigate to the project root directory and execute the following command:langstring>pytest.\testsVersion 2: Key Differences and ImprovementsThe LangString Library has undergone significant enhancements from Version 1 (V1) to Version 2 (V2). These changes have improved the library's functionality and usability, particularly in terms of control mechanisms and handling multilingual strings. Below are the key differences and improvements:Global Control AvailabilityV1: Global control was not available for bothLangStringandMultiLangString.V2: Global control has been introduced for bothLangStringandMultiLangString, allowing for more flexible and centralized management of behavior across all instances.Individual Level ControlV1: Individual level control was only available forMultiLangString, with no control options forLangString.V2: BothLangStringandMultiLangStringnow have global control capabilities, enhancing consistency and ease of configuration.Handling of Multilingual StringsV1:MultiLangStringwas designed to handle a single string associated with multiple languages, allowing for synonyms.V2: The internal structure ofMultiLangStringhas been rebuilt. It now handles multiple strings associated with multiple languages, still allowing for synonyms. This change provides more versatility in managing multilingual content.Method Restructuring in MultiLangStringV2: Methods inMultiLangStringhave been restructured, offering more options and improved functionality in v2 compared to v1.Dependency Between MultiLangString and LangStringV1:MultiLangStringswere entirely dependent onLangStrings.V2: This dependency has been removed.MultiLangStringnow operates independently ofLangString, providing more flexibility and reducing coupling between these two components.These improvements in v2 of the LangString Library mark a significant step forward in its capability to handle multilingual text data, offering users more control and flexibility in their applications.How to ContributeReporting IssuesIf you find a bug or wish to suggest a feature, pleaseopen a new issue.If you notice any discrepancies in the documentation created with the aid of AI, feel free toreport them by opening an issue.Code ContributionsFork the project repository and create a new feature branch for your work:git checkout -b feature/YourFeatureName.Make and commit your changes with descriptive commit messages.Push your work back up to your fork:git push origin feature/YourFeatureName.Submit a pull request to propose merging your feature branch into the main project repository.Test ContributionsEnhance the project's reliability by adding new tests or improving existing ones.General GuidelinesEnsure your code follows our coding standards.Update the documentation as necessary.Make sure your contributions do not introduce new issues.We appreciate your time and expertise in contributing to this project!DependenciesThe LangString Python Library is designed with simplicity and ease of use in mind. To achieve this, we have minimized external dependencies.The LangString Library depends only on thelangcodes package, particularly for validating language tags when theENSURE_VALID_LANGflag is enabled. This dependency is crucial for ensuring that language tags used inLangStringandMultiLangStringinstances are valid and conform to international standards, thereby maintaining the integrity and reliability of multilingual text processing.This project can be set up using either Poetry orrequirements.txt. Both are kept in sync to ensure consistency in dependencies.Using PoetryPoetryis used for easy management of dependencies and packaging. To install the dependencies with Poetry, firstinstall Poetryif you haven't already, and then run:poetryinstallThis will install all the dependencies as specified inpyproject.toml.Usingrequirements.txtIf you prefer not to use Poetry, arequirements.txtfile is also provided. You can install the dependencies using pip:pipinstall-rrequirements.txtThis is a straightforward way to set up the project if you are accustomed to using pip and traditional requirements files.LicenseThis project is licensed under the Apache License 2.0. See theLICENSEfile for details.AuthorThis project is an initiative of theSemantics, Cybersecurity & Services (SCS) Groupat theUniversity of Twente, The Netherlands. The main developer is:Pedro Paulo Favato Barcelos [GitHub] [LinkedIn]Feel free to reach out using the provided links. For inquiries, contributions, or to report any issues, you canopen a new issueon this repository.