Giter VIP home page Giter VIP logo

textych's Introduction

textych

CRAN log

The goal of textych is to create interactive text parallels. This form of reference is useful for exploring similarities and differences betwixt passages.

Installation

You can install the released version of textych from GitHub with:

remotes::install_github("daranzolin/textych")

Simple Example

Split any text into words and assign a corresponding color and tooltip.

library(textych)
library(tidytext)
library(dplyr)

df <- tibble(
  text = c("The quick brown fox jumps over the lazy grey dog",
           "The catepiller ate through once nice green leaf"),
  ind = c("A", "B")
) %>% 
  unnest_tokens(word, text, to_lower = FALSE) %>% 
  mutate(
    color = case_when(
      word == "brown" ~ "brown",
      word == "grey" ~ "grey",
      word == "green" ~ "green",
      TRUE ~ "#333333"
    ),
    tooltip = case_when(
      word == "caterpillar" ~ "An insect",
      word %in% c("fox", "dog") ~ "A cute mammal",
      word == "leaf" ~ "Vegetation"
    )
  )

textych(df, text = word, text_index = ind, color = color, tooltip = tooltip)

Complex Example: Greek Text Analysis

Arranging parallel texts with similar language and ideas is a common practice in textual analysis, and there is very expensive software that parses each word's form, tense, mood, gender, case, etc. This is a cheaper (and more customizable) alternative.

First, I load the packages, then retrieve and parse the texts via rperseus.

library(rperseus) # remotes::install_github("ropensci/rperseus")
library(glue)

texts <- bind_rows(
  get_perseus_text("urn:cts:greekLit:tlg0031.tlg012.perseus-grc2", "1.4"),
  get_perseus_text("urn:cts:greekLit:tlg0031.tlg013.perseus-grc2", "1.3"),
  get_perseus_text("urn:cts:greekLit:tlg0031.tlg006.perseus-grc2", "8.39")
)

parsed_texts <- bind_rows(
  parse_excerpt("urn:cts:greekLit:tlg0031.tlg012.perseus-grc2", "1.4"),
  parse_excerpt("urn:cts:greekLit:tlg0031.tlg013.perseus-grc2", "1.3"),
  parse_excerpt("urn:cts:greekLit:tlg0031.tlg006.perseus-grc2", "8.39")
)

Second, I want to (1) specify title labels; (2) color the word ἀγάπη ("love"); and (3) create a custom HTML tooltip parsing each word on hover.

tt_data <- texts %>% 
  transmute(
    text,
    passage = glue("{label} {section}")
  ) %>% 
  unnest_tokens(word, text) %>% 
  left_join(
    distinct(parsed_texts, word, form, .keep_all = TRUE), 
    by = c("word" = "form")
  ) %>% 
  mutate(color = ifelse(grepl("ἀγάπη", word), "firebrick", "#333333")) %>% 
  mutate(tooltip = glue("<table>
                              <tr>
                                  <th>word</th>
                                  <th>part</th>
                                  <th>number</th>
                                  <th>gender</th>
                                  <th>case</th>
                              </tr>
                              <tr>
                                  <td>{word.y}</td>
                                  <td>{part_of_speech}</td>
                                  <td>{number}</td>
                                  <td>{gender}</td>
                                  <td>{case}</td>
                              </tr>
                        </table>
                              ")
         )

Finally, I pass the data to textych, specifying the respective columns for each parallel, text color, and tooltip.

textych(tt_data, word, passage, color, tooltip)

Future work

  • Highlighting words
  • More easily customizable tooltips
  • Additional styling
  • Improved margins

textych's People

Contributors

daranzolin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.