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

Teradata® Python Package Function Reference

Product
Teradata Python Package
Release Number
16.20
Published
February 2020
Language
English (United States)
Last Update
2020-07-17
lifecycle
previous
Product Category
Teradata Vantage

 
teradataml.analytics.sqle.NGramSplitter = class NGramSplitter(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_order_column=None)
DESCRIPTION:
    The NGramSplitter function tokenizes (splits) an input stream of text and 
    outputs n multigrams (called n-grams) based on the specified 
    delimiter and reset parameters. NGramSplitter 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.
 
    Note: This function is only available when teradataml is connected
          to Vantage 1.1 or later versions.
 
 
PARAMETERS:
    data:
        Required Argument.
        Specifies input teradataml DataFrame, where each row contains a document 
        to be tokenized. The input teradataml DataFrame can have additional rows, 
        some or all of which the function returns in the output table.
    
    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.
        Specifies a 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.
        Specifies 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 has 
        the syntax integer1 - integer2, where integer1 <= integer2. The values 
        of n, integer1, and integer2 must be positive.
        Types: str OR list of strs
    
    overlapping:
        Optional Argument.
        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.
        Specifies whether the function converts all letters in the input text 
        to lowercase. 
        Default Value: True
        Types: bool
    
    punctuation:
        Optional Argument.
        Specifies the punctuation characters for the function to remove before 
        evaluating the input text.
        Default Value: "`~#^&*()-"
        Types: str
    
    reset:
        Optional Argument.
        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.
        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.
        Specifies the names of the columns 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.
        Specifies 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
 
RETURNS:
    Instance of NgramSplitter.
    Output teradataml DataFrames can be accessed using attribute 
    references, such as NgramSplitterObj.<attribute_name>.
    Output teradataml DataFrame attribute name is:
        result
 
 
RAISES:
    TeradataMlException
 
 
EXAMPLES:
    # Load example data.
    load_example_data("NGrams","paragraphs_input")
    
    # Create teradataml DataFrame
    paragraphs_input = DataFrame.from_table("paragraphs_input")
    
    # Example 1
    # Creates output for tokenized data on grams values
    NGramSplitter_out1 = NGramSplitter(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(NGramSplitter_out1.result)
 
    # Example 2
    # Creates total count column with default column totalcnt if total_gram_count is specified as False
    NGramSplitter_out2 = NGramSplitter(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(NGramSplitter_out2.result)
__repr__(self)
Returns the string representation for a NgramSplitter class instance.