{"action":"created","comment":{"url":"https://api.github.com/repos/apache/flink/pulls/comments/32622189","id":32622189,"diff_hunk":"@@ -0,0 +1,128 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one\n+ * or more contributor license agreements.  See the NOTICE file\n+ * distributed with this work for additional information\n+ * regarding copyright ownership.  The ASF licenses this file\n+ * to you under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance\n+ * with the License.  You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package org.apache.flink.ml.feature\n+\n+import org.apache.flink.api.scala.{DataSet, _}\n+import org.apache.flink.ml.common.{Parameter, ParameterMap, Transformer}\n+import org.apache.flink.ml.math.SparseVector\n+\n+import scala.collection.mutable.LinkedHashSet\n+import scala.math.log;\n+\n+/**\n+ * This transformer calcuates the term-frequency times inverse document frequency for the give DataSet of documents.\n+ * The DataSet will be treated as the corpus of documents. The single words will be filtered against the regex:\n+ * <code>\n+ * (?u)\\b\\w\\w+\\b\n+ * </code>\n+ * <p>\n+ * The TF is the frequence of a word inside one document\n+ * <p>\n+ * The IDF for a word is calculated: log(total number of documents / documents that contain the word) + 1\n+ * <p>\n+ * This transformer returns a SparseVector where the index is the hash of the word and the value the tf-idf.\n+ * @author Ronny Bräunlich\n+ * @author Vassil Dimov\n+ * @author Filip Perisic\n+ */\n+class TfIdfTransformer extends Transformer[(Int, Seq[String]), (Int, SparseVector)] {\n+\n+  override def transform(input: DataSet[(Int /* docId */ , Seq[String] /*The document */ )], transformParameters: ParameterMap): DataSet[(Int, SparseVector)] = {\n+\n+    val matchedWordCounts = input.flatMap(t => {\n+      t._2.flatMap(s => {\n+        \"(?u)\\\\b\\\\w\\\\w+\\\\b\".r findAllIn s map (","path":"flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/feature/TfIdfTransformer.scala","position":50,"original_position":50,"commit_id":"6f799b7d018488acbfdf670f8648a5427aa10f42","original_commit_id":"6f799b7d018488acbfdf670f8648a5427aa10f42","user":{"login":"tillrohrmann","id":5756858,"avatar_url":"https://avatars.githubusercontent.com/u/5756858?v=3","gravatar_id":"","url":"https://api.github.com/users/tillrohrmann","html_url":"https://github.com/tillrohrmann","followers_url":"https://api.github.com/users/tillrohrmann/followers","following_url":"https://api.github.com/users/tillrohrmann/following{/other_user}","gists_url":"https://api.github.com/users/tillrohrmann/gists{/gist_id}","starred_url":"https://api.github.com/users/tillrohrmann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tillrohrmann/subscriptions","organizations_url":"https://api.github.com/users/tillrohrmann/orgs","repos_url":"https://api.github.com/users/tillrohrmann/repos","events_url":"https://api.github.com/users/tillrohrmann/events{/privacy}","received_events_url":"https://api.github.com/users/tillrohrmann/received_events","type":"User","site_admin":false},"body":"This regex won't capture single character words such as \"a\" for example.","created_at":"2015-06-17T12:46:57Z","updated_at":"2015-06-17T12:46:57Z","html_url":"https://github.com/apache/flink/pull/730#discussion_r32622189","pull_request_url":"https://api.github.com/repos/apache/flink/pulls/730","_links":{"self":{"href":"https://api.github.com/repos/apache/flink/pulls/comments/32622189"},"html":{"href":"https://github.com/apache/flink/pull/730#discussion_r32622189"},"pull_request":{"href":"https://api.github.com/repos/apache/flink/pulls/730"}}},"pull_request":{"url":"https://api.github.com/repos/apache/flink/pulls/730","id":36249191,"html_url":"https://github.com/apache/flink/pull/730","diff_url":"https://github.com/apache/flink/pull/730.diff","patch_url":"https://github.com/apache/flink/pull/730.patch","issue_url":"https://api.github.com/repos/apache/flink/issues/730","number":730,"state":"open","locked":false,"title":"[Flink-1999] basic TfidfTransformer","user":{"login":"rbraeunlich","id":2596597,"avatar_url":"https://avatars.githubusercontent.com/u/2596597?v=3","gravatar_id":"","url":"https://api.github.com/users/rbraeunlich","html_url":"https://github.com/rbraeunlich","followers_url":"https://api.github.com/users/rbraeunlich/followers","following_url":"https://api.github.com/users/rbraeunlich/following{/other_user}","gists_url":"https://api.github.com/users/rbraeunlich/gists{/gist_id}","starred_url":"https://api.github.com/users/rbraeunlich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rbraeunlich/subscriptions","organizations_url":"https://api.github.com/users/rbraeunlich/orgs","repos_url":"https://api.github.com/users/rbraeunlich/repos","events_url":"https://api.github.com/users/rbraeunlich/events{/privacy}","received_events_url":"https://api.github.com/users/rbraeunlich/received_events","type":"User","site_admin":false},"body":"Hi everybody,\r\n\r\ndue to [Flink-1999](https://issues.apache.org/jira/browse/FLINK-1999) we created a first implementation of a TfIdfTranformer.\r\nThere is still one problem left, because using modulo after the hashing causes collisions.\r\nNevertheless, we would be glad to receive some comments to our implementation.\r\n\r\nCheers,\r\nRonny","created_at":"2015-05-26T21:48:10Z","updated_at":"2015-06-17T12:46:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"83b8e2c289769c3b3b3df34a23270d94ff93c063","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/flink/pulls/730/commits","review_comments_url":"https://api.github.com/repos/apache/flink/pulls/730/comments","review_comment_url":"https://api.github.com/repos/apache/flink/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/flink/issues/730/comments","statuses_url":"https://api.github.com/repos/apache/flink/statuses/6f799b7d018488acbfdf670f8648a5427aa10f42","head":{"label":"rbraeunlich:tfidf","ref":"tfidf","sha":"6f799b7d018488acbfdf670f8648a5427aa10f42","user":{"login":"rbraeunlich","id":2596597,"avatar_url":"https://avatars.githubusercontent.com/u/2596597?v=3","gravatar_id":"","url":"https://api.github.com/users/rbraeunlich","html_url":"https://github.com/rbraeunlich","followers_url":"https://api.github.com/users/rbraeunlich/followers","following_url":"https://api.github.com/users/rbraeunlich/following{/other_user}","gists_url":"https://api.github.com/users/rbraeunlich/gists{/gist_id}","starred_url":"https://api.github.com/users/rbraeunlich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rbraeunlich/subscriptions","organizations_url":"https://api.github.com/users/rbraeunlich/orgs","repos_url":"https://api.github.com/users/rbraeunlich/repos","events_url":"https://api.github.com/users/rbraeunlich/events{/privacy}","received_events_url":"https://api.github.com/users/rbraeunlich/received_events","type":"User","site_admin":false},"repo":{"id":35498381,"name":"flink","full_name":"rbraeunlich/flink","owner":{"login":"rbraeunlich","id":2596597,"avatar_url":"https://avatars.githubusercontent.com/u/2596597?v=3","gravatar_id":"","url":"https://api.github.com/users/rbraeunlich","html_url":"https://github.com/rbraeunlich","followers_url":"https://api.github.com/users/rbraeunlich/followers","following_url":"https://api.github.com/users/rbraeunlich/following{/other_user}","gists_url":"https://api.github.com/users/rbraeunlich/gists{/gist_id}","starred_url":"https://api.github.com/users/rbraeunlich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rbraeunlich/subscriptions","organizations_url":"https://api.github.com/users/rbraeunlich/orgs","repos_url":"https://api.github.com/users/rbraeunlich/repos","events_url":"https://api.github.com/users/rbraeunlich/events{/privacy}","received_events_url":"https://api.github.com/users/rbraeunlich/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rbraeunlich/flink","description":"Mirror of Apache Flink","fork":true,"url":"https://api.github.com/repos/rbraeunlich/flink","forks_url":"https://api.github.com/repos/rbraeunlich/flink/forks","keys_url":"https://api.github.com/repos/rbraeunlich/flink/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rbraeunlich/flink/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rbraeunlich/flink/teams","hooks_url":"https://api.github.com/repos/rbraeunlich/flink/hooks","issue_events_url":"https://api.github.com/repos/rbraeunlich/flink/issues/events{/number}","events_url":"https://api.github.com/repos/rbraeunlich/flink/events","assignees_url":"https://api.github.com/repos/rbraeunlich/flink/assignees{/user}","branches_url":"https://api.github.com/repos/rbraeunlich/flink/branches{/branch}","tags_url":"https://api.github.com/repos/rbraeunlich/flink/tags","blobs_url":"https://api.github.com/repos/rbraeunlich/flink/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rbraeunlich/flink/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rbraeunlich/flink/git/refs{/sha}","trees_url":"https://api.github.com/repos/rbraeunlich/flink/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rbraeunlich/flink/statuses/{sha}","languages_url":"https://api.github.com/repos/rbraeunlich/flink/languages","stargazers_url":"https://api.github.com/repos/rbraeunlich/flink/stargazers","contributors_url":"https://api.github.com/repos/rbraeunlich/flink/contributors","subscribers_url":"https://api.github.com/repos/rbraeunlich/flink/subscribers","subscription_url":"https://api.github.com/repos/rbraeunlich/flink/subscription","commits_url":"https://api.github.com/repos/rbraeunlich/flink/commits{/sha}","git_commits_url":"https://api.github.com/repos/rbraeunlich/flink/git/commits{/sha}","comments_url":"https://api.github.com/repos/rbraeunlich/flink/comments{/number}","issue_comment_url":"https://api.github.com/repos/rbraeunlich/flink/issues/comments{/number}","contents_url":"https://api.github.com/repos/rbraeunlich/flink/contents/{+path}","compare_url":"https://api.github.com/repos/rbraeunlich/flink/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rbraeunlich/flink/merges","archive_url":"https://api.github.com/repos/rbraeunlich/flink/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rbraeunlich/flink/downloads","issues_url":"https://api.github.com/repos/rbraeunlich/flink/issues{/number}","pulls_url":"https://api.github.com/repos/rbraeunlich/flink/pulls{/number}","milestones_url":"https://api.github.com/repos/rbraeunlich/flink/milestones{/number}","notifications_url":"https://api.github.com/repos/rbraeunlich/flink/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rbraeunlich/flink/labels{/name}","releases_url":"https://api.github.com/repos/rbraeunlich/flink/releases{/id}","created_at":"2015-05-12T16:04:01Z","updated_at":"2015-05-12T16:04:11Z","pushed_at":"2015-06-15T20:15:55Z","git_url":"git://github.com/rbraeunlich/flink.git","ssh_url":"git@github.com:rbraeunlich/flink.git","clone_url":"https://github.com/rbraeunlich/flink.git","svn_url":"https://github.com/rbraeunlich/flink","homepage":null,"size":50143,"stargazers_count":0,"watchers_count":0,"language":"Java","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":"12cd15622afbbcbb40c9dd80151f4ca459a26922","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":20587599,"name":"flink","full_name":"apache/flink","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/flink","description":"Mirror of Apache Flink","fork":false,"url":"https://api.github.com/repos/apache/flink","forks_url":"https://api.github.com/repos/apache/flink/forks","keys_url":"https://api.github.com/repos/apache/flink/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/flink/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/flink/teams","hooks_url":"https://api.github.com/repos/apache/flink/hooks","issue_events_url":"https://api.github.com/repos/apache/flink/issues/events{/number}","events_url":"https://api.github.com/repos/apache/flink/events","assignees_url":"https://api.github.com/repos/apache/flink/assignees{/user}","branches_url":"https://api.github.com/repos/apache/flink/branches{/branch}","tags_url":"https://api.github.com/repos/apache/flink/tags","blobs_url":"https://api.github.com/repos/apache/flink/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/flink/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/flink/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/flink/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/flink/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/flink/languages","stargazers_url":"https://api.github.com/repos/apache/flink/stargazers","contributors_url":"https://api.github.com/repos/apache/flink/contributors","subscribers_url":"https://api.github.com/repos/apache/flink/subscribers","subscription_url":"https://api.github.com/repos/apache/flink/subscription","commits_url":"https://api.github.com/repos/apache/flink/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/flink/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/flink/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/flink/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/flink/contents/{+path}","compare_url":"https://api.github.com/repos/apache/flink/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/flink/merges","archive_url":"https://api.github.com/repos/apache/flink/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/flink/downloads","issues_url":"https://api.github.com/repos/apache/flink/issues{/number}","pulls_url":"https://api.github.com/repos/apache/flink/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/flink/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/flink/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/flink/labels{/name}","releases_url":"https://api.github.com/repos/apache/flink/releases{/id}","created_at":"2014-06-07T07:00:10Z","updated_at":"2015-06-17T03:09:02Z","pushed_at":"2015-06-17T12:02:32Z","git_url":"git://github.com/apache/flink.git","ssh_url":"git@github.com:apache/flink.git","clone_url":"https://github.com/apache/flink.git","svn_url":"https://github.com/apache/flink","homepage":null,"size":2565508,"stargazers_count":461,"watchers_count":461,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":300,"mirror_url":"git://git.apache.org/incubator-flink.git","open_issues_count":56,"forks":300,"open_issues":56,"watchers":461,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/flink/pulls/730"},"html":{"href":"https://github.com/apache/flink/pull/730"},"issue":{"href":"https://api.github.com/repos/apache/flink/issues/730"},"comments":{"href":"https://api.github.com/repos/apache/flink/issues/730/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/flink/pulls/730/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/flink/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/flink/pulls/730/commits"},"statuses":{"href":"https://api.github.com/repos/apache/flink/statuses/6f799b7d018488acbfdf670f8648a5427aa10f42"}}}}