Edit

Share via


Fix grammar with the ai.fix_grammar function

The ai.fix_grammar function uses Generative AI to correct the spelling, grammar, and punctuation of input text—all with a single line of code.

AI functions turbocharge data engineering by putting the power of Fabric's built-in large languages models into your hands. To learn more, visit this overview article.

Important

This feature is in preview, for use in the Fabric 1.3 runtime and higher.

  • Review the prerequisites in this overview article, including the library installations that are temporarily required to use AI functions.
  • By default, AI functions are currently powered by the gpt-3.5-turbo (0125) model. To learn more about billing and consumption rates, visit this article.
  • Although the underlying model can handle several languages, most of the AI functions are optimized for use on English-language texts.
  • During the initial rollout of AI functions, users are temporarily limited to 1,000 requests per minute with Fabric's built-in AI endpoint.

Use ai.fix_grammar with pandas

The ai.fix_grammar function extends the pandas Series class. Call the function on a pandas DataFrame text column to correct the spelling, grammar, and punctuation of each row of input.

The function returns a pandas Series that contains corrected text values, which can be stored in a new DataFrame column.

Syntax

df["corrections"] = df["text"].ai.fix_grammar()

Parameters

None

Returns

The function returns a pandas Series that contains corrected text for each input text row. If the input text is null, the result is null.

Example

# This code uses AI. Always review output for mistakes. 
# Read terms: https://azure.microsoft.com/support/legal/preview-supplemental-terms/

df = pd.DataFrame([
        "There are an error here.",
        "She and me go weigh back. We used to hang out every weeks.",
        "The big picture are right, but you're details is all wrong."
    ], columns=["text"])

df["corrections"] = df["text"].ai.fix_grammar()
display(df)

Use ai.fix_grammar with PySpark

The ai.fix_grammar function is also available for Spark DataFrames. The name of an existing input column must be specified as a parameter.

The function returns a new DataFrame, with corrected text for each input text row stored in an output column.

Syntax

df.ai.fix_grammar(input_col="text", output_col="corrections")

Parameters

Name Description
input_col
Required
A string containing the name of an existing column with input text values to be corrected for spelling, grammar, and punctuation.
output_col
Optional
A string containing the name of a new column to store corrected text for each row of input text. If this parameter isn't set, a default name is generated for the output column.
error_col
Optional
A string containing the name of a new column to store any OpenAI errors that result from processing each row of input text. If this parameter isn't set, a default name is generated for the error column. If there are no errors for a row of input, the value in this column is null.

Returns

A Spark DataFrame with a new column containing corrected text for each row of text in the input column. If the input text is null, the result is null.

Example

# This code uses AI. Always review output for mistakes. 
# Read terms: https://azure.microsoft.com/support/legal/preview-supplemental-terms/

df = spark.createDataFrame([
        ("There are an error here.",),
        ("She and me go weigh back. We used to hang out every weeks.",),
        ("The big picture are right, but you're details is all wrong.",)
    ], ["text"])

results = df.ai.fix_grammar(input_col="text", output_col="corrections")
display(results)