def test_read_file(self):
     """
     Test to read a file
     """
     text = read_file(__file__)  # Get current filename
     # The length must be greater than 0
     self.assertTrue(len(text) > 0)
Exemple #2
0
def get_suggestions(language: str, config_file: str = None):
    """
     This method can be used to obtain the suggestions for a given language as
     configured in the configuration
     file. If the configuration file is not specified, the default configuration file is used

    Parameters
    ----------
      language : str
        This parameter is the language code or locale (en: English)
      config_file : str
        This parameter contains the file path of a congiguration file

    Returns
    -------
      suggestions
        suggestions for a given language

    """
    config_file = get_all_language_resource_config_file(config_file)
    resources = get_all_language_resources(config_file)
    suggestions = {}

    # Load all the suggestion files for a given language
    if language in resources:
        for suggestion_file in resources[language]:
            suggestion_file = pkg_resources.resource_filename(
                "inclusivewriting", suggestion_file)
            suggestion_file_data = read_file(suggestion_file)
            suggestion = json.loads(suggestion_file_data)
            for key, value in suggestion.items():
                lexeme = Lexeme(key, value["lexeme"])
                replacements = []
                for replacement in value["replacement"]:
                    replacement_lexeme = Lexeme(
                        replacement,
                        value["replacement"][replacement]["lexeme"])
                    replacement = Replacement(
                        replacement_lexeme,
                        value["replacement"][replacement]["references"],
                    )
                    replacements.append(replacement)

                suggestion_value = Suggestion(lexeme, replacements)
                suggestions[key] = suggestion_value

    return suggestions
def get_all_punctuation_separator_characters_from_resources(
        config_file: str = None):
    """
    Get all punctuation and characters, i.e., unicode category
    belonging to P or Z
    """
    punctuations_separator = set()
    config_file = get_all_language_resource_config_file(config_file)
    resources = get_all_language_resources(config_file)

    if "separators" in resources:
        separator_file = pkg_resources.resource_filename(
            "inclusivewriting", resources["separators"][0])
        punctuations_separator = read_file(separator_file)
        punctuations_separator = set(punctuations_separator.split("|"))
    else:
        punctuations_separator = get_all_punctuation_separator_characters()

    return punctuations_separator
Exemple #4
0
def detect(
    language: str,
    config: str = typer.Option(None,
                               help="Use a different configuration file"),
    filepath: str = typer.Option(
        None,
        help="File name; if missing, you will be prompted to enter a text"),
):
    """
    Detect possible issues in an input text or from a file and
    show possible suggestions
    """
    _ = get_default_locale_message_handler()
    if language is None:
        language, _ = get_default_locale_encoding()
    text = None
    if filepath is None:
        rich.print(_("Enter [bold magenta]a text[/bold magenta]."), end="")
        rich.print(_(" Press [bold magenta]Ctrl+D[/bold magenta] to exit:"))
        text = read_input_from_terminal()
    else:
        text = read_file(filepath)
    used_suggestions, suggestions, updated_text = detect_and_get_suggestions(
        language, text, config)
    updated_text = updated_text.replace("<change>", "[bold green]")
    updated_text = updated_text.replace("</change>", "[/bold green]")
    rich.print(updated_text)
    rich.print()
    if len(used_suggestions) > 0:
        rich.print("Following are some suggested replacements:")
        for word in used_suggestions:
            rich.print("[bold green]" + word + " [/bold green]: ", end="")
            for replacement_lexeme in suggestions[
                    word.lower()].get_replacement_lexemes():
                rich.print(
                    "[bold blue]" + replacement_lexeme.get_value() +
                    "[/bold blue]",
                    end="; ",
                )
            rich.print()
    rich.print()
def get_all_language_resources(config_file: str = None):
    """
     This method can be used to obtain all the language resources configured in the configuration
     file. If the configuration file is not specified, the default configuration file is used

    Parameters
    ----------
      config_file : str
        This parameter contains the file path of a congiguration file

    Returns
    -------
      resources
        language resources

    """
    config_file = get_all_language_resource_config_file(config_file)
    resources = {}
    resource_file_string = read_file(config_file)
    resources_parsed_data = json.loads(resource_file_string)
    for key, value in resources_parsed_data.items():
        resources[key] = value
    return resources