Skip to content

Commit

Permalink
UPDATE 0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
davidycliao committed Nov 5, 2023
1 parent eef1f28 commit 54d10a9
Show file tree
Hide file tree
Showing 11 changed files with 598 additions and 53 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export(load_tagger_pos)
export(load_tagger_sentiments)
export(map_entities)
export(show_flair_cache)
export(uninstall_python_package)
import(reticulate)
importFrom(attempt,stop_if_all)
importFrom(data.table,":=")
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

* The major module _flair_ in {`flaiR`} should be renamed from `flair()` to `import_flair()` to avoid overlapping with conventional practice `import flair` in Python.

* _install_python_package()_ and _uninstall_python_package()_ are new functions to install and uninstall Python packages using pip in the environment used by your flaiR package.

* `zzz.R` is a revised code that proceeds through three steps. First, when installing and loading the package, {flaiR} utilizes the system's environment tool and undergoes three evaluation stages. Initially, {flaiR} requires at least Python 3 to be installed on your device. If Python 3 is not available, you will be unable to install {flaiR} successfully. Once this requirement is met, the system then checks for the appropriate versions of PyTorch and Flair. The primary focus here is on Flair. If it is not already installed, you will see a message indicating that 'Flair is being installed from Python'. This process represents a new format for loading the Python environment used by your flaiR package.




</div>


Expand Down
427 changes: 427 additions & 0 deletions R/flair_datasets.html

Large diffs are not rendered by default.

49 changes: 34 additions & 15 deletions R/flar_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,46 @@
#' gives access to various classes and utilities in the `flair.data` module,
#' most notably:
#' \itemize{
#' \item \strong{Sentence}: Represents a sentence, which is a list of
#' Tokens. This class provides various utilities for sentence
#' manipulation, such as adding tokens, tagging with pre-trained models,
#' and obtaining embeddings.
#' \item \strong{Token}: Represents a word or a sub-word unit in a sentence.
#' It can carry various annotations such as named entity tags, part-of-speech
#' tags, and embeddings. Additionally, the token provides functionalities
#' to retrieve or check its annotations.
#' \item \strong{Corpus}: Represents a collection of sentences,
#' \item \strong{BoundingBox(left, top, right, bottom)}: Bases: tuple (Python); list (R)
#' \itemize{
#' \item left - str. Alias for field number 0.
#' \item top - int Alias for field number 1
#' \item right - int Alias for field number 2
#' \item bottom - int Alias for field number 3
#' }
#'
#' \item \strong{Sentence(text, use_tokenizer=True, language_code=None,
#' start_position=0)}:A Sentence is a list of tokens and is used to
#' represent a sentence or text fragment. `Sentence` can be imported by
#' `flair_data()$Sentence` via {flaiR}.
#' \itemize{
#' \item text \code{Union[str, List[str], List[Token]]} - The original string (sentence), or a pre-tokenized list of tokens.
#' \item use_tokenizer \code{Union[bool, Tokenizer]} - Specify a custom tokenizer to split the text into tokens. The default is \code{flair.tokenization.SegTokTokenizer}. If \code{use_tokenizer} is set to \code{False}, \code{flair.tokenization.SpaceTokenizer} will be used instead. The tokenizer will be ignored if \code{text} refers to pre-tokenized tokens.
#' \item language_code \code{Optional[str]} - Language of the sentence. If not provided, \code{langdetect} will be called when the \code{language_code} is accessed for the first time.
#' \item start_position \code{int} - Start character offset of the sentence in the superordinate document.
#' }
#' \item \strong{Span(tokens, tag=None, score=1.0)}: Bases: _PartOfSentence.
#' A Span is a slice of a Sentence, consisting of a list of Tokens.
#' `Span` can be imported by `flair_data()$Span`.
#'
#' \item \strong{Token(text, head_id=None, whitespace_after=1, start_position=0, sentence=None)}:
#' This class represents one word in a tokenized sentence.
#' Each token may have any number of tags. It may also point to its head in a
#' dependency tree. `Token` can be imported by `flair_data()$Token` via {flaiR}.
#'
#' \item \strong{Corpus(train=None, dev=None, test=None, name='corpus', sample_missing_splits=True)}: Represents a collection of sentences,
#' facilitating operations like splitting into train/test/development
#' sets and applying transformations. It is particularly useful
#' for training and evaluating models on custom datasets.
#' `Corpus` can be imported by `flair_data()$Corpus` via {flaiR}.
#'
#' \item \strong{Dictionary}: Represents a mapping between items and indices.
#' It is useful for converting text into machine-readable formats.
#' }
#' Additionally, the module offers utilities for reading data in the CoNLL
#' format, a common format for NER, POS tagging, and more. It also contains
#' the `Dictionary` class for item-index mapping, facilitating the conversion
#' of text into machine-readable formats. This function provides a bridge
#' to access these functionalities directly from R.
#'
#' @return A Python module (`flair.data`).
#' @return A Python module (`flair.data`). To access the classes and utilities.
#'
#' @seealso [flair.data](https://flairnlp.github.io/flair/master/api/flair.data.html#)
#' @export
#'
#' @examples
Expand Down
1 change: 1 addition & 0 deletions R/toolbox.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ highlight_text <- function(text, entities_mapping, font_family = "Arial") {
return(HTML(justified_text))
}


#' @title Create Mapping for NER Highlighting
#'
#' @description This function generates a mapping list for Named Entity Recognition (NER)
Expand Down
40 changes: 40 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,44 @@ install_python_package <- function(package_name, package_version = NULL, python_
# ))
# }

#' @title Uninstall a Python Package
#'
#' @description `uninstall_python_package` function uninstalls a specified Python
#' package using the system's Python installation. It checks if Python is
#' installed and accessible, then proceeds to uninstall the package. Finally,
#' `uninstall_python_package` verifies that the package has been successfully uninstalled.
#'
#' @param package_name The name of the Python package to uninstall.
#' @param python_path The path to the Python executable. If not provided, it uses the system's default Python path.
#'
#' @return Invisibly returns TRUE if the package is successfully uninstalled, otherwise it stops with an error message.
#' @export
#'
#' @examples
#' \dontrun{
#' uninstall_python_package("numpy")
#' }
uninstall_python_package <- function(package_name, python_path = Sys.which("python3")) {
# Check if Python is installed or found in the system PATH
if (python_path == "") {
stop("Python is not installed, not found in the system PATH, or an incorrect path was provided.")

Check warning on line 504 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L503-L504

Added lines #L503 - L504 were not covered by tests
} else {
message("Using Python at: ", python_path)

Check warning on line 506 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L506

Added line #L506 was not covered by tests
}

# Uninstall the specified package
uninstall_command <- paste(python_path, "-m pip uninstall -y", package_name)
system(uninstall_command)

Check warning on line 511 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L510-L511

Added lines #L510 - L511 were not covered by tests

# Check if the package is still installed
check_uninstall_command <- paste(python_path, "-c 'import ", package_name, "'", sep="")
package_uninstall_check <- try(system(check_uninstall_command, intern = TRUE, ignore.stderr = TRUE), silent = TRUE)

Check warning on line 515 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L514-L515

Added lines #L514 - L515 were not covered by tests

if (inherits(package_uninstall_check, "try-error")) {
message("Package '", package_name, "' was successfully uninstalled.")
invisible(TRUE)

Check warning on line 519 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L517-L519

Added lines #L517 - L519 were not covered by tests
} else {
stop("Failed to uninstall the package. It may still be installed.")

Check warning on line 521 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L521

Added line #L521 was not covered by tests
}
}

44 changes: 23 additions & 21 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ reference:
desc: <div class="text-justify">This is a very high-level overview of flair. Accessing whole flair module. The flair library is a powerful NLP (Natural Language Processing) tool based on PyTorch. Its primary focus is to provide state-of-the-art models for NLP tasks while keeping the API simple and easy to use.</div>

- contents:
- flair
- import_flair

- title: The flair_embeddings Module (Python)
desc: <div class="text-justify">Accessing flair.embeddings module from flair. `flair.embeddings` is a module from the Flair NLP library, which provides several state-of-the-art embeddings (vector representations) for natural language processing (NLP) tasks. Embeddings are crucial in NLP because they allow models to understand the semantic content of words, capturing relationships between words, similarities, and much more. The Flair library provides a rich set of embeddings, which can be either standalone or combined to enhance performance on various tasks.</div>
Expand Down Expand Up @@ -92,6 +92,8 @@ reference:
- embeddings_to_matrix
- clear_flair_cache
- show_flair_cache
- uninstall_python_package
- install_python_package


- title: flairR Tutorial Dataset
Expand Down Expand Up @@ -175,26 +177,26 @@ navbar:
menu:
- text: "All Function Reference"
href: reference/index.html
- text: "The flair Module"
href: reference/index.html#the-flair-module
- text: "The flair_embeddings Module"
href: reference/index.html#the-flair-embeddings-module
- text: "The flair_data Module"
href: reference/index.html#the-flair-data-module
- text: "The flair_nn Module"
href: reference/index.html#the-flair-nn-module
- text: "The flair_trainers Module"
href: reference/index.html#the-flair-trainers-module
- text: "The flair_splitter Module"
href: reference/index.html#the-flair-splitter-module
- text: "The flair_models Module"
href: reference/index.html#the-flair-models-module
- text: "The flair_datasets Module"
href: reference/index.html#the-flair-datasets-module
- text: "The Pre-trianed Models for NLP Tasks"
href: articles/flair_models.html
- text: "The Wordembeddings in Flair"
href: articles/transformer_wordembeddings.html
# - text: "The flair Module"
# href: reference/index.html#the-flair-module
# - text: "The flair_embeddings Module"
# href: reference/index.html#the-flair-embeddings-module
# - text: "The flair_data Module"
# href: reference/index.html#the-flair-data-module
# - text: "The flair_nn Module"
# href: reference/index.html#the-flair-nn-module
# - text: "The flair_trainers Module"
# href: reference/index.html#the-flair-trainers-module
# - text: "The flair_splitter Module"
# href: reference/index.html#the-flair-splitter-module
# - text: "The flair_models Module"
# href: reference/index.html#the-flair-models-module
# - text: "The flair_datasets Module"
# href: reference/index.html#the-flair-datasets-module
# - text: "The Pre-trianed Models for NLP Tasks"
# href: articles/flair_models.html
# - text: "The Wordembeddings in Flair"
# href: articles/transformer_wordembeddings.html

- icon: fa-newspaper-o
text: News
Expand Down
51 changes: 36 additions & 15 deletions man/flair_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/uninstall_python_package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/quickstart.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ __System Requirement:__
- Anaconda ___(highly recommended)___


First, When first installing and loading the package, {flaiR} uses the system tool and goes through three evaluation stages. First and foremost, {flaiR} requires you to have at least Python 3 installed. If your device does not have Python 3, you will not be able to successfully install {flaiR}. Once this step is successful, the system further assesses whether you have the appropriate versions of PyTorch and Flair installed. The primary focus is on Flair, and if you haven't installed it, the message "Flair is installing from Python" will be displayed.
First, When first installing and loading the package, {flaiR} uses the system environment tool and goes through three evaluation stages. First, {flaiR} requires you to have at least Python 3 installed. If your device does not have Python 3, you will not be able to successfully install {flaiR}. Once this step is successful, the system further assesses whether you have the appropriate versions of PyTorch and Flair installed. The primary focus is on Flair, and if you haven't installed it, the message "Flair is installing from Python" will be displayed.

During this process, you will observe numerous messages related to the installation of the Python environment and the Python flair module. Notably, flair has numerous dependencies, including libraries related to transformers (like the Pytorch, gensim, flair, etc). Thus, the installation might take some time to complete.

Expand Down
2 changes: 1 addition & 1 deletion vignettes/tutorial.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ knitr::opts_chunk$set(
system(paste(Sys.which("python3"), "-m pip install --upgrade pip"))
system(paste(Sys.which("python3"), "-m pip install torch"))
system(paste(Sys.which("python3"), "-m pip install flair"))
library(reticulate)
# Sys.setenv(RETICULATE_PYTHON = Sys.which("python3"))
library(flaiR)
# system(paste(reticulate::py_config()$python, "-m pip install flair"))
# reticulate::py_install("flair")
Expand Down

0 comments on commit 54d10a9

Please sign in to comment.