Solodye commented on a change in pull request #192: [HIVEMALL-253]UDF 
map_roulette() created
URL: https://github.com/apache/incubator-hivemall/pull/192#discussion_r291961257
 
 

 ##########
 File path: core/src/main/java/hivemall/tools/map/MapRouletteUDF.java
 ##########
 @@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package hivemall.tools.map;
+
+import hivemall.utils.hadoop.HiveUtils;
+import org.apache.hadoop.hive.ql.exec.*;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.udf.UDFType;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
+import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
+import java.util.*;
+import static hivemall.HivemallConstants.*;
+
+/**
+ * The map_roulette() can be use to do roulette, according to each map.entry 
's weight.
+ * 
+ * @author Wang, Yizheng
+ */
+@Description(name = "map_roulette", value = "_FUNC_(Map<K, number> map)"
+        + " - Returns the key K which determine to its weight , the bigger 
weight is ,the more probability K will return. "
+        + "Number is a probability value or a positive weight")
+@UDFType(deterministic = false, stateful = false) // it is false because it 
return value base on probability
+public class MapRouletteUDF extends GenericUDF {
+
+    /**
+     * The map passed in saved all the value and its weight
+     *
+     * @param m A map contains a lot of item as key, with their weight as value
+     * @return The key that computer selected according to key's weight
+     */
+    private Object algorithm(Map<Object, Double> m) {
+        // normalize the weight
+        double sum = 0;
+        for (Map.Entry<Object, Double> entry : m.entrySet()) {
+            sum += entry.getValue();
+        }
+        for (Map.Entry<Object, Double> entry : m.entrySet()) {
+            entry.setValue(entry.getValue() / sum);
+        }
+
+        // sort and generate a number axis
+        List<Map.Entry<Object, Double>> entryList = new 
ArrayList<>(m.entrySet());
+        Collections.sort(entryList, new MapRouletteUDF.KvComparator());
+        double tmp = 0;
+        for (Map.Entry<Object, Double> entry : entryList) {
+            tmp += entry.getValue();
+            entry.setValue(tmp);
+        }
+
+        // judge last value
+        if (entryList.get(entryList.size() - 1).getValue() > 1.0) {
+            entryList.get(entryList.size() - 1).setValue(1.0);
+        }
+
+        // pick a Object base on its weight
+        double cursor = Math.random();
+        for (Map.Entry<Object, Double> entry : entryList) {
+            if (cursor < entry.getValue()) {
+                return entry.getKey();
 
 Review comment:
   Yes, I tried to implement the Roulette Wheel Selection. But I called it 
"roulette".

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to