This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-247
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-247 by this push:
new e748138 WIP.
e748138 is described below
commit e748138e4303466c6d4910fa132a9a67643db6dc
Author: Aaron Radzinzski <[email protected]>
AuthorDate: Sun Feb 21 20:53:41 2021 -0800
WIP.
---
.../nlpcraft/common/makro/NCMacroCompiler.scala | 21 +++++++---
.../common/makro/NCMacroCompilerSpec.scala | 47 ++++++++++++++++++++++
2 files changed, 63 insertions(+), 5 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroCompiler.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroCompiler.scala
index d09e63c..105b9ea 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroCompiler.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroCompiler.scala
@@ -74,7 +74,16 @@ object NCMacroCompiler extends LazyLogging {
require(top.inGroup)
- // TODO.
+ val arg = stack.top
+
+ arg.buffer.flatMap { s ⇒
+ (
+ for (z ← top.buffer) yield
+ for (i ← min to max) yield
+ s + " " + (s"$z " * i)
+ )
+ .flatten.toSet
+ }
// Reset min max.
min = 1
@@ -84,15 +93,17 @@ object NCMacroCompiler extends LazyLogging {
override def exitSyn(ctx: P.SynContext): Unit = {
val syn = if (ctx.TXT() != null) ctx.TXT().getText else
ctx.INT().getText
val top = stack.top
+ val buf = top.buffer
if (top.inGroup)
- top.buffer += syn
- else
- top.buffer.map(_ + " " + syn)
+ buf += syn
+ else {
+ if (buf.isEmpty) buf += syn else buf.map(_ + " " + syn)
+ }
}
override def exitList(ctx: P.ListContext): Unit = if (ctx.UNDERSCORE()
!= null) stack.top.buffer += ""
- override def exitLine(ctx: P.LineContext): Unit = expandedSyns =
stack.pop().buffer.toSet
+ override def exitLine(ctx: P.LineContext): Unit = expandedSyns =
stack.pop().buffer.map(_.trim).toSet
override def exitMinMax(ctx: P.MinMaxContext): Unit = {
implicit val evidence: ParserRuleContext = ctx
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/common/makro/NCMacroCompilerSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/common/makro/NCMacroCompilerSpec.scala
new file mode 100644
index 0000000..2ab7d50
--- /dev/null
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/common/makro/NCMacroCompilerSpec.scala
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nlpcraft.common.makro
+
+import org.junit.jupiter.api.Assertions.assertTrue
+import org.junit.jupiter.api.Test
+
+/**
+ * Unit tests for the macro compiler.
+ */
+class NCMacroCompilerSpec {
+ /**
+ *
+ * @param txt
+ * @param exp
+ */
+ private def checkEq(txt: String, exp: Seq[String]): Unit = {
+ val res = NCMacroCompiler.compile(txt).toSeq.sorted
+ val z = exp.sorted
+
+ if (res != z)
+ println(s"$res != $z")
+
+ assertTrue(res == z)
+ }
+
+ @Test
+ def testCompiler(): Unit = {
+ checkEq("A", Seq("A"))
+ checkEq("A B", Seq("A B"))
+ }
+}