Repository: mahout Updated Branches: refs/heads/master 19182a81d -> 84e90ed23
MAHOUT-1895 Add convenience methods for converting Vectors to Scala Types closes apache/mahout#262 Project: http://git-wip-us.apache.org/repos/asf/mahout/repo Commit: http://git-wip-us.apache.org/repos/asf/mahout/commit/84e90ed2 Tree: http://git-wip-us.apache.org/repos/asf/mahout/tree/84e90ed2 Diff: http://git-wip-us.apache.org/repos/asf/mahout/diff/84e90ed2 Branch: refs/heads/master Commit: 84e90ed23327355f92abeb4aede8f3a9ee5b5867 Parents: 19182a8 Author: rawkintrevo <[email protected]> Authored: Mon Jan 16 21:17:57 2017 -0600 Committer: rawkintrevo <[email protected]> Committed: Mon Jan 16 21:17:57 2017 -0600 ---------------------------------------------------------------------- .../math/scalabindings/MahoutCollections.scala | 46 ++++++++++++++++++++ .../scalabindings/MahoutCollectionsSuite.scala | 42 ++++++++++++++++++ 2 files changed, 88 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mahout/blob/84e90ed2/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MahoutCollections.scala ---------------------------------------------------------------------- diff --git a/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MahoutCollections.scala b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MahoutCollections.scala new file mode 100644 index 0000000..8251b3a --- /dev/null +++ b/math-scala/src/main/scala/org/apache/mahout/math/scalabindings/MahoutCollections.scala @@ -0,0 +1,46 @@ +/* + * 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 org.apache.mahout.math.scalabindings + +import org.apache.mahout.math.Vector + +class MahoutVectorInterfaces(v: Vector) { + /** Convert to Array[Double] */ + def toArray: Array[Double] = { + var a = new Array[Double](v.size) + for (i <- 0 until v.size){ + a(i) = v.get(i) + } + a + } + + /** Convert to Map[Int, Double] */ + def toMap: Map[Int, Double] = { + import collection.JavaConverters._ + val ms = collection.mutable.Map[Int, Double]() + for (e <- v.nonZeroes().asScala) { + ms += (e.index -> e.get) + } + ms.toMap + } + +} + +object MahoutCollections { + implicit def v2scalaish(v: Vector) = new MahoutVectorInterfaces(v) +} http://git-wip-us.apache.org/repos/asf/mahout/blob/84e90ed2/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/MahoutCollectionsSuite.scala ---------------------------------------------------------------------- diff --git a/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/MahoutCollectionsSuite.scala b/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/MahoutCollectionsSuite.scala new file mode 100644 index 0000000..cf62eea --- /dev/null +++ b/math-scala/src/test/scala/org/apache/mahout/math/scalabindings/MahoutCollectionsSuite.scala @@ -0,0 +1,42 @@ +/* + * 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 org.apache.mahout.math.scalabindings + +import org.apache.mahout.math.Vector +import org.apache.mahout.test.MahoutSuite +import org.scalatest.FunSuite +import org.apache.mahout.math.scalabindings.MahoutCollections._ +import org.apache.mahout.math._ +import org.apache.mahout.math.scalabindings.RLikeOps._ + +class MahoutCollectionsSuite extends FunSuite with MahoutSuite { + test("toArray") { + val a = Array(1.0, 2.0, 3.0) + val v: Vector = new org.apache.mahout.math.DenseVector(a) + + v.toArray.deep shouldBe a.deep + + } + + test("toMap") { + val m = Map( (1 -> 1.0), (3 -> 3.0)) + val sv = svec(m) + + sv.toMap shouldBe m + } +}
