Skip to content

Commit 6bd4c0b

Browse files
committedMay 23, 2018
don't show bad sentences on annotation
1 parent d747b1c commit 6bd4c0b

File tree

7 files changed

+103
-15
lines changed

7 files changed

+103
-15
lines changed
 

‎.scalafmt.conf

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ align = some
33
align.openParenDefnSite = false
44
rewrite.rules = [AvoidInfix, SortImports, RedundantParens, PreferCurlyFors]
55
rewrite.neverInfix.excludeFilters += "min"
6-
rewrite.neverInfix.excludeFilters += "max"
6+
rewrite.neverInfix.excludeFilters += "max"
7+
rewrite.neverInfix.excludeFilters += "ne"
8+
rewrite.neverInfix.excludeFilters += "eq"

‎play/app/code/annotation/AnnotationDb.scala

+44-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import net.codingwell.scalaguice.ScalaModule
1111
import org.apache.commons.lang3.RandomUtils
1212
import org.joda.time.DateTime
1313
import play.api.Configuration
14-
import play.api.mvc.Result
1514
import reactivemongo.api.collections.bson.BSONCollection
1615
import reactivemongo.api.commands.UpdateWriteResult
1716
import reactivemongo.api.{Cursor, DefaultDB, MongoConnection}
@@ -113,8 +112,8 @@ object BsonPicklers {
113112

114113
object AnnotationToolUser {
115114
implicit val bsonRw: BSONDocumentHandler[AnnotationToolUser] = Macros.handler[AnnotationToolUser]
116-
import prickle._
117115
import BsonPicklers._
116+
import prickle._
118117

119118
implicit val pickler: Pickler[AnnotationToolUser] =
120119
prickle.Pickler.materializePickler[AnnotationToolUser]
@@ -304,6 +303,29 @@ object SentenceBSON {
304303

305304
class SentenceDbo @Inject()(db: AnnotationDb)(implicit ec: ExecutionContext) extends StrictLogging {
306305

306+
private val coll = db.db.collection[BSONCollection]("sentences")
307+
308+
import SentenceBSON._
309+
310+
def recalcStatuses() = {
311+
coll.find(BSONDocument()).cursor[Sentence]().fold(NotUsed) {
312+
case (_, doc) =>
313+
val newStatus = SentenceUtils.computeStatus(doc)
314+
if (newStatus != doc.status) {
315+
val search = BSONDocument(
316+
"_id" -> doc.id
317+
)
318+
val upd = BSONDocument(
319+
"$set" -> BSONDocument(
320+
"status" -> newStatus.value
321+
)
322+
)
323+
coll.update(search, upd)
324+
}
325+
NotUsed
326+
}
327+
}
328+
307329
def findAlreadyReported(surfaceComment: String): Future[Option[String]] = {
308330
val q = BSONDocument(
309331
"tags" -> "report",
@@ -312,16 +334,12 @@ class SentenceDbo @Inject()(db: AnnotationDb)(implicit ec: ExecutionContext) ext
312334

313335
val proj = BSONDocument("_id" -> 1)
314336

315-
coll.find(q, proj).one.map {
337+
coll.find(q, proj).one[BSONDocument].map {
316338
case Some(doc) => doc.getAs[String]("_id")
317339
case _ => None
318340
}
319341
}
320342

321-
private val coll = db.db.collection[BSONCollection]("sentences")
322-
323-
import SentenceBSON._
324-
325343
def checkExistance(ids: Seq[String]): Future[Set[String]] = {
326344
if (ids.isEmpty) {
327345
return Future.successful(Set.empty)
@@ -384,6 +402,10 @@ class SentenceDbo @Inject()(db: AnnotationDb)(implicit ec: ExecutionContext) ext
384402
doc.merge(
385403
"status" -> SentenceStatus.WorkRequired.value
386404
)
405+
case "rj" | "rej" =>
406+
doc.merge(
407+
"status" -> SentenceStatus.Rejected.value
408+
)
387409
case "bad" | "ng" =>
388410
doc.merge(
389411
"status" -> BSONDocument(
@@ -460,6 +482,13 @@ class SentenceDbo @Inject()(db: AnnotationDb)(implicit ec: ExecutionContext) ext
460482
"$ne" -> user._id
461483
)
462484
)
485+
if (req.query == "") {
486+
q = q.merge(
487+
"status" -> BSONDocument(
488+
"$ne" -> SentenceStatus.Rejected.value
489+
)
490+
)
491+
}
463492
}
464493

465494
if (req.reviewedBefore.isDefined) {
@@ -659,6 +688,13 @@ class SentenceDbo @Inject()(db: AnnotationDb)(implicit ec: ExecutionContext) ext
659688
}
660689

661690
object SentenceUtils {
691+
692+
val badAnnotations = Set(
693+
"どうでもいい",
694+
"入力:意味不明",
695+
"入力:誤字脱字"
696+
)
697+
662698
def cleanTags(blocks: Seq[SentenceBlock], allowedFields: Set[String]): Seq[SentenceBlock] = {
663699
blocks.map { b =>
664700
b.copy(spans = b.spans.map { s =>
@@ -689,6 +725,7 @@ object SentenceUtils {
689725
} else {
690726
val values = anns.map(_.value).distinct
691727
values match {
728+
case vs if vs.exists(badAnnotations.contains) => SentenceStatus.Rejected
692729
case Seq(XInt(_)) => SentenceStatus.TotalAgreement
693730
case vs if vs.forall(v => XInt.unapply(v).isDefined) => SentenceStatus.PartialAgreement
694731
case _ => SentenceStatus.WorkRequired

‎play/app/code/annotation/LogDbo.scala

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package code.annotation
2+
3+
import javax.inject.Inject
4+
import reactivemongo.api.collections.bson.BSONCollection
5+
import reactivemongo.bson.Subtype.GenericBinarySubtype
6+
import reactivemongo.bson.{BSONBinary, BSONDateTime, BSONDocument, BSONObjectID}
7+
8+
import scala.concurrent.ExecutionContext
9+
10+
class LogDbo @Inject()(
11+
db: AnnotationDb
12+
)(implicit ec: ExecutionContext) {
13+
14+
private val coll = db.db.collection[BSONCollection]("sentenceActions")
15+
16+
def log(req: SentenceRequest, user: AnnotationToolUser): Unit = {
17+
val doc = BSONDocument(
18+
"_id" -> BSONObjectID.generate(),
19+
"user" -> user._id,
20+
"kind" -> req.request.number,
21+
"date" -> BSONDateTime(System.currentTimeMillis()),
22+
"blob" -> BSONBinary(req.toByteArray, GenericBinarySubtype)
23+
)
24+
coll.insert(doc)
25+
}
26+
}

‎play/app/controllers/SentenceApiController.scala

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package controllers
22

3-
import java.time.{Instant, LocalDateTime}
3+
import java.time.LocalDateTime
44
import java.time.format.DateTimeFormatter
55

6-
import javax.inject.Inject
76
import code.annotation._
87
import code.transport.lattice.{
98
CandidateNode,
@@ -12,6 +11,7 @@ import code.transport.lattice.{
1211
PartialAnalysisQuery
1312
}
1413
import com.typesafe.scalalogging.StrictLogging
14+
import javax.inject.Inject
1515
import play.api.mvc.{InjectedController, Result}
1616
import reactivemongo.bson.BSONObjectID
1717
import ws.kotonoha.akane.analyzers.jumanpp.wire.lattice.LatticeDump
@@ -21,11 +21,14 @@ import scala.concurrent.{ExecutionContext, Future}
2121

2222
class SentenceApiController @Inject()(
2323
dbo: SentenceDbo,
24+
objLog: LogDbo,
2425
reportSvc: SentenceReportService
2526
)(implicit ec: ExecutionContext)
2627
extends InjectedController {
2728

2829
def doHandle(body: SentenceRequest, user: AnnotationToolUser): Future[Result] = {
30+
objLog.log(body, user)
31+
2932
body.request match {
3033
case SentenceRequest.Request.Sentences(sents) =>
3134
dbo.getSentences(user, sents).map(sents => Ok(LiftPB(sents)))
@@ -55,6 +58,14 @@ class SentenceApiController @Inject()(
5558
dbo.saveBadLog(uid, req).map { _ =>
5659
Ok(LiftPB(Annotation()))
5760
}
61+
case SentenceRequest.Request.RecalcStatuses(_) =>
62+
if (user.admin) {
63+
dbo.recalcStatuses().map { _ =>
64+
Ok(LiftPB(Annotation()))
65+
}
66+
} else {
67+
Future.successful(Forbidden("You are not admin"))
68+
}
5869
case _ => Future.successful(NotImplemented)
5970
}
6071
}
@@ -94,13 +105,13 @@ class BlockSeqBuilder {
94105
private val blocks = new ArrayBuffer[SentenceBlock]()
95106

96107
private def addNode(node: CandidateNode) = {
97-
if (nodes.isEmpty || (nodes.last.ne(node))) {
108+
if (nodes.isEmpty || nodes.last.ne(node)) {
98109
nodes += node
99110
}
100111
}
101112

102113
private def addPart(part: EditableSentencePart) = {
103-
if (parts.isEmpty || (parts.last.ne(part))) {
114+
if (parts.isEmpty || parts.last.ne(part)) {
104115
parts += part
105116
}
106117
}

‎scalajs/src/main/scala/code/annotation/SentenceAnnotation.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ case class SentenceAnnotation(apiSvc: ApiService, uid: ObjId, isAdmin: Boolean)
666666
}
667667
),
668668
<.button(
669-
"Reject",
669+
"No Candidate",
670670
^.onClick --> cback(CommentEdit("Reject", state.comment, state.focus))
671671
)
672672
)

‎scalajs/src/main/scala/code/annotation/SentenceImport.scala

+12-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ object SentenceImport {
2626
}
2727
.runNow()
2828
}
29-
3029
}
3130

31+
def recalcStatuses(): Callback =
32+
scope.props.map { api =>
33+
val req = SentenceRequest(SentenceRequest.Request.RecalcStatuses(0))
34+
api.sentenceCall[Annotation](req)
35+
}.void
36+
3237
def close() = Callback {
3338
if (connection != null) {
3439
connection.close()
@@ -76,7 +81,12 @@ object SentenceImport {
7681
"Import"
7782
),
7883
<.h2("Messages"),
79-
renderMessages(s.messages)
84+
renderMessages(s.messages),
85+
<.h2("Controls"),
86+
<.button(
87+
^.onClick --> recalcStatuses(),
88+
"Recompute Statuses"
89+
)
8090
)
8191
}
8292

‎shared/src/main/protobuf/sentences.proto

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum SentenceStatus {
3838
TotalAgreement = 1;
3939
PartialAgreement = 2;
4040
WorkRequired = 3;
41+
Rejected = 4;
4142
}
4243

4344
message Review {
@@ -72,6 +73,7 @@ message SentenceRequest {
7273
MergeEdits merge = 3;
7374
ReviewSentence review = 4;
7475
ReportAllBad bothBad = 5;
76+
int32 recalcStatuses = 6;
7577
}
7678
}
7779

0 commit comments

Comments
 (0)
Please sign in to comment.