{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/37051774","id":37051774,"diff_hunk":"@@ -19,45 +19,133 @@ package org.apache.spark.ml.feature\n import scala.collection.mutable\n \n import org.apache.spark.annotation.Experimental\n-import org.apache.spark.ml.UnaryTransformer\n-import org.apache.spark.ml.param.{ParamMap, ParamValidators, IntParam}\n-import org.apache.spark.ml.util.Identifiable\n-import org.apache.spark.mllib.linalg.{Vectors, VectorUDT, Vector}\n-import org.apache.spark.sql.types.{StringType, ArrayType, DataType}\n+import org.apache.spark.ml.param._\n+import org.apache.spark.ml.param.shared.{HasInputCol, HasOutputCol}\n+import org.apache.spark.ml.util.{Identifiable, SchemaUtils}\n+import org.apache.spark.ml.{Estimator, Model}\n+import org.apache.spark.mllib.linalg.{VectorUDT, Vectors}\n+import org.apache.spark.rdd.RDD\n+import org.apache.spark.sql.functions._\n+import org.apache.spark.sql.types._\n+import org.apache.spark.sql.DataFrame\n+\n+/**\n+ * Params for [[CountVectorizer]] and [[CountVectorizerModel]].\n+ */\n+private[feature] trait CountVectorizerParams extends Params with HasInputCol with HasOutputCol {\n+\n+  /**\n+   * size of the vocabulary.\n+   * If using Estimator, CountVectorizer will build a vocabulary that only consider the top\n+   * vocabSize terms ordered by term frequency across the corpus.\n+   * Default: 10000\n+   * @group param\n+   */\n+  val vocabSize: IntParam = new IntParam(this, \"vocabSize\", \"size of the vocabulary\")\n+\n+  /** @group getParam */\n+  def getVocabSize: Int = $(vocabSize)\n+\n+  /** Validates and transforms the input schema. */\n+  protected def validateAndTransformSchema(schema: StructType): StructType = {\n+    SchemaUtils.checkColumnType(schema, $(inputCol), new ArrayType(StringType, true))\n+    SchemaUtils.appendColumn(schema, $(outputCol), new VectorUDT)\n+  }\n+\n+  override def validateParams(): Unit = {\n+    require($(vocabSize) > 0, s\"The vocabulary size (${$(vocabSize)}) must be above 0.\")\n+  }\n+}\n \n /**\n  * :: Experimental ::\n- * Converts a text document to a sparse vector of token counts.\n- * @param vocabulary An Array over terms. Only the terms in the vocabulary will be counted.\n+ * Extracts a vocabulary from document collections and generates a [[CountVectorizerModel]].\n  */\n-@Experimental\n-class CountVectorizerModel (override val uid: String, val vocabulary: Array[String])\n-  extends UnaryTransformer[Seq[String], Vector, CountVectorizerModel] {\n+class CountVectorizer(override val uid: String)\n+  extends Estimator[CountVectorizerModel] with CountVectorizerParams {\n \n-  def this(vocabulary: Array[String]) =\n-    this(Identifiable.randomUID(\"cntVec\"), vocabulary)\n+  def this() = this(Identifiable.randomUID(\"cntVec\"))\n \n   /**\n-   * Corpus-specific filter to ignore scarce words in a document. For each document, terms with\n-   * frequency (count) less than the given threshold are ignored.\n+   * The minimum number of times a token must appear in the corpus to be included in the vocabulary\n    * Default: 1\n    * @group param\n    */\n-  val minTermFreq: IntParam = new IntParam(this, \"minTermFreq\",\n-    \"minimum frequency (count) filter used to neglect scarce words (>= 1). For each document, \" +\n-      \"terms with frequency less than the given threshold are ignored.\", ParamValidators.gtEq(1))\n+  val minCount: IntParam = new IntParam(this, \"minCount\",\n+    \"minimum number of times a token must appear in the corpus to be included in the vocabulary.\"\n+    , ParamValidators.gtEq(1))\n+\n+  /** @group getParam */\n+  def getMinCount: Int = $(minCount)\n \n   /** @group setParam */\n-  def setMinTermFreq(value: Int): this.type = set(minTermFreq, value)\n+  def setInputCol(value: String): this.type = set(inputCol, value)\n \n-  /** @group getParam */\n-  def getMinTermFreq: Int = $(minTermFreq)\n+  /** @group setParam */\n+  def setOutputCol(value: String): this.type = set(outputCol, value)\n \n-  setDefault(minTermFreq -> 1)\n+  /** @group setParam */\n+  def setVocabSize(value: Int): this.type = set(vocabSize, value)\n+\n+  /** @group setParam */\n+  def setMinCount(value: Int): this.type = set(minCount, value)\n \n-  override protected def createTransformFunc: Seq[String] => Vector = {\n+  setDefault(vocabSize -> 10000, minCount -> 1)\n+\n+  override def fit(dataset: DataFrame): CountVectorizerModel = {\n+    transformSchema(dataset.schema, logging = true)\n+    val input = dataset.select($(inputCol)).map(_.getAs[Seq[String]](0))\n+    val wordCounts: RDD[(String, Long)] = input\n+      .flatMap { case (tokens) => tokens.map(_ -> 1L) }\n+      .reduceByKey(_ + _)\n+      .filter(_._2 >= $(minCount))","path":"mllib/src/main/scala/org/apache/spark/ml/feature/CountVectorizerModel.scala","position":105,"original_position":105,"commit_id":"589e93d3267b8a3f9ef1f1aba3aec22463dddfa6","original_commit_id":"589e93d3267b8a3f9ef1f1aba3aec22463dddfa6","user":{"login":"jkbradley","id":5084283,"avatar_url":"https://avatars.githubusercontent.com/u/5084283?v=3","gravatar_id":"","url":"https://api.github.com/users/jkbradley","html_url":"https://github.com/jkbradley","followers_url":"https://api.github.com/users/jkbradley/followers","following_url":"https://api.github.com/users/jkbradley/following{/other_user}","gists_url":"https://api.github.com/users/jkbradley/gists{/gist_id}","starred_url":"https://api.github.com/users/jkbradley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jkbradley/subscriptions","organizations_url":"https://api.github.com/users/jkbradley/orgs","repos_url":"https://api.github.com/users/jkbradley/repos","events_url":"https://api.github.com/users/jkbradley/events{/privacy}","received_events_url":"https://api.github.com/users/jkbradley/received_events","type":"User","site_admin":false},"body":"That sounds good, but it's not high priority (so 1.6, not for 1.5)","created_at":"2015-08-14T06:07:46Z","updated_at":"2015-08-14T06:07:46Z","html_url":"https://github.com/apache/spark/pull/7388#discussion_r37051774","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/7388","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/37051774"},"html":{"href":"https://github.com/apache/spark/pull/7388#discussion_r37051774"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/7388"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/7388","id":39878458,"html_url":"https://github.com/apache/spark/pull/7388","diff_url":"https://github.com/apache/spark/pull/7388.diff","patch_url":"https://github.com/apache/spark/pull/7388.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/7388","number":7388,"state":"open","locked":false,"title":"[Spark-9028] [ML] Add CountVectorizer as an estimator to generate CountVectorizerModel","user":{"login":"hhbyyh","id":7981698,"avatar_url":"https://avatars.githubusercontent.com/u/7981698?v=3","gravatar_id":"","url":"https://api.github.com/users/hhbyyh","html_url":"https://github.com/hhbyyh","followers_url":"https://api.github.com/users/hhbyyh/followers","following_url":"https://api.github.com/users/hhbyyh/following{/other_user}","gists_url":"https://api.github.com/users/hhbyyh/gists{/gist_id}","starred_url":"https://api.github.com/users/hhbyyh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hhbyyh/subscriptions","organizations_url":"https://api.github.com/users/hhbyyh/orgs","repos_url":"https://api.github.com/users/hhbyyh/repos","events_url":"https://api.github.com/users/hhbyyh/events{/privacy}","received_events_url":"https://api.github.com/users/hhbyyh/received_events","type":"User","site_admin":false},"body":"jira: https://issues.apache.org/jira/browse/SPARK-9028 \r\n\r\nAdd an estimator for CountVectorizerModel. The estimator will extract a vocabulary from document collections according to the term frequency.\r\n\r\nI changed the meaning of minCount as a filter across the corpus. This aligns with Word2Vec and the similar parameter in SKlearn. ","created_at":"2015-07-14T03:20:34Z","updated_at":"2015-08-14T06:07:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"258a79a6e7a5a75206fb0ea4f09a094ade4807a1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/7388/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/7388/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/7388/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/589e93d3267b8a3f9ef1f1aba3aec22463dddfa6","head":{"label":"hhbyyh:cvEstimator","ref":"cvEstimator","sha":"589e93d3267b8a3f9ef1f1aba3aec22463dddfa6","user":{"login":"hhbyyh","id":7981698,"avatar_url":"https://avatars.githubusercontent.com/u/7981698?v=3","gravatar_id":"","url":"https://api.github.com/users/hhbyyh","html_url":"https://github.com/hhbyyh","followers_url":"https://api.github.com/users/hhbyyh/followers","following_url":"https://api.github.com/users/hhbyyh/following{/other_user}","gists_url":"https://api.github.com/users/hhbyyh/gists{/gist_id}","starred_url":"https://api.github.com/users/hhbyyh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hhbyyh/subscriptions","organizations_url":"https://api.github.com/users/hhbyyh/orgs","repos_url":"https://api.github.com/users/hhbyyh/repos","events_url":"https://api.github.com/users/hhbyyh/events{/privacy}","received_events_url":"https://api.github.com/users/hhbyyh/received_events","type":"User","site_admin":false},"repo":{"id":29568485,"name":"spark","full_name":"hhbyyh/spark","owner":{"login":"hhbyyh","id":7981698,"avatar_url":"https://avatars.githubusercontent.com/u/7981698?v=3","gravatar_id":"","url":"https://api.github.com/users/hhbyyh","html_url":"https://github.com/hhbyyh","followers_url":"https://api.github.com/users/hhbyyh/followers","following_url":"https://api.github.com/users/hhbyyh/following{/other_user}","gists_url":"https://api.github.com/users/hhbyyh/gists{/gist_id}","starred_url":"https://api.github.com/users/hhbyyh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hhbyyh/subscriptions","organizations_url":"https://api.github.com/users/hhbyyh/orgs","repos_url":"https://api.github.com/users/hhbyyh/repos","events_url":"https://api.github.com/users/hhbyyh/events{/privacy}","received_events_url":"https://api.github.com/users/hhbyyh/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hhbyyh/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/hhbyyh/spark","forks_url":"https://api.github.com/repos/hhbyyh/spark/forks","keys_url":"https://api.github.com/repos/hhbyyh/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hhbyyh/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hhbyyh/spark/teams","hooks_url":"https://api.github.com/repos/hhbyyh/spark/hooks","issue_events_url":"https://api.github.com/repos/hhbyyh/spark/issues/events{/number}","events_url":"https://api.github.com/repos/hhbyyh/spark/events","assignees_url":"https://api.github.com/repos/hhbyyh/spark/assignees{/user}","branches_url":"https://api.github.com/repos/hhbyyh/spark/branches{/branch}","tags_url":"https://api.github.com/repos/hhbyyh/spark/tags","blobs_url":"https://api.github.com/repos/hhbyyh/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hhbyyh/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hhbyyh/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/hhbyyh/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hhbyyh/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/hhbyyh/spark/languages","stargazers_url":"https://api.github.com/repos/hhbyyh/spark/stargazers","contributors_url":"https://api.github.com/repos/hhbyyh/spark/contributors","subscribers_url":"https://api.github.com/repos/hhbyyh/spark/subscribers","subscription_url":"https://api.github.com/repos/hhbyyh/spark/subscription","commits_url":"https://api.github.com/repos/hhbyyh/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/hhbyyh/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/hhbyyh/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/hhbyyh/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/hhbyyh/spark/contents/{+path}","compare_url":"https://api.github.com/repos/hhbyyh/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hhbyyh/spark/merges","archive_url":"https://api.github.com/repos/hhbyyh/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hhbyyh/spark/downloads","issues_url":"https://api.github.com/repos/hhbyyh/spark/issues{/number}","pulls_url":"https://api.github.com/repos/hhbyyh/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/hhbyyh/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/hhbyyh/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hhbyyh/spark/labels{/name}","releases_url":"https://api.github.com/repos/hhbyyh/spark/releases{/id}","created_at":"2015-01-21T03:08:12Z","updated_at":"2015-05-07T09:16:53Z","pushed_at":"2015-08-13T05:52:56Z","git_url":"git://github.com/hhbyyh/spark.git","ssh_url":"git@github.com:hhbyyh/spark.git","clone_url":"https://github.com/hhbyyh/spark.git","svn_url":"https://github.com/hhbyyh/spark","homepage":null,"size":116659,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"b7bcbe25f90ba4e78b548465bc80d4de1d2c4a4a","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","created_at":"2014-02-25T08:00:08Z","updated_at":"2015-08-14T01:34:48Z","pushed_at":"2015-08-14T05:47:08Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":1608845,"stargazers_count":5076,"watchers_count":5076,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4631,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":323,"forks":4631,"open_issues":323,"watchers":5076,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/7388"},"html":{"href":"https://github.com/apache/spark/pull/7388"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/7388"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/7388/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/7388/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/7388/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/589e93d3267b8a3f9ef1f1aba3aec22463dddfa6"}}}}