Giter Club home page Giter Club logo

covid.word.embedding's Introduction

covid.word.embedding

Lifecycle: experimental

The goal of this project is to provide a demo Shiny app that can be run on a user’s local environment to explore a GloVe model trained on the CORD-19 dataset.

Dependencies prior to setting up application locally

A user will need to have R and RStudio installed on their local environment. The following packages will need to be installed with any dependencies:

  • golem
  • thinkr
  • DT
  • text2ved
  • dplyr
  • tibble
  • magrittr
  • purrr

Running the application locally

A user can run this developmental Shiny application by pulling the repository to their local machine and opening the project in RStudio. This prototype was built using Golem. To run from RStudio, follow the directions as specified under 3. Day-to-day dev with golem part A. The code to be run is also shown in R code block below which can be found in covid.word.embedding/dev/run_dev.R file.

# Set options here
options(golem.app.prod = FALSE)

# Detach all loaded packages and clean your environment
golem::detach_all_attached()

# Document and reload your package
golem::document_and_reload()

# Run the application
covid.word.embedding::run_app()

How to use the application locally

After running the application locally, a user can interactively add or subtract word vectors produced by GloVe algorithm from the CORD-19 dataset, which can then identify the closest related words to the calculated word vector by cosine similarity. A positive number indicates a closer cosine similarity between the calculated word vector and the word. This article provides some background on adding and subtracting word vectors and the interesting relationships that can be uncovered. The purpose of this demo application is help users explore potentially related words/features from the CORD-19 dataset.

Background on the GloVe model training

The model was trainined using text2vec. An independent R project was set up and the CORD-19 dataset downloaded on 3/23/2020 into the following project folder PROJ_ROOT/rawdata/. The purpose of the code chunk below is to provide interested users an opportunity to explore creating their own GloVe models from the CORD-19 dataset and to understand how the GloVe model in the prototype app was generated. Given the large size of the dataset, this should be done independently.

## libraries --------
library(tidyverse)
library(magrittr)
library(DBI)
library(jsonlite)
library(text2vec)
library(tm)
library(SnowballC)

## set up --------
.PROJ_ROOT <- here::here()
.R_DIR <- file.path(.PROJ_ROOT, "R")
.RAW_DATA <- file.path(.PROJ_ROOT, "rawdata")
.RAW_PATH <- list.files(.RAW_DATA, recursive = T, full.names = T, pattern = ".json")

.STR_REMOVE <- paste0(c("The copyright holder.*preprint", "author.*permission", "The copyright holder.*funder", "CC-BY.*perpetuity","CC-BY.*funder", "All rights.*permission", "\\[[1-9]*\\]", "\\[[1-9]*"), collapse = "|")

metadata <- read_csv(file = file.path(.RAW_DATA, "metadata.csv"))

#Preprocessing functions-------------
prep_fun <- function(x){
  x <- tolower(x)
  x <- gsub(" +", " ", str_trim(x))
  x <- gsub("[0-9]", "", x)
  x <- gsub("\\(fig.* \\)", "", x)
  x <- gsub("\\.|,|!", "", x)
  x <- gsub(paste("\\b", stopwords(), "\\b", sep = "", collapse = "|"), "", x)
  x <- wordStem(x)
  x <- gsub(" +", " ", str_trim(x))
  return(x)
}

tmp <- list()

for(i in 1:length(.RAW_PATH)){
  print(i)
  tmp[[i]] <- try(fromJSON(txt = .RAW_PATH[[i]]))
}

tmp2 <- list()

for(i in 1:length(tmp)){
  print(i)
  tmp2[[i]] <- try(paste(gsub(pattern = .STR_REMOVE, replacement = "", tmp[[i]]$body_text$text), collapse = " "))
}

tmp3 <- list()

for(i in 1:length(tmp2)){
  print(i)
  tmp3[[i]] <- try(prep_fun(tmp2[[i]]))
}

tokens <- space_tokenizer(tmp3)
it = itoken(tokens, 
            ids = tmp$paper_id, 
            progressbar = TRUE)
vocab = create_vocabulary(it)
vocab = prune_vocabulary(vocab, term_count_min = 10L)
vectorizer = vocab_vectorizer(vocab)
tcm = create_tcm(it, vectorizer, skip_grams_window = 7L)
glove = GlobalVectors$new(word_vectors_size = 50, vocabulary = vocab, x_max = 50)
wv_main = glove$fit_transform(tcm, n_iter = 100, convergence_tol = 0.01, n_threads = 4)

covid.word.embedding's People

Watchers

 avatar  avatar  avatar  avatar  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.