Teradata Package for Python Function Reference - NGrams - Teradata Package for Python - Look here for syntax, methods and examples for the functions included in the Teradata Package for Python.

Teradata® Package for Python Function Reference

Product
Teradata Package for Python
Release Number
17.00
Published
November 2021
Language
English (United States)
Last Update
2021-11-19
lifecycle
previous
Product Category
Teradata Vantage

 
teradataml.analytics.mle.NGrams = class NGrams(builtins.object)
     Methods defined here:
__init__(self, data=None, text_column=None, delimiter='[\\s]+', grams=None, overlapping=True, to_lower_case=True, punctuation='[`~#^&*()-]', reset='[.,?!]', total_gram_count=False, total_count_column='totalcnt', accumulate=None, n_gram_column='ngram', num_grams_column='n', frequency_column='frequency', data_sequence_column=None, data_order_column=None)
DESCRIPTION:
    The NGrams function tokenizes (splits) an input stream of text and
    outputs n multigrams (called n-grams) based on the specified
    delimiter and reset parameters. NGrams provides more flexibility
    than standard tokenization when performing text analysis.
    Many two-word phrases carry important meaning (for example,
    "machine learning") that unigrams (single-word tokens) do not
    capture. This, combined with additional analytical techniques, can
    be useful for performing sentiment analysis, topic identification,
    and document classification.
 
PARAMETERS:
    data:
        Required Argument.
        Specifies an input teradataml DataFrame, and each row contains a document to be
        tokenized.
 
    data_order_column:
        Optional Argument.
        Specifies Order By columns for data.
        Values to this argument can be provided as a list, if multiple
        columns are used for ordering.
        Types: str OR list of Strings (str)
 
    text_column:
        Required Argument.
        Specifies the name of the column that contains the input text. The column
        must have a SQL string data type.
        Types: str
 
    delimiter:
        Optional Argument.
        A regular expression that specifies the character or string that
        separates words in the input text. The default value is the set of
        all whitespace characters which includes the characters for space,
        tab, newline, carriage return and some others.
        Default Value: "[\s]+"
        Types: str
 
    grams:
        Required Argument.
        A list of integers or ranges of integers that specify the length, in
        words, of each n-gram (that is, the value of n). A range of values from integer1 to integer2
        has the syntax integer1 - integer2, where integer1 <= integer2. The values
        of n, integer1, and integer2 must be positive.
        Types: str OR list of Strings (str)
 
    overlapping:
        Optional Argument.
        A Boolean value that specifies whether the function allows
        overlapping n-grams. When this value is True, each
        word in each sentence starts an n-gram, if enough words follow it (in
        the same sentence) to form a whole n-gram of the specified size. For
        information on sentences, see the description of the reset argument.
        Default Value: True
        Types: bool
 
    to_lower_case:
        Optional Argument.
        A Boolean value that specifies whether the function converts all
        letters in the input text to lowercase.
        Default Value: True
        Types: bool
 
    punctuation:
        Optional Argument.
        A regular expression that specifies the punctuation characters for
        the function to remove before evaluating the input text.
        Default Value: "[`~#^&*()-]"
        Types: str
 
    reset:
        Optional Argument.
        A regular expression that specifies the character or string that ends
        a sentence. At the end of a sentence, the function discards any
        partial n-grams and searches for the next n-gram at the beginning
        of the next sentence. An n-gram cannot span two sentences.
        Default Value: "[.,?!]"
        Types: str
 
    total_gram_count:
        Optional Argument.
        A Boolean value that specifies whether the function returns the total
        number of n-grams in the document (that is, in the row). If you
        specify True, then the name of the returned column is specified by
        the total.count.column argument.
        Note: The total number of n-grams is not necessarily the number of unique n-grams.
        Default Value: False
        Types: bool
 
    total_count_column:
        Optional Argument.
        Specifies the name of the column to return if the value of the total_gram_count
        argument is True.
        Default Value: "totalcnt"
        Types: str
 
    accumulate:
        Optional Argument.
        Specify the name of the column(s) to return for each n-gram. These columns
        cannot have the same names as those specified by the arguments n_gram_column,
        num_grams_column, and total_count_column. By default, the function
        returns all input columns for each n-gram.
        Types: str OR list of Strings (str)
 
    n_gram_column:
        Optional Argument.
        Specifies the name of the column that is to contain the generated n-grams.
        Default Value: "ngram"
        Types: str
 
    num_grams_column:
        Optional Argument.
        Specifies the name of the column that is to contain the length of n-gram (in
        words).
        Default Value: "n"
        Types: str
 
    frequency_column:
        Optional Argument.
        The name of the column that is to contain the count of each unique
        n-gram (that is, the number of times that each unique n-gram appears
        in the document).
        Default Value: "frequency"
        Types: str
 
    data_sequence_column:
        Optional Argument.
        Specifies the list of column(s) that uniquely identifies each row of
        the input argument "data". The argument is used to ensure
        deterministic results for functions which produce results that vary
        from run to run.
        Types: str OR list of Strings (str)
 
RETURNS:
    Instance of NGrams.
    Output teradataml DataFrames can be accessed using attribute
    references, such as NGramsObj.<attribute_name>.
    Output teradataml dataframe attribute name is:
        result
 
 
 
RAISES:
    TeradataMlException
 
 
EXAMPLES:
    # Load the data to run the example.
    load_example_data("NGrams","paragraphs_input")
 
    # Create teradataml DataFrame
    paragraphs_input = DataFrame.from_table("paragraphs_input")
 
    # Example 1
    ngrams_out1 = NGrams(data=paragraphs_input,
                        text_column='paratext',
                        delimiter = " ",
                        grams = "4-6",
                        overlapping=True,
                        to_lower_case=True,
                        total_gram_count=True,
                        accumulate=['paraid','paratopic']
                        )
 
    # Print the result DataFrame
    print(ngrams_out1.result)
 
    # Example 2
    # Creates total count column with default column totalcnt if total_gram_count is specified as False
    ngrams_out2 = NGrams(data = paragraphs_input,
                        text_column='paratext',
                        delimiter = " ",
                        grams = "4-6",
                        overlapping=False,
                        to_lower_case=True,
                        total_gram_count=False,
                        accumulate=['paraid','paratopic']
                       )
    # Print the result DataFrame
    print(ngrams_out2.result)
__repr__(self)
Returns the string representation for a NGrams class instance.
get_build_time(self)
Function to return the build time of the algorithm in seconds.
When model object is created using retrieve_model(), then the value returned is 
as saved in the Model Catalog.
get_prediction_type(self)
Function to return the Prediction type of the algorithm.
When model object is created using retrieve_model(), then the value returned is 
as saved in the Model Catalog.
get_target_column(self)
Function to return the Target Column of the algorithm.
When model object is created using retrieve_model(), then the value returned is 
as saved in the Model Catalog.
show_query(self)
Function to return the underlying SQL query.
When model object is created using retrieve_model(), then None is returned.