Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get NumericEntityRecognizerShell to work again #781

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.clulab.numeric

import org.clulab.processors.clu.BalaurProcessor
import org.clulab.utils.ReloadableProcessor
import org.clulab.utils.ReloadableShell

import java.io.File

class ReloadableNumericProcessor(ruleDirOpt: Option[String]) extends ReloadableProcessor(() => new BalaurProcessor(), true) {

override def get: BalaurProcessor = {
val processor = super.get.asInstanceOf[BalaurProcessor]

// Other code will run without this check, but no Mentions will be produced,
// which would lead to confusion.
assert(processor.numericEntityRecognizerOpt.isDefined)
processor
}

override def reload(): Unit = {
val balaurProcessor = this.get
val numericEntityRecognizerOpt = balaurProcessor
.numericEntityRecognizerOpt
.map(_.reloaded(new File(ruleDirOpt.get)))
val numericEntityRecognizerOptOpt = numericEntityRecognizerOpt.map(Option(_))

processorOpt = Some(balaurProcessor.copy(numericEntityRecognizerOptOpt = numericEntityRecognizerOptOpt))
}
}

class NumericEntityRecognizerShell(ruleDirOpt: Option[String]) extends ReloadableShell {
val proc = new ReloadableNumericProcessor(ruleDirOpt)

/** The actual work, including printing out the output */
def work(text: String): Unit = {
val doc = proc.get.annotate(text)
val mentions = proc.get.numericEntityRecognizerOpt.map(_.extractFrom(doc)).getOrElse(Seq.empty)

setLabelsAndNorms(doc, mentions)
displayMentions(mentions, doc)
}

def reload(): Unit = {
if (ruleDirOpt.isDefined) {
println("The numeric entity recognizer is reloading...")
try {
proc.reload()
}
catch {
case throwable: Throwable =>
println(s"Rules could not be reloaded from ${ruleDirOpt.get}!")
throwable.printStackTrace
}
}
else
println("No directory was specified, possibly on the command line, from which to reload rules!")
}
}

object NumericEntityRecognizerShell extends App {
// args(0) can optionally be a directory from which to reload rules.
new NumericEntityRecognizerShell(args.lift(0)).shell()
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import PostProcessor._
class BalaurProcessor protected (
val config: Config,
val optionalNER: Option[LexiconNER],
numericEntityRecognizerOpt: Option[NumericEntityRecognizer],
val numericEntityRecognizerOpt: Option[NumericEntityRecognizer],
wordTokenizer: Tokenizer,
wordLemmatizer: Lemmatizer,
tokenClassifier: TokenClassifier // multi-task classifier for all tasks addressed
Expand All @@ -44,6 +44,25 @@ class BalaurProcessor protected (
// TokenClassifier.fromFiles(config.getString(s"$prefix.modelName"))
TokenClassifier.fromResources(config.getString(s"$prefix.modelName"))
)

def copy(
configOpt: Option[Config] = None,
optionalNEROpt: Option[Option[LexiconNER]] = None,
numericEntityRecognizerOptOpt: Option[Option[NumericEntityRecognizer]] = None,
wordTokenizerOpt: Option[Tokenizer] = None,
wordLemmatizerOpt: Option[Lemmatizer] = None,
tokenClassifierOpt: Option[TokenClassifier] = None
): BalaurProcessor = {
new BalaurProcessor(
configOpt.getOrElse(this.config),
optionalNEROpt.getOrElse(this.optionalNER),
numericEntityRecognizerOptOpt.getOrElse(this.numericEntityRecognizerOpt),
wordTokenizerOpt.getOrElse(this.wordTokenizer),
wordLemmatizerOpt.getOrElse(this.wordLemmatizer),
tokenClassifierOpt.getOrElse(this.tokenClassifier)
)
}

val eisner = new EisnerEnsembleParser()

override def getConf: Config = config
Expand Down