Skip to content

Commit

Permalink
move rating to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
intracer committed Mar 16, 2024
1 parent 50861c2 commit 58c52e6
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.time.{ZoneOffset, ZonedDateTime}
import org.scalawiki.dto.markup.Table
import org.scalawiki.wlx.dto.{AdmDivision, Contest, Monument}
import org.scalawiki.wlx.query.MonumentQuery
import org.scalawiki.wlx.stat.PerPlaceStat
import org.scalawiki.wlx.stat.rating.PerPlaceStat

class MonumentDB(
val contest: Contest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.typesafe.config.{
ConfigParseOptions,
ConfigResolveOptions
}
import org.scalawiki.wlx.stat.RateConfig
import org.scalawiki.wlx.stat.rating.RateConfig

import scala.util.Try

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.scalawiki.wlx.stat

import org.scalawiki.wlx.stat.rating.RateConfig

import java.time.ZonedDateTime

case class StatConfig(
Expand Down Expand Up @@ -124,7 +126,7 @@ object StatParams {
exceptRegions = conf.exceptRegions.getOrElse(Nil),
cities = conf.cities.getOrElse(Nil),
exceptCities = conf.exceptCities.getOrElse(Nil),
rateConfig = RateConfig(conf),
rateConfig = rating.RateConfig(conf),
gallery = conf.gallery.getOrElse(false),
fillLists = conf.fillLists.getOrElse(false),
wrongIds = conf.wrongIds.getOrElse(false),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.scalawiki.wlx.stat
package org.scalawiki.wlx.stat.rating

import com.typesafe.config.Config

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.scalawiki.wlx.stat
package org.scalawiki.wlx.stat.rating

import com.typesafe.config.Config
import org.scalawiki.MwBot
import org.scalawiki.wlx.ImageDB
import org.scalawiki.wlx.stat.reports.RateInputDistribution
import org.scalawiki.wlx.stat.{ContestStat, StatParams}

import scala.collection.mutable
import scala.util.Try
Expand Down Expand Up @@ -69,46 +70,46 @@ object Rater {
stat.contest.config.map(fromConfig(stat, _)).getOrElse {
val config = stat.contest.rateConfig

val raters = Seq(new NumberOfMonuments(stat, config.baseRate)) ++
val raters = Seq(NumberOfMonuments(stat, config.baseRate)) ++
config.newAuthorObjectRating
.map(r =>
new NewlyPicturedPerAuthorBonus(
NewlyPicturedPerAuthorBonus(
stat,
config.newObjectRating.getOrElse(1),
r
)
)
.orElse(
config.newObjectRating.map(new NewlyPicturedBonus(stat, _))
config.newObjectRating.map(NewlyPicturedBonus(stat, _))
)

if (raters.tail.isEmpty) {
raters.head
} else {
new RateSum(stat, raters)
RateSum(stat, raters)
}
}
}

def fromConfig(stat: ContestStat, config: Config): Rater = {
val rateCfg = config.getConfig(s"rates.${stat.contest.year}")
val raters = Seq(
new NumberOfMonuments(
NumberOfMonuments(
stat,
Try(rateCfg.getDouble("base-rate")).toOption.getOrElse(1)
)
) ++
(if (rateCfg.hasPath("number-of-authors-bonus")) {
Seq(
new NumberOfAuthorsBonus(
NumberOfAuthorsBonus(
stat,
RateRanges(rateCfg.getConfig("number-of-authors-bonus"))
)
)
} else Nil) ++
(if (rateCfg.hasPath("number-of-images-bonus")) {
Seq(
new NumberOfImagesInPlaceBonus(
NumberOfImagesInPlaceBonus(
stat,
RateRanges(rateCfg.getConfig("number-of-images-bonus"))
)
Expand All @@ -124,7 +125,8 @@ object Rater {
}
}

case class NumberOfMonuments(stat: ContestStat, baseRate: Double) extends Rater {
case class NumberOfMonuments(stat: ContestStat, baseRate: Double)
extends Rater {
val monumentIds = stat.monumentDb.map(_.ids).getOrElse(Set.empty)

override def rate(monumentId: String, author: String): Double = {
Expand Down Expand Up @@ -158,7 +160,7 @@ case class NewlyPicturedBonus(stat: ContestStat, newlyPicturedRate: Double)
}

case class NewlyPicturedPerAuthorBonus(
val stat: ContestStat,
stat: ContestStat,
newlyPicturedRate: Double,
newlyPicturedPerAuthorRate: Double
) extends Rater {
Expand Down Expand Up @@ -469,8 +471,8 @@ case class RateSum(stat: ContestStat, raters: Seq[Rater]) extends Rater {
}

override def explain(monumentId: String, author: String): String = {
val disqulifiedRater = raters.find(_.disqualify(monumentId, author))
disqulifiedRater.fold(
val disqualifiedRater = raters.find(_.disqualify(monumentId, author))
disqualifiedRater.fold(
s"Rating = ${raters.map(_.rate(monumentId, author)).sum}, is a sum of: " + raters
.map(_.explain(monumentId, author))
.mkString(", ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.scalawiki.MwBot
import org.scalawiki.dto.markup.Table
import org.scalawiki.wlx.ImageDB
import org.scalawiki.wlx.stat._
import org.scalawiki.wlx.stat.rating.{NumberOfImagesInPlaceBonus, RateSum, Rater}

class AuthorMonuments(
val stat: ContestStat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package org.scalawiki.wlx.stat.reports
import org.scalawiki.MwBot
import org.scalawiki.dto.Image
import org.scalawiki.wlx.dto.Contest
import org.scalawiki.wlx.stat.{ContestStat, RateSum, Rater}
import org.scalawiki.wlx.stat.rating.{RateSum, Rater}
import org.scalawiki.wlx.stat.ContestStat
import org.scalawiki.wlx.{ImageDB, MonumentDB}

import scala.concurrent.ExecutionContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package org.scalawiki.wlx.stat.reports

import org.scalawiki.MwBot
import org.scalawiki.dto.markup.Table
import org.scalawiki.wlx.stat.{ContestStat, Rater, StatConfig, Stats}
import org.scalawiki.wlx.stat.rating.Rater
import org.scalawiki.wlx.stat.{ContestStat, StatConfig, Stats}
import org.scalawiki.wlx.{ImageDB, ImageFiller, MonumentDB}

import scala.concurrent.ExecutionContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.scalawiki.dto.Image
import org.scalawiki.dto.markup.Table
import org.scalawiki.wlx.dto._
import org.scalawiki.wlx.dto.lists.ListConfig._
import org.scalawiki.wlx.stat.rating.RateConfig
import org.scalawiki.wlx.stat.reports.AuthorMonuments
import org.scalawiki.wlx.{ImageDB, MonumentDB}
import org.specs2.mutable.Specification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.typesafe.config.ConfigFactory
import org.scalawiki.dto.Image
import org.scalawiki.wlx.{ImageDB, MonumentDB}
import org.scalawiki.wlx.dto.{Contest, ContestType, Country, Monument}
import org.scalawiki.wlx.stat.rating.NumberOfAuthorsBonus
import org.specs2.mutable.Specification

class NumberOfAuthorsBonusSpec extends Specification {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.scalawiki.wlx.stat

import java.time.ZonedDateTime
import org.scalawiki.wlx.stat.rating.RateConfig

import java.time.ZonedDateTime
import org.specs2.mutable.Specification

class StatParamsSpec extends Specification {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.scalawiki.wlx.stat
package org.scalawiki.wlx.stat.rating

import com.typesafe.config.ConfigFactory
import org.scalawiki.wlx.dto.ContestType.{WLE, WLM}
import org.scalawiki.wlx.dto.{Contest, ContestType, Country}
import org.scalawiki.wlx.stat.RaterSpec.loadRater
import org.scalawiki.wlx.stat.ContestStat
import org.scalawiki.wlx.{ImageDB, MonumentDB}
import org.specs2.mutable.Specification

Expand Down

0 comments on commit 58c52e6

Please sign in to comment.