Skip to content
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
14 changes: 10 additions & 4 deletions NLPPlus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def clear_logs(self, analyzer_name: PathLike) -> int:
return removed

def analyze(self, text: str, analyzer_name: PathLike, develop: bool = False,
compiled: bool = False) -> Results:
compiled: bool = False, datum: Optional[str] = None) -> Results:
"""Analyze text with the named analyzer.

Args:
Expand All @@ -287,14 +287,18 @@ def analyze(self, text: str, analyzer_name: PathLike, develop: bool = False,
package README for the cmake / cloud build step
that turns them into the actual ``.so``/``.dylib``/
``.dll`` files.
datum: a string the analyzer reads as ``G("$datum")``, for this
call only -- eg the output format the caller wants
(``"json"``, ``"xml"``). None (the default) is none, and
``G("$datum")`` is then 0.
"""
outdir = self.analyzer_dir(analyzer_name) / "output"
# Delete the files of the previous run in the outdir.
for file_path in glob.glob(str(outdir / "*")):
if os.path.isfile(file_path):
os.remove(file_path)
outtext = self.engine.analyze(self._engine_analyzer_arg(analyzer_name),
text, develop, compiled)
text, develop, compiled, datum or "")
return Results(outtext, outdir)

def compile(self, analyzer_name: PathLike, develop: bool = False,
Expand Down Expand Up @@ -524,14 +528,16 @@ def put_json_object(analyzer_name: str, obj: Any, name: str) -> Path:


def analyze(text: str, parser: str = "parse-en-us", develop: bool = False,
compiled: bool = False) -> str:
compiled: bool = False, datum: Optional[str] = None) -> str:
"""Run the analyzer named on the input string.

If ``compiled=True``, the engine loads the analyzer's compiled
shared libraries (``bin/run.<ext>`` and ``bin/kb.<ext>``) instead of
running interpreted. See :func:`compile` for producing those.
``datum`` reaches the analyzer as ``G("$datum")``; see
:meth:`Engine.analyze`.
"""
return engine.analyze(text, parser, develop, compiled).output_text
return engine.analyze(text, parser, develop, compiled, datum).output_text


def compile(analyzer: str = "parse-en-us", develop: bool = False,
Expand Down
11 changes: 8 additions & 3 deletions bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ wrap_init(NLP_ENGINE *self, const std::string &workingFolder,
const std::string
wrap_analyze(NLP_ENGINE &engine, const std::string &parser,
const std::string &input, const bool develop,
const bool compiled) {
const bool compiled, const std::string &datum) {
CLocaleGuard cLocale;
// For this call only: the analyzer sees it as G("$datum"), eg the
// output format the caller wants. "" (the default) is none.
engine.setDatum(datum.c_str());
_TCHAR *_parser = _tcsdup(parser.c_str());
std::istringstream instream(input);
std::ostringstream outstream;
Expand Down Expand Up @@ -184,7 +187,7 @@ NB_MODULE(bindings, m) {
"silent"_a = true)
.def("analyze", &wrap_analyze,
"parser"_a, "input"_a, "develop"_a = false,
"compiled"_a = false,
"compiled"_a = false, "datum"_a = "",
"Analyze `input` with `parser`.\n"
"The `parser` argument refers to an analyzer contained in the\n"
"`analyzers` folder inside the workingFolder used to create\n"
Expand All @@ -193,7 +196,9 @@ NB_MODULE(bindings, m) {
"bin/kb.<ext> from the analyzer dir instead of running\n"
"interpreted from the .nlp source. Build those shared\n"
"libraries first via `compile()` plus an external cmake/\n"
"cloud build step.")
"cloud build step.\n"
"`datum` reaches the analyzer as G(\"$datum\"), eg the output\n"
"format wanted; \"\" (the default) is none.")
.def("compile", &wrap_compile,
"analyzer"_a, "develop"_a = false, "kbOnly"_a = false,
"analyzerOnly"_a = false,
Expand Down
254 changes: 254 additions & 0 deletions tests/analyzers/datum/kb/user/attr.kb
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
ind attr
"concept"
0
"concept" "sys" "attrs" "common" "var"
psym
"cg_CONCEPT"
end ind


ind attr
"concept" "gram"
0
"concept" "sys" "dict" "a" "x" "xu" "xwhite"
pst
"true"
end ind


ind attr
"concept" "gram"
0
"concept" "sys" "dict" "a" "c" "co" "constrain"
pst
"true"
end ind


ind attr
"concept" "gram"
0
"concept" "sys" "dict" "a" "c" "cl" "closed"
pst
"false"
end ind


ind attr
"concept" "gram"
0
"concept" "sys" "dict" "a" "l" "li" "lit"
pst
"false"
end ind


ind attr
"concept" "gram"
0
"concept" "sys" "dict" "a" "s" "sp" "split"
pst
"false"
end ind


ind attr
"concept" "gram"
0
"concept" "sys" "dict" "a" "g" "ge" "gen"
pst
"false"
end ind


ind attr
"concept" "gram"
0
"concept" "sys" "dict" "a" "l" "lo" "locked"
pst
"false"
end ind


ind attr
"concept" "gram"
0
"concept" "sys" "dict" "a" "d" "di" "dirty"
pst
"true"
end ind


ind attr
"concept" "gram"
0
"concept" "sys" "dict" "a" "q" "qu" "quicksem"
pst
"false"
end ind


ind attr
"concept" "sys"
0
"concept" "sys" "attrs" "common" "var"
psym
"con_SYS"
end ind


ind attr
"concept" "sys" "attrs"
0
"concept" "sys" "attrs" "common" "var"
psym
"sys_ATTRS"
end ind


ind attr
"concept" "sys" "attrs" "proxy"
0
"concept" "sys" "attrs" "common" "var"
psym
"attrs_PROXY"
end ind


ind attr
"concept" "sys" "attrs" "proxy" "atom"
0
"concept" "sys" "attrs" "common" "var"
psym
"cg_ATOM"
end ind


ind attr
"concept" "sys" "attrs" "pproxy"
0
"concept" "sys" "attrs" "common" "var"
psym
"attrs_PPROXY"
end ind


ind attr
"concept" "sys" "attrs" "pproxy" "phrase"
0
"concept" "sys" "attrs" "common" "var"
psym
"cg_PHRASE"
end ind


ind attr
"concept" "sys" "attrs" "common"
0
"concept" "sys" "attrs" "common" "var"
psym
"attrs_COMMON"
end ind


ind attr
"concept" "sys" "attrs" "common" "var"
0
"concept" "sys" "attrs" "common" "var"
psym
"cg_VAR"
end ind


ind attr
"concept" "sys" "nlp"
0
"concept" "sys" "attrs" "common" "var"
psym
"sys_NLP"
end ind


ind attr
"concept" "sys" "nlp" "lex" "slot" "cases"
0
"concept" "sys" "attrs" "common" "var"
psym
"id_CASES"
end ind


ind attr
"concept" "sys" "mean" "slot" "case"
0
"concept" "sys" "attrs" "common" "var"
psym
"id_mslot_CASE"
end ind


ind attr
"concept" "sys" "mean" "slot" "case" "cap"
0
"concept" "sys" "attrs" "common" "var"
psym
"id_CAP"
end ind


ind attr
"concept" "sys" "mean" "slot" "case" "allcap"
0
"concept" "sys" "attrs" "common" "var"
psym
"id_ALLCAP"
end ind


ind attr
"concept" "sys" "mean" "slot" "case" "mixed"
0
"concept" "sys" "attrs" "common" "var"
psym
"id_MIXED"
end ind


ind attr
"concept" "sys" "mean" "slot" "mixed"
0
"concept" "sys" "attrs" "common" "var"
psym
"id_mslot_MIXED"
end ind


ind attr
"concept" "sys" "dict"
0
"concept" "sys" "attrs" "common" "var"
psym
"sys_DICT"
end ind


ind attr
"concept" "sys" "dict" "a"
0
"concept" "sys" "attrs" "common" "var"
psym
"dict_ALPHA"
end ind


ind attr
"concept" "sys" "dict" "unknown"
0
"concept" "sys" "attrs" "common" "var"
psym
"dict_UNKNOWN"
end ind


quit

Loading
Loading