An open API service indexing awesome lists of open source software.

https://github.com/trinker/formality


https://github.com/trinker/formality

formality r text-measure text-mining

Last synced: 11 months ago
JSON representation

Awesome Lists containing this project

README

          

---
title: "formality"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
md_document:
toc: true
---

```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(knitr)
desc <- suppressWarnings(readLines("DESCRIPTION"))
regex <- "(^Version:\\s+)(\\d+\\.\\d+\\.\\d+)"
loc <- grep(regex, desc)
ver <- gsub(regex, "\\2", desc[loc])
verbadge <- sprintf('Version', ver, ver)
````

```{r, echo=FALSE}
knit_hooks$set(htmlcap = function(before, options, envir) {
if(!before) {
paste('

',options$htmlcap,"

",sep="")
}
})
knitr::opts_knit$set(self.contained = TRUE, cache = FALSE)
knitr::opts_chunk$set(fig.path = "tools/figure/")
```

[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/0.1.0/active.svg)](http://www.repostatus.org/#active)
[![Build Status](https://travis-ci.org/trinker/formality.svg?branch=master)](https://travis-ci.org/trinker/formality)
[![Coverage Status](https://coveralls.io/repos/trinker/formality/badge.svg?branch=master)](https://coveralls.io/r/trinker/formality?branch=master)
`r verbadge`

![](tools/formality_logo/r_formality.png)

**formality** utilizes the [**tagger**](https://github.com/trinker/tagger) package to conduct formality analysis. Heylighen (1999) and Heylighen & Dewaele (2002, 1999) have given the *F-measure* as a measure of how *contextual* or *formal* language is. Language is considered more formal when it contains much of the information directly in the text, whereas, contextual language relies on shared experiences to more efficiently dialogue with others.

# Formality Equation

The **formality** package's main function is also titled `formality` and uses Heylighen & Dewaele's (1999) *F-measure*. The *F-measure* is defined formally as:

$$F = 50(((n_f - n_c)/N) + 1)$$

Where:

$$f = \{noun, adjective, preposition, article\}$$
$$c = \{pronoun, verb, adverb, interjection\}$$
$$N = n_f + n_c$$

This yields an *F-measure* between $0$ and $100$%, with completely contextualized language on the zero end and completely formal language on the $100$ end.

Please see the following references for more details about formality and the *F-measure*:

- Heylighen, F. (1999). Advantages and limitations of formal expression. Foundations of Science, 4, 25-56. doi:10.1023/A:1009686703349
- Heylighen, F. & Dewaele, J.-M. (1999). Formality of language: Definition, measurement and behavioral determinants. Center "Leo Apostel", Free University of Brussels. Retrieved from [http://pespmc1.vub.ac.be/Papers/Formality.pdf](http://pespmc1.vub.ac.be/Papers/Formality.pdf)
- Heylighen, F. & Dewaele, J.-M. (2002). Variation in the contextuality of language: An empirical measure. Foundations of Science, 7(3), 293-340. doi:10.1023/A:1019661126744

# Installation

To download the development version of **formality**:

Download the [zip ball](https://github.com/trinker/formality/zipball/master) or [tar ball](https://github.com/trinker/formality/tarball/master), decompress and run `R CMD INSTALL` on it, or use the **pacman** package to install the development version:

```r
if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh(c(
"trinker/termco",
"trinker/tagger",
"trinker/formality"
))
```

# Contact

You are welcome to:
* submit suggestions and bug-reports at:
* send a pull request on:
* compose a friendly e-mail to:

# Examples

The following examples demonstrate some of the functionality of **formality**.

## Load the Tools/Data

```{r}
library(formality)
data(presidential_debates_2012)
```

## Assessing Formality

`formality` takes the text as `text.var` and any number of grouping variables as `grouping.var`. Here we use the `presidential_debates_2012` data set and look at the formality of the people involved. Note that for smaller text Heylighen & Dewaele (2002) state:

> At present, a sample would probably need to contain a few hundred words for the measure to be minimally reliable. For single sentences, the F-value should only be computed for purposes of illustration" (p. 24).

```{r}
form1 <- with(presidential_debates_2012, formality(dialogue, person))
form1
```

## Recycling the First Run

This will take ~20 seconds because of the part of speech tagging that must be undertaken. The output can be reused as `text.var`, cutting the time to a fraction of the first run.

```{r}
with(presidential_debates_2012, formality(form1, list(time, person)))
```

## Plotting

The generic `plot` function provides three views of the data:

1. A filled bar plot of formal vs. contextual usage
2. A dotplot of formality\*\*
3. A heatmap of the usage of the parts of speech used to calculate the formality score

\*\****Note*** *red dot in center is a warning of less than 300 words*

```{r}
plot(form1)
````

The `plot` function uses **gridExtra** to stitch the plots together, which is plotted imediately. However, the three subplots are actually returned as a list as seen below.

```{r}
names(plot(form1, plot=FALSE))
```

Each of these is a **ggplot2** object that can be further manipulated with various scales, facets, and annotations. I demonstrate some of this functionality in the plots below.

```{r, warning=FALSE, message=FALSE}
library(ggplot2)
plot(form1, plot=FALSE)[[1]] +
scale_size(range= c(8, 45)) +
scale_x_continuous(limits = c(52, 63))

plot(form1, plot=FALSE)[[2]] +
scale_fill_grey()

plot(form1, plot=FALSE)[[2]] +
scale_fill_brewer(palette = "Pastel1") +
facet_grid(~type)

plot(form1, plot=FALSE)[[3]] +
scale_fill_gradient(high = "red", low="white") +
ggtitle("Participant's Use of Parts of Speech")

plot(form1, plot=FALSE)[[3]] +
scale_fill_gradient2(midpoint=.12, high = "red", low="blue")
```