{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "library(readr)\nlibrary(tm)\nlibrary(SnowballC)\nlibrary(skmeans)\n\nproducts <- read_csv('../input/producto_tabla.csv')\n\n\nextract_shortname <- function(product_name) {\n  # Split the name\n  tokens <- strsplit(product_name, \" \")[[1]]\n  \n  # Delete ID\n  tokens <- head(tokens, length(tokens) - 1)\n  \n  # Delete Brands (name till the last token with digit)\n  digit_indeces <- grep(\"[0-9]\", tokens)\n  \n  # Product names without digits\n  digit_index <- ifelse(length(digit_indeces) == 0, 1,\n                        max(digit_indeces))\n  paste(tokens[1:digit_index], collapse = \" \")\n}\n\n# Delete product with no name\nproducts <- products[2:nrow(products),]\n\nproducts$product_shortname <- unlist(lapply(products$NombreProducto, extract_shortname))\n\n# Short Names Preprocessing\nCorpusShort <- Corpus(VectorSource(products$product_shortname))\nCorpusShort <- tm_map(CorpusShort, tolower)\nCorpusShort <- tm_map(CorpusShort, PlainTextDocument)\n\n# Remove Punctuation\nCorpusShort <- tm_map(CorpusShort, removePunctuation)\n\n# Remove Stopwords\nCorpusShort <- tm_map(CorpusShort, removeWords, stopwords(\"es\"))\n\n# Stemming\nCorpusShort <- tm_map(CorpusShort, stemDocument, language=\"es\")\n\n# Create DTM\nCorpusShort <- Corpus(VectorSource(CorpusShort))\ndtmShort <- DocumentTermMatrix(CorpusShort)\n\n# Delete Sparse Terms (all the words now)\nsparseShort <- removeSparseTerms(dtmShort, 0.9999)\nShortWords <- as.data.frame(as.matrix(sparseShort))\n\n# Create valid names\ncolnames(ShortWords) <- make.names(colnames(ShortWords))\n\n# Spherical k-means for product clustering (30 clusters at the moment)\nset.seed(123)\nmod <- skmeans(as.matrix(ShortWords), 30, method = \"genetic\")\nproducts$cluster <- mod$cluster\n\n# Example for one of the clusters\nwrite_csv(products[mod$cluster == 1,], 'example_cluster.csv')\n"
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}