
R version 3.6.0 (2019-04-26) -- "Planting of a Tree"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(tidyverse)
Registered S3 method overwritten by 'rvest':
  method            from
  read_xml.response xml2
── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
✔ ggplot2 3.2.1     ✔ purrr   0.3.2
✔ tibble  2.1.3     ✔ dplyr   0.8.3
✔ tidyr   0.8.3     ✔ stringr 1.4.0
✔ readr   1.3.1     ✔ forcats 0.4.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
> library(stringr)
> library(sessioninfo)
> options(width = 120)
> 
> # ------------------------------------------------------------------------------
> 
> classes <- c("bad", "poor", "ok", "good", "great")
> 
> # Data here:
> # https://snap.stanford.edu/data/web-FineFoods.html
> 
> raw <-
+   read_delim(
+     "finefoods.txt",
+     delim = "\n",
+     col_names = "text",
+     col_types = cols(text = col_character())
+   ) %>%
+   mutate(
+     text = str_remove(text, "product/"),
+     text = str_remove(text, "review/"),
+     prod_num  = ifelse(str_detect(text, "productId"), 1, 0),
+     prod_num = cumsum(prod_num)
+   ) %>%
+   dplyr::filter(
+     str_detect(text, "(productId:)|(text:)|(score:)")
+   ) %>%
+   mutate(
+     field = case_when(
+       str_detect(text, "productId:") ~ "product",
+       str_detect(text, "text:") ~ "review",
+       str_detect(text, "score:") ~ "score",
+       TRUE ~ "unknown"
+     ),
+     text = str_replace(text, "(productId: )|(text: )|(score: )", "")
+   ) %>%
+   spread(field, text) %>%
+   dplyr::select(-prod_num) %>%
+   mutate(
+     score = factor(ifelse(score == "5.0", "great", "other"))
+   )
> 
> # ------------------------------------------------------------------------------
> # Choose _products_ to split to training and test set. There tend to be multiple
> # rows per product.
> 
> prod_dist <-
+   raw %>%
+   group_by(product) %>%
+   count() %>%
+   ungroup() %>%
+   arrange(desc(n))
> 
> # Take the 25 well characterized products for the training set (or more for larger
> # training set)
> set.seed(9565)
> train_prods <-
+   prod_dist %>%
+   dplyr::filter(n > 100) %>%
+   sample_n(25) %>%
+   dplyr::select(product)
> 
> training_data <-
+   train_prods %>%
+   inner_join(raw, by = "product")
> 
> testing_data <-
+   raw %>%
+   anti_join(train_prods %>% dplyr::select(product), by = "product") %>%
+   group_by(product) %>%
+   sample_n(1) %>%
+   ungroup() %>%
+   sample_n(1000)
> 
> save(training_data, testing_data, file = "data/small_fine_foods.RData", version = 2, compress = "xz")
> 
> # ------------------------------------------------------------------------------
> 
> session_info()
─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.0 (2019-04-26)
 os       macOS High Sierra 10.13.6   
 system   x86_64, darwin15.6.0        
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2019-09-05                  

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date       lib source                               
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                       
 backports     1.1.4      2019-04-10 [1] CRAN (R 3.6.0)                       
 broom         0.5.2      2019-04-07 [1] CRAN (R 3.6.0)                       
 cellranger    1.1.0      2016-07-27 [1] CRAN (R 3.6.0)                       
 cli           1.1.0      2019-03-19 [1] CRAN (R 3.6.0)                       
 colorspace    1.4-1      2019-03-18 [1] CRAN (R 3.6.0)                       
 crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                       
 dplyr       * 0.8.3      2019-07-04 [1] CRAN (R 3.6.0)                       
 forcats     * 0.4.0      2019-02-17 [1] CRAN (R 3.6.0)                       
 generics      0.0.2      2018-11-29 [1] CRAN (R 3.6.0)                       
 ggplot2     * 3.2.1      2019-08-10 [1] CRAN (R 3.6.0)                       
 glue          1.3.1      2019-03-12 [1] CRAN (R 3.6.0)                       
 gtable        0.3.0      2019-03-25 [1] CRAN (R 3.6.0)                       
 haven         2.1.0      2019-02-19 [1] CRAN (R 3.6.0)                       
 hms           0.4.2      2018-03-10 [1] CRAN (R 3.6.0)                       
 httr          1.4.1      2019-08-05 [1] CRAN (R 3.6.0)                       
 jsonlite      1.6        2018-12-07 [1] CRAN (R 3.6.0)                       
 lattice       0.20-38    2018-11-04 [1] CRAN (R 3.6.0)                       
 lazyeval      0.2.2      2019-03-15 [1] CRAN (R 3.6.0)                       
 lubridate     1.7.4      2018-04-11 [1] CRAN (R 3.5.0)                       
 magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)                       
 modelr        0.1.4      2019-02-18 [1] CRAN (R 3.6.0)                       
 munsell       0.5.0      2018-06-12 [1] CRAN (R 3.6.0)                       
 nlme          3.1-139    2019-04-09 [1] CRAN (R 3.6.0)                       
 pillar        1.4.2      2019-06-29 [1] CRAN (R 3.6.0)                       
 pkgconfig     2.0.2      2018-08-16 [1] CRAN (R 3.6.0)                       
 purrr       * 0.3.2      2019-03-15 [1] CRAN (R 3.6.0)                       
 R6            2.4.0      2019-02-14 [1] CRAN (R 3.6.0)                       
 Rcpp          1.0.2      2019-07-25 [1] CRAN (R 3.6.0)                       
 readr       * 1.3.1      2018-12-21 [1] CRAN (R 3.6.0)                       
 readxl        1.3.1      2019-03-13 [1] CRAN (R 3.6.0)                       
 rlang         0.4.0.9002 2019-08-31 [1] Github (r-lib/rlang@15e799c)         
 rstudioapi    0.10       2019-03-19 [1] CRAN (R 3.6.0)                       
 rvest         0.3.2      2016-06-17 [1] CRAN (R 3.6.0)                       
 scales        1.0.0      2018-08-09 [1] CRAN (R 3.6.0)                       
 sessioninfo * 1.1.1.9000 2019-03-26 [1] Github (r-lib/sessioninfo@dfb3ea8)   
 stringi       1.4.3      2019-03-12 [1] CRAN (R 3.6.0)                       
 stringr     * 1.4.0      2019-02-10 [1] CRAN (R 3.6.0)                       
 tibble      * 2.1.3      2019-06-06 [1] CRAN (R 3.6.0)                       
 tidyr       * 0.8.3      2019-03-01 [1] CRAN (R 3.6.0)                       
 tidyselect    0.2.5      2018-10-11 [1] CRAN (R 3.6.0)                       
 tidyverse   * 1.2.1      2017-11-14 [1] CRAN (R 3.6.0)                       
 withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)                       
 xml2          1.2.0.9000 2019-06-30 [1] Github (HenrikBengtsson/xml2@618bc74)

[1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library
> 
> q("no")
> proc.time()
   user  system elapsed 
 34.792   2.881  37.713 
