You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2022/12/14 10:32:29 UTC
[incubator-nlpcraft] branch NLPCRAFT-520 updated (4c27312e -> 96e574ae)
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a change to branch NLPCRAFT-520
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
from 4c27312e Update NCOpenNLPTokenEnricher.scala
new b9f683a1 WIP.
new 077ec10e Merge remote-tracking branch 'origin/NLPCRAFT-520' into NLPCRAFT-520
new 96e574ae WIP.
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../nlp/enrichers/NCEnStopWordsTokenEnricher.scala | 61 ++++++++++++++--------
.../nlp/enrichers/NCStopWordsEnricherSpec.scala | 36 +++++++++++++
2 files changed, 74 insertions(+), 23 deletions(-)
[incubator-nlpcraft] 02/03: Merge remote-tracking branch 'origin/NLPCRAFT-520' into NLPCRAFT-520
Posted by se...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-520
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 077ec10e47617f9b9cff67939d441f61105f9c47
Merge: b9f683a1 4c27312e
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Wed Dec 14 14:24:40 2022 +0400
Merge remote-tracking branch 'origin/NLPCRAFT-520' into NLPCRAFT-520
# Conflicts:
# nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
.../src/main/resources/stopwords/stop_words.txt | 2 +-
.../org/apache/nlpcraft/NCPipelineBuilder.scala | 6 +-
.../nlp/enrichers/NCDictionaryTokenEnricher.scala | 2 +-
.../nlp/enrichers/NCEnStopWordsTokenEnricher.scala | 114 ++++++++++-----------
.../nlp/enrichers/NCOpenNLPTokenEnricher.scala | 21 ++--
.../apache/nlpcraft/nlp/stemmer/NCEnStemmer.scala | 5 +-
.../apache/nlpcraft/nlp/stemmer/NCStemmer.scala | 7 +-
.../nlp/parsers/NCOpenNLPTokenParserSpec.scala | 4 +-
8 files changed, 80 insertions(+), 81 deletions(-)
diff --cc nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
index 3ac14f84,64137cda..108fe3aa
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
@@@ -204,11 -204,11 +207,11 @@@ class NCEnStopWordsTokenEnricher
init()
private def read(path: String): Set[String] = NCUtils.readTextGzipResource(path, "UTF-8", logger).toSet
- private def stem(s: String): String = stemmer.stem(s.toLowerCase)
- private def toStemKey(toks: Seq[NCToken]): String = toks.map(_.getText).map(stem).mkString(" ")
+ private def getStem(s: String): String = stemmer.stem(s.toLowerCase)
+ private def toStemKey(toks: Seq[NCToken]): String = toks.map(_.getText).map(getStem).mkString(" ")
/**
- * Stop words holder, used for hash search.
+ * stopword holder, used for hash search.
*
* @param any Any POSes container.
* @param includes Included by POS container.
@@@ -299,8 -299,8 +302,8 @@@
*
*/
private def init(): Unit =
- addStems = if addStopsSet == null then Set.empty else addStopsSet.map(getStem)
- exclStems = if exclStopsSet == null then Set.empty else exclStopsSet.map(getStem)
- addStems = if addSet == null then Set.empty else addSet.map(stem)
- exclStems = if exclSet == null then Set.empty else exclSet.map(stem)
++ addStems = if addSet == null then Set.empty else addSet.map(getStem)
++ exclStems = if exclSet == null then Set.empty else exclSet.map(getStem)
def check(name: String, set: Set[String]): Unit =
if set.exists(_.exists(_.isWhitespace)) then throw E(s"$name contain a string with whitespaces.")
@@@ -431,8 -431,7 +434,7 @@@
if idxWild < 0 then
val (word, form) =
if isCase then (s, ORIG)
- else
- if !hasPoses then (getStem(s), STEM) else (getStem(s), LEM)
- else if !hasPoses then (stem(s), STEM) else (stem(s), LEM)
++ else if !hasPoses then (getStem(s), STEM) else (getStem(s), LEM)
mHash((isExc, form)).addCondition(word, poses)
else
val b = s.take(idxWild)
@@@ -507,14 -502,14 +505,14 @@@
b
/**
- * Marks as stopwords, words with POS from configured list, which also placed before another stop words.
+ * Marks as stopwords, words with POS from configured list, which also placed before another stopword.
*/
- private def processCommonStops(ns: Seq[NCToken], stops: mutable.HashSet[NCToken]): Unit =
+ private def processCommonStops(ns: Seq[NCToken], extraToks: Map[NCToken, TokenExtra], stops: mutable.HashSet[NCToken]): Unit =
/**
- * Marks as stopwords, words with POS from configured list, which also placed before another stop words.
+ * Marks as stopwords, words with POS from configured list, which also placed before another stopword.
*/
@tailrec
- def processCommonStops0(ns: Seq[NCToken]): Unit =
+ def processCommonStops0(ns: Seq[NCToken], extraToks: Map[NCToken, TokenExtra]): Unit =
val max = ns.size - 1
var stop = true
@@@ -626,24 -611,18 +624,22 @@@
// +-------------------------------------------------+
// | Pass #6. |
- // | Processing additional and excluded stop words. |
+ // | Processing additional and excluded stopword. |
// +-------------------------------------------------+
- for ((t, extra) <- extraToks if addStems.contains(extra.stem) || addStems.contains(extra.lemmaStem))
- stops += t
- for (t <- toks if addStems.contains(stem(t.getText))) stops += t
- for (t <- stops.filter(t => exclStems.contains(stem(t.getText)))) stops -= t
++ for ((t, extra) <- extraToks if addStems.contains(extra.stem) || addStems.contains(extra.lemmaStem)) stops += t
+
+ for (t <- stops.filter( t =>
+ val extra = extraToks(t)
+ exclStems.contains(extra.stem) || exclStems.contains(extra.lemmaStem))
- )
- stops -= t
++ ) stops -= t
// +-------------------------------------------------+
// | Pass #7. |
// | Marks as stopwords, words with POS from |
// | configured list, which also placed before |
- // | another stop words. |
+ // | another stopword. |
// +-------------------------------------------------+
- processCommonStops(toks, stops)
+ processCommonStops(toks, extraToks, stops)
// +-------------------------------------------------+
// | Pass #8. |
[incubator-nlpcraft] 03/03: WIP.
Posted by se...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-520
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 96e574aeb832e6f36c5012be62cb2d817abd84fe
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Wed Dec 14 14:32:39 2022 +0400
WIP.
---
.../nlp/enrichers/NCEnStopWordsTokenEnricher.scala | 28 ++++++++++------------
1 file changed, 13 insertions(+), 15 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
index 108fe3aa..367d96be 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
@@ -201,8 +201,8 @@ class NCEnStopWordsTokenEnricher(
private var stopWords: StopWordHolder = _
private var exceptions: StopWordHolder = _
- private case class TokenExtra(lemma: String, stem: String):
- val lemmaStem: String = getStem(lemma)
+ private case class TokenExtra(lemma: String, stemTxt: String):
+ val stemLemma: String = getStem(lemma)
init()
@@ -521,8 +521,8 @@ class NCEnStopWordsTokenEnricher(
extra = extraToks(tok)
if
idx != max && !isStopWord(tok) &&
- !exclStems.contains(extra.stem) &&
- !exclStems.contains(extra.lemmaStem) &&
+ !exclStems.contains(extra.stemTxt) &&
+ !exclStems.contains(extra.stemLemma) &&
POSES.contains(getPos(tok)) && isStopWord(ns(idx + 1)))
stops += tok
stop = false
@@ -550,7 +550,7 @@ class NCEnStopWordsTokenEnricher(
val idx = tok.getIndex
val pos = getPos(tok)
val lemma = extra.lemma
- val st = extra.stem
+ val st = extra.stemTxt
def isFirst: Boolean = idx == 0
def isLast: Boolean = idx == toks.length - 1
@@ -618,32 +618,30 @@ class NCEnStopWordsTokenEnricher(
// +-------------------------------------------------+
// | Pass #5. |
- // | Mark words with POSes before stopwords. |
+ // | Mark words with POSes before stopwords. |
// +-------------------------------------------------+
markBefore(toks, STOP_BEFORE_STOP, toks.size - 1, isException, stops)
// +-------------------------------------------------+
// | Pass #6. |
- // | Processing additional and excluded stopword. |
+ // | Processing additional and excluded stopword. |
// +-------------------------------------------------+
- for ((t, extra) <- extraToks if addStems.contains(extra.stem) || addStems.contains(extra.lemmaStem)) stops += t
+ def has(set: Set[String], extra: TokenExtra) = set.contains(extra.stemTxt) || set.contains(extra.stemLemma)
- for (t <- stops.filter( t =>
- val extra = extraToks(t)
- exclStems.contains(extra.stem) || exclStems.contains(extra.lemmaStem))
- ) stops -= t
+ for ((t, extra) <- extraToks if has(addStems, extra)) stops += t
+ for ((t, _) <- stops.map(t => t -> extraToks(t)).filter { (_, extra) => has(exclSet, extra)}) stops -= t
// +-------------------------------------------------+
// | Pass #7. |
// | Marks as stopwords, words with POS from |
// | configured list, which also placed before |
- // | another stopword. |
+ // | another stopword. |
// +-------------------------------------------------+
processCommonStops(toks, extraToks, stops)
// +-------------------------------------------------+
// | Pass #8. |
- // | Deletes stopword if they are marked as quoted.|
+ // | Deletes stopword if they are marked as quoted. |
// +-------------------------------------------------+
var quotes = toks.filter(isQuote)
@@ -663,7 +661,7 @@ class NCEnStopWordsTokenEnricher(
// +-------------------------------------------------+
// | Pass #9. |
- // | Deletes stopword if they are brackets. |
+ // | Deletes stopword if they are brackets. |
// +-------------------------------------------------+
val stack = new java.util.Stack[String]()
val set = mutable.HashSet.empty[NCToken]
[incubator-nlpcraft] 01/03: WIP.
Posted by se...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-520
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit b9f683a1d77a5e54052a58e9f8f0c3dd45fe57d4
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Wed Dec 14 14:19:39 2022 +0400
WIP.
---
.../nlp/enrichers/NCEnStopWordsTokenEnricher.scala | 54 ++++++++++++++--------
.../nlp/enrichers/NCStopWordsEnricherSpec.scala | 36 +++++++++++++++
2 files changed, 71 insertions(+), 19 deletions(-)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
index 3fc1e57a..3ac14f84 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/nlp/enrichers/NCEnStopWordsTokenEnricher.scala
@@ -198,11 +198,14 @@ class NCEnStopWordsTokenEnricher(
private var stopWords: StopWordHolder = _
private var exceptions: StopWordHolder = _
+ private case class TokenExtra(lemma: String, stem: String):
+ val lemmaStem: String = getStem(lemma)
+
init()
private def read(path: String): Set[String] = NCUtils.readTextGzipResource(path, "UTF-8", logger).toSet
- private def stem(s: String): String = stemmer.stem(s.toLowerCase)
- private def toStemKey(toks: Seq[NCToken]): String = toks.map(_.getText).map(stem).mkString(" ")
+ private def getStem(s: String): String = stemmer.stem(s.toLowerCase)
+ private def toStemKey(toks: Seq[NCToken]): String = toks.map(_.getText).map(getStem).mkString(" ")
/**
* Stop words holder, used for hash search.
@@ -293,11 +296,11 @@ class NCEnStopWordsTokenEnricher(
wildcardsOrigins.matches(toOriginalKey(toks), posOpt)
/**
- *
+ *
*/
private def init(): Unit =
- addStems = if addStopsSet == null then Set.empty else addStopsSet.map(stem)
- exclStems = if exclStopsSet == null then Set.empty else exclStopsSet.map(stem)
+ addStems = if addStopsSet == null then Set.empty else addStopsSet.map(getStem)
+ exclStems = if exclStopsSet == null then Set.empty else exclStopsSet.map(getStem)
def check(name: String, set: Set[String]): Unit =
if set.exists(_.exists(_.isWhitespace)) then throw E(s"$name contain a string with whitespaces.")
@@ -308,7 +311,7 @@ class NCEnStopWordsTokenEnricher(
val dups = addStems.intersect(exclStems)
if dups.nonEmpty then E(s"Duplicate stems detected between additional and excluded stopwords [dups=${dups.mkString(",")}]")
- percents = PERCENTS.map(stem)
+ percents = PERCENTS.map(getStem)
// Stemmatization is done already by generator.
NCUtils.execPar(
@@ -429,7 +432,7 @@ class NCEnStopWordsTokenEnricher(
val (word, form) =
if isCase then (s, ORIG)
else
- if !hasPoses then (stem(s), STEM) else (stem(s), LEM)
+ if !hasPoses then (getStem(s), STEM) else (getStem(s), LEM)
mHash((isExc, form)).addCondition(word, poses)
else
val b = s.take(idxWild)
@@ -506,23 +509,29 @@ class NCEnStopWordsTokenEnricher(
/**
* Marks as stopwords, words with POS from configured list, which also placed before another stop words.
*/
- private def processCommonStops(ns: Seq[NCToken], stops: mutable.HashSet[NCToken]): Unit =
+ private def processCommonStops(ns: Seq[NCToken], extraToks: Map[NCToken, TokenExtra], stops: mutable.HashSet[NCToken]): Unit =
/**
* Marks as stopwords, words with POS from configured list, which also placed before another stop words.
*/
@tailrec
- def processCommonStops0(ns: Seq[NCToken]): Unit =
+ def processCommonStops0(ns: Seq[NCToken], extraToks: Map[NCToken, TokenExtra]): Unit =
val max = ns.size - 1
var stop = true
- for ((tok, idx) <- ns.zipWithIndex if idx != max && !isStopWord(tok) && !exclStems.contains(stem(tok.getText)) &&
- POSES.contains(getPos(tok)) && isStopWord(ns(idx + 1)))
+ for (
+ (tok, idx) <- ns.zipWithIndex;
+ extra = extraToks(tok)
+ if
+ idx != max && !isStopWord(tok) &&
+ !exclStems.contains(extra.stem) &&
+ !exclStems.contains(extra.lemmaStem) &&
+ POSES.contains(getPos(tok)) && isStopWord(ns(idx + 1)))
stops += tok
stop = false
- if !stop then processCommonStops0(ns)
+ if !stop then processCommonStops0(ns, extraToks)
- processCommonStops0(ns)
+ processCommonStops0(ns, extraToks)
/** @inheritdoc */
override def enrich(req: NCRequest, cfg: NCModelConfig, toks: List[NCToken]): Unit =
@@ -535,11 +544,15 @@ class NCEnStopWordsTokenEnricher(
val stops = mutable.HashSet.empty[NCToken]
- for (tok <- toks)
+ val extraToks =
+ scala.collection.mutable.LinkedHashMap.empty[NCToken, TokenExtra] ++=
+ toks.map(t => t -> TokenExtra(getLemma(t), getStem(t.getText)))
+
+ for ((tok, extra) <- extraToks)
val idx = tok.getIndex
val pos = getPos(tok)
- val lemma = getLemma(tok)
- val st = stem(tok.getText)
+ val lemma = extra.lemma
+ val st = extra.stem
def isFirst: Boolean = idx == 0
def isLast: Boolean = idx == toks.length - 1
@@ -615,10 +628,13 @@ class NCEnStopWordsTokenEnricher(
// | Pass #6. |
// | Processing additional and excluded stop words. |
// +-------------------------------------------------+
- for (t <- toks if addStems.contains(stem(t.getText)))
+ for ((t, extra) <- extraToks if addStems.contains(extra.stem) || addStems.contains(extra.lemmaStem))
stops += t
- for (t <- stops.filter(t => exclStems.contains(stem(t.getText))))
+ for (t <- stops.filter( t =>
+ val extra = extraToks(t)
+ exclStems.contains(extra.stem) || exclStems.contains(extra.lemmaStem))
+ )
stops -= t
// +-------------------------------------------------+
@@ -627,7 +643,7 @@ class NCEnStopWordsTokenEnricher(
// | configured list, which also placed before |
// | another stop words. |
// +-------------------------------------------------+
- processCommonStops(toks, stops)
+ processCommonStops(toks, extraToks, stops)
// +-------------------------------------------------+
// | Pass #8. |
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/enrichers/NCStopWordsEnricherSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/enrichers/NCStopWordsEnricherSpec.scala
index d0c92d40..142c16b4 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/enrichers/NCStopWordsEnricherSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/nlp/enrichers/NCStopWordsEnricherSpec.scala
@@ -21,6 +21,7 @@ import org.apache.nlpcraft.*
import internal.util.NCResourceReader
import nlp.util.*
import nlp.enrichers.NCEnStopWordsTokenEnricher
+import org.apache.nlpcraft.nlp.stemmer.NCStemmer
import org.scalatest.funsuite.AnyFunSuite
/**
@@ -60,4 +61,39 @@ class NCStopWordsEnricherSpec extends AnyFunSuite:
false,
true
)
+ // The synonym is defined as lemma => all kind of input words should be found.
+ test(
+ new NCEnStopWordsTokenEnricher(Set("woman")),
+ "woman women",
+ true,
+ true
+ )
+ // The synonym is defined in some form => only in the same form input words should be found.
+ test(
+ new NCEnStopWordsTokenEnricher(Set("women")),
+ "woman women",
+ false,
+ true
+ )
+ // The synonym is defined in some form, but stemmer is very rough => all kind of input words should be found.
+ test(
+ new NCEnStopWordsTokenEnricher(addStopsSet = Set("women"), stemmer = _.take(3)),
+ "woman women",
+ true,
+ true
+ )
+ // The synonym is defined as lemma => all kind of input words should be found, but excluded set is defined.
+ test(
+ new NCEnStopWordsTokenEnricher(Set("woman"), Set("women")),
+ "woman women",
+ true,
+ false
+ )
+ // Very rough stemmer defined.
+ test(
+ new NCEnStopWordsTokenEnricher(addStopsSet = Set("women"), stemmer = _.head.toString),
+ "weather windows",
+ true,
+ true
+ )
}
\ No newline at end of file