DEV Community

yosleycarrero2025
yosleycarrero2025

Posted on

Detecting Word Attraction and Repulsion in Text with wordorientation

wordorientation is an R package that measures how strongly pairs of words attract or repel each other in a text corpus.

For every pair of words, it computes the phi coefficient (a correlation measure for co-occurrence data), tests it for statistical significance, and classifies the pair as:

  • attraction — the words co-occur more often than chance would predict
  • repulsion — the words co-occur less often than chance would predict
  • neutral — no significant relationship

Unlike general-purpose correlation tools, it's built specifically for text: it handles tokenization and stopword removal automatically, applies proper significance testing rather than just reporting a raw correlation, and produces a ready-to-plot attraction/repulsion network.

Installation

install.packages("wordorientation")
Enter fullscreen mode Exit fullscreen mode

Basic usage

library(wordorientation)

result <- analyze_word_orientation(
  my_data,
  text_col  = "text",
  doc_col   = "id",
  min_count = 2
)

head(result$scored)
plot_orientation_network(result$scored)
Enter fullscreen mode Exit fullscreen mode

Each row of your data is treated as one "document" — the unit within which two words are said to co-occur.

One thing worth knowing: the package needs a large corpus (survey responses, social media posts, reviews, or similar) to have enough statistical power to find real patterns. On a single short text, it will correctly report few or no significant pairs rather than manufacturing noise into a misleading result.

Full source and documentation: https://github.com/yosleycarrero2025/wordorientation

Feedback and issues welcome!

Top comments (0)