Skip to content

Commit

Permalink
Improve Miner API
Browse files Browse the repository at this point in the history
Rename XPMParameters to MinerParameters and overload operator to
pattern miner either AtomSpace or provided text collection.
  • Loading branch information
ngeiswei committed Jul 17, 2018
1 parent 95b0df9 commit 89a989e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
35 changes: 21 additions & 14 deletions opencog/learning/miner/Miner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ namespace opencog
// TODO:
// 7. make sure that filtering is still meaningfull

XPMParameters::XPMParameters(unsigned ms, unsigned iconjuncts,
const Handle& ipat, int maxd, double io)
MinerParameters::MinerParameters(unsigned ms, unsigned iconjuncts,
const Handle& ipat, int maxd,
double io)
: minsup(ms), initconjuncts(iconjuncts), initpat(ipat),
maxdepth(maxd), info(io)
{
Expand Down Expand Up @@ -79,14 +80,19 @@ XPMParameters::XPMParameters(unsigned ms, unsigned iconjuncts,
initconjuncts = Miner::conjuncts(initpat);
}

Miner::Miner(AtomSpace& as, const XPMParameters& prm)
: text_as(as), param(prm) {}
Miner::Miner(const MinerParameters& prm)
: param(prm) {}

HandleTree Miner::operator()()
HandleTree Miner::operator()(const AtomSpace& texts_as)
{
HandleSet texts;
text_as.get_handles_by_type(std::inserter(texts, texts.end()),
opencog::ATOM, true);
texts_as.get_handles_by_type(std::inserter(texts, texts.end()),
opencog::ATOM, true);
return operator()(texts);
}

HandleTree Miner::operator()(const HandleSet& texts)
{
// If the initial pattern is specialized, this initial filtering
// may save some computation
HandleSet fltexts = filter_texts(param.initpat, texts);
Expand All @@ -97,6 +103,7 @@ HandleTree Miner::specialize(const Handle& pattern,
const HandleSet& texts,
int maxdepth)
{
// TODO: decide what to choose and remove or comment
// return specialize_alt(pattern, texts, Valuations(pattern, texts), maxdepth);
return specialize(pattern, texts, Valuations(pattern, texts), maxdepth);
}
Expand Down Expand Up @@ -651,25 +658,25 @@ Handle Miner::restricted_satisfying_set(const Handle& pattern,
const HandleSet& texts,
int maxf)
{
static AtomSpace tmp_text_as;
tmp_text_as.clear();
static AtomSpace tmp_texts_as;
tmp_texts_as.clear();
HandleSeq tmp_texts;
for (const auto& text : texts)
tmp_texts.push_back(tmp_text_as.add_atom(text));
tmp_texts.push_back(tmp_texts_as.add_atom(text));

// Avoid pattern matcher warning
// TODO: support 1 < conjuncts
if (totally_abstract(pattern) and conjuncts(pattern) == 1)
return tmp_text_as.add_link(SET_LINK, tmp_texts);
return tmp_texts_as.add_link(SET_LINK, tmp_texts);

// Run the pattern matcher
AtomSpace tmp_query_as(&tmp_text_as);
AtomSpace tmp_query_as(&tmp_texts_as);
Handle tmp_pattern = tmp_query_as.add_atom(pattern),
vardecl = get_vardecl(tmp_pattern),
body = get_body(tmp_pattern),
gl = tmp_query_as.add_link(GET_LINK, vardecl, body),
results = (maxf < 0 ? satisfying_set(&tmp_text_as, gl)
: satisfying_set(&tmp_text_as, gl, maxf));
results = (maxf < 0 ? satisfying_set(&tmp_texts_as, gl)
: satisfying_set(&tmp_texts_as, gl, maxf));
return results;
}

Expand Down
43 changes: 22 additions & 21 deletions opencog/learning/miner/Miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ namespace opencog
* Frequent Subtree Mining -- An Overview, from Yun Chi et al, when
* possible.
*/
struct XPMParameters {
struct MinerParameters {

/**
* CTor. Note that conjuncts will be overwritten by initpat, if
* provided.
*/
XPMParameters(unsigned minsup=1,
unsigned conjuncts=1,
const Handle& initpat=Handle::UNDEFINED,
int maxdepth=-1,
double info=1.0);
MinerParameters(unsigned minsup=1,
unsigned conjuncts=1,
const Handle& initpat=Handle::UNDEFINED,
int maxdepth=-1,
double info=1.0);

// Minimum support. Mined patterns must have a frequency equal or
// above this value.
Expand Down Expand Up @@ -114,15 +114,20 @@ class Miner
/**
* CTor
*/
Miner(AtomSpace& as, const XPMParameters& param=XPMParameters());
Miner(const MinerParameters& param=MinerParameters());

/**
* Mine and return a tree of patterns linked by specialization
* relationship (children are specializations of parent) with
* frequency equal to or above minsup, starting from the initial
* pattern, excluded.
* Mine the given AtomSpace and return a tree of patterns linked by
* specialization relationship (children are specializations of
* parent) with frequency equal to or above minsup, starting from
* the initial pattern, excluded.
*/
HandleTree operator()();
HandleTree operator()(const AtomSpace& texts_as);

/**
* Like above but only mine amongst the provided text collection.
*/
HandleTree operator()(const HandleSet& texts);

/**
* Specialization. Given a pattern and a collection to text atoms,
Expand All @@ -148,14 +153,8 @@ class Miner
const Valuations& valuations,
int maxdepth);

// AtomSpace containing the text trees to mine.
AtomSpace& text_as;

// Parameters
XPMParameters param;

// Working atomspace containing the patterns, and other junk.
AtomSpace pattern_as;
MinerParameters param;

private:

Expand Down Expand Up @@ -216,8 +215,10 @@ class Miner
* at the given variable, then call Miner::specialize on the
* obtained specialization.
*/
HandleTree specialize_shapat(const Handle& pattern, const HandleSet texts,
const Handle& var, const Handle& shapat,
HandleTree specialize_shapat(const Handle& pattern,
const HandleSet texts,
const Handle& var,
const Handle& shapat,
int maxdepth);

/**
Expand Down

0 comments on commit 89a989e

Please sign in to comment.