forked from gstamatelat/random-sampling
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c425b73
commit 6b7bd6c
Showing
11 changed files
with
136 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apply plugin: 'scala' | ||
|
||
repositories { | ||
jcenter() | ||
} | ||
|
||
sourceSets { | ||
main { | ||
scala { | ||
srcDirs = ['src'] | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compile rootProject, | ||
'org.scala-lang:scala-library:2.12.6' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.Random | ||
|
||
import gr.james.sampling.WatermanSampling | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
/** | ||
* Select 10 numbers at random in the range [1,100] using [[WatermanSampling]]. Each number has a 10% probability of | ||
* appearing in the sample. | ||
*/ | ||
object SelectRandomFromRange extends App { | ||
val rs = new WatermanSampling[Int](10, new Random) | ||
rs.feed((1 to 100).iterator.asJava) | ||
val sample = rs.sample | ||
println(sample) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import java.util.{Random, Scanner} | ||
|
||
import gr.james.sampling.VitterXSampling | ||
|
||
/** | ||
* Select 5 random tokens from an input stream using [[VitterXSampling]]. | ||
*/ | ||
object SelectRandomFromStreamX extends App { | ||
val rs = new VitterXSampling[String](5, new Random) | ||
rs.feed(new Scanner(System.in)) | ||
System.out.println(rs.sample) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import java.util.{Random, Scanner} | ||
|
||
import gr.james.sampling.VitterZSampling | ||
|
||
/** | ||
* Select 5 random tokens from an input stream using [[VitterZSampling]]. | ||
*/ | ||
object SelectRandomFromStreamZ extends App { | ||
val rs = new VitterZSampling[String](5, new Random) | ||
rs.feed(new Scanner(System.in)) | ||
System.out.println(rs.sample) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.Random | ||
|
||
import gr.james.sampling.EfraimidisSampling | ||
|
||
/** | ||
* Select 2 terms from a vocabulary using [[EfraimidisSampling]], based on their weight. | ||
*/ | ||
object SelectWeightedFromVocabulary extends App { | ||
val rs = new EfraimidisSampling[String](2, new Random) | ||
rs.feed("collection", 1) | ||
rs.feed("algorithms", 2) | ||
rs.feed("java", 2) | ||
rs.feed("random", 3) | ||
rs.feed("sampling", 4) | ||
rs.feed("reservoir", 5) | ||
System.out.println(rs.sample) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import java.util.Random | ||
|
||
import gr.james.sampling.{RandomSampling, WatermanSampling} | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
/** | ||
* Unweighted random sampling using functional constructs. | ||
*/ | ||
object UnweightedStream extends App { | ||
val sample = (0 until 20) | ||
.foldLeft(construct[Int](5, new Random()))(foldOperation) | ||
.sample().asScala.toList | ||
|
||
def construct[T](sample: Int, random: Random): RandomSampling[T] = | ||
new WatermanSampling[T](sample, random) | ||
|
||
def foldOperation[T] = | ||
(rs: RandomSampling[T], i: T) => { | ||
rs.feed(i) | ||
rs | ||
} | ||
|
||
println(sample) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import java.util.Random | ||
|
||
import gr.james.sampling.{ChaoSampling, WeightedRandomSampling} | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
/** | ||
* Weighted random sampling using functional constructs. | ||
*/ | ||
object WeightedStream extends App { | ||
val map = Map( | ||
"collection" -> 1.0, | ||
"algorithms" -> 2.0, | ||
"java" -> 2.0, | ||
"random" -> 3.0, | ||
"sampling" -> 4.0, | ||
"reservoir" -> 5.0 | ||
) | ||
val sample = map | ||
.foldLeft(construct[String](2, new Random()))(foldOperation) | ||
.sample().asScala.toList | ||
|
||
def construct[T](sample: Int, random: Random): WeightedRandomSampling[T] = | ||
new ChaoSampling[T](sample, random) | ||
|
||
def foldOperation[T] = | ||
(wrs: WeightedRandomSampling[T], i: (T, Double)) => { | ||
wrs.feed(i._1, i._2) | ||
wrs | ||
} | ||
|
||
println(sample) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
rootProject.name = 'random-sampling' | ||
include 'demo' | ||
include 'demo', 'demo-scala' |