Skip to content

Commit c2e571c

Browse files
committed
rename LabelParser.apply to LabelParser.parse
use extends for singleton
1 parent 11c94e0 commit c2e571c

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

mllib/src/main/scala/org/apache/spark/mllib/util/LabelParsers.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package org.apache.spark.mllib.util
2020
/** Trait for label parsers. */
2121
trait LabelParser extends Serializable {
2222
/** Parses a string label into a double label. */
23-
def apply(labelString: String): Double
23+
def parse(labelString: String): Double
2424
}
2525

2626
/**
@@ -32,24 +32,22 @@ class BinaryLabelParser extends LabelParser {
3232
* Parses the input label into positive (1.0) if the value is greater than 0.5,
3333
* or negative (0.0) otherwise.
3434
*/
35-
override def apply(labelString: String): Double = if (labelString.toDouble > 0.5) 1.0 else 0.0
35+
override def parse(labelString: String): Double = if (labelString.toDouble > 0.5) 1.0 else 0.0
3636
}
3737

38-
object BinaryLabelParser {
39-
private lazy val instance = new BinaryLabelParser()
38+
object BinaryLabelParser extends BinaryLabelParser {
4039
/** Gets the default instance of BinaryLabelParser. */
41-
def apply(): BinaryLabelParser = instance
40+
def getInstance(): BinaryLabelParser = this
4241
}
4342

4443
/**
4544
* Label parser for multiclass labels, which converts the input label to double.
4645
*/
4746
class MulticlassLabelParser extends LabelParser {
48-
override def apply(labelString: String): Double = labelString.toDouble
47+
override def parse(labelString: String): Double = labelString.toDouble
4948
}
5049

51-
object MulticlassLabelParser {
52-
private lazy val instance = new MulticlassLabelParser()
50+
object MulticlassLabelParser extends MulticlassLabelParser {
5351
/** Gets the default instance of MulticlassLabelParser. */
54-
def apply(): MulticlassLabelParser = instance
52+
def getInstance(): MulticlassLabelParser = this
5553
}

mllib/src/main/scala/org/apache/spark/mllib/util/MLUtils.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ object MLUtils {
7878
}.reduce(math.max)
7979
}
8080
parsed.map { items =>
81-
val label = labelParser(items.head)
81+
val label = labelParser.parse(items.head)
8282
val (indices, values) = items.tail.map { item =>
8383
val indexAndValue = item.split(':')
8484
val index = indexAndValue(0).toInt - 1
@@ -96,7 +96,7 @@ object MLUtils {
9696
* with number of features determined automatically and the default number of partitions.
9797
*/
9898
def loadLibSVMData(sc: SparkContext, path: String): RDD[LabeledPoint] =
99-
loadLibSVMData(sc, path, BinaryLabelParser(), -1, sc.defaultMinSplits)
99+
loadLibSVMData(sc, path, BinaryLabelParser, -1, sc.defaultMinSplits)
100100

101101
/**
102102
* Loads labeled data in the LIBSVM format into an RDD[LabeledPoint],

mllib/src/test/scala/org/apache/spark/mllib/util/MLUtilsSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class MLUtilsSuite extends FunSuite with LocalSparkContext {
8080
Files.write(lines, file, Charsets.US_ASCII)
8181
val path = tempDir.toURI.toString
8282

83-
val pointsWithNumFeatures = MLUtils.loadLibSVMData(sc, path, BinaryLabelParser(), 6).collect()
83+
val pointsWithNumFeatures = MLUtils.loadLibSVMData(sc, path, BinaryLabelParser, 6).collect()
8484
val pointsWithoutNumFeatures = MLUtils.loadLibSVMData(sc, path).collect()
8585

8686
for (points <- Seq(pointsWithNumFeatures, pointsWithoutNumFeatures)) {
@@ -93,7 +93,7 @@ class MLUtilsSuite extends FunSuite with LocalSparkContext {
9393
assert(points(2).features === Vectors.sparse(6, Seq((1, 4.0), (3, 5.0), (5, 6.0))))
9494
}
9595

96-
val multiclassPoints = MLUtils.loadLibSVMData(sc, path, MulticlassLabelParser()).collect()
96+
val multiclassPoints = MLUtils.loadLibSVMData(sc, path, MulticlassLabelParser).collect()
9797
assert(multiclassPoints.length === 3)
9898
assert(multiclassPoints(0).label === 1.0)
9999
assert(multiclassPoints(1).label === -1.0)

0 commit comments

Comments
 (0)