handle_negation: Handle Negation in Token List

View source: R/save_negation.R

handle_negationR Documentation

Handle Negation in Token List

Description

This function processes a character vector of tokens and handles negations by combining the word "not" with the immediately following word (e.g., "not happy" becomes "not_happy"). This technique helps to better preserve sentiment polarity during text analysis.

Usage

handle_negation(tokens)

Arguments

tokens

A character vector of tokens (individual words).

Details

The negation handling procedure follows these steps:

  • Iterate through each token.

  • If a token is "not" and followed by another token, merge them into a single token separated by an underscore (e.g., "not_happy").

  • Skip the next token after merging to avoid duplication.

  • Otherwise, keep the token unchanged.

This method is especially useful in sentiment analysis tasks where the presence of negations can invert the sentiment polarity of words.

Value

A character vector of tokens with negations handled by combining "not" with the next word.

Examples

handle_negation(c("i", "am", "not", "happy"))
# Returns: c("i", "am", "not_happy")

handle_negation(c("this", "is", "not", "good", "but", "not", "terrible"))
# Returns: c("this", "is", "not_good", "but", "not_terrible")

handle_negation(c("nothing", "to", "worry", "about"))
# Returns: c("nothing", "to", "worry", "about")


text2emotion documentation built on June 8, 2025, 1:04 p.m.