示例#1
0
int TreeScorerResult::Calculate( Dictionary & dict, const TreeScorerBoundVariables & bound_variables ) const
{
 if( type==0 )
  return score;
 else
  {
   lem::MCollect<const Solarix::Word_Form*> vars;
   for( lem::Container::size_type i=0; i<args.size(); ++i )
   {
    const Solarix::Word_Form * wf = bound_variables.GetVariable( args[i] );
    if( wf==NULL )
     {
      lem::MemFormatter mem;
      mem.printf( "tree_scorer: can not find bound variable %us", args[i].c_str() );
      throw lem::E_BaseException( mem.string() );
     }

    vars.push_back(wf);
   }

   KB_CheckingResult res = dict.GetLexAuto().GetKnowledgeBase().Prove( id_fact, vars );
   if( res.IsMatched() )
    return res.GetInt();
   else
    return 0;
  }
}
示例#2
0
int TreeScorerResult::Calculate(
                                Dictionary & dict,
                                const TreeScorerBoundVariables & bound_variables,
                                const ElapsedTimeConstraint & constraints,
                                TrTrace *trace_log
                               ) const
{
 if( type==NumberScoreType )
  return score;
 else if( type==NGramScoreType )
  {
   lem::MCollect<const Solarix::Word_Form*> vars;
   for( lem::Container::size_type i=0; i<args.size(); ++i )
   {
    const Solarix::Word_Form * wf = bound_variables.GetVariable( args[i] );
    if( wf==NULL )
     {
      lem::MemFormatter mem;
      mem.printf( "tree_scorer: can not find bound variable %us", args[i].c_str() );
      throw lem::E_BaseException( mem.string() );
     }

    vars.push_back(wf);
   }

   KB_CheckingResult res = dict.GetLexAuto().GetKnowledgeBase().Prove( id_fact, vars );
   if( res.IsMatched() )
    return res.GetInt();
   else
    return 0;
  }
 else if( type==FuncScoreType )
  {
   TrFunContext ctx0( (TrFunContext*)NULL );

   TrContextInvokation ctx2( &ctx0 );

   for( lem::Container::size_type i=0; i<args.size(); ++i )
   {
    const Solarix::Word_Form * wf = bound_variables.GetVariable( args[i] );
    if( wf==NULL )
     {
      lem::MemFormatter mem;
      mem.printf( "tree_scorer: can not find bound variable %us", args[i].c_str() );
      throw lem::E_BaseException( mem.string() );
     }

    lem::Ptr<TrValue> this_wordform( new TrValue( new Tree_Node(*wf), true ) );
    ctx2.AddVar( args[i], this_wordform );
   }

   lem::Ptr<TrValue> fun_res = score_fun->Run( constraints, dict.GetLexAuto(), ctx2, trace_log );

   if( fun_res->GetType().IsInt() )
    {
     int res_int = fun_res->GetInt();
     return res_int;
    }
   else
    {
     #if LEM_DEBUGGING==1
     if( trace_log!=NULL )
      trace_log->PrintStack(*lem::mout);
     #endif

     lem::UFString msg( lem::format_str( L"Score function must return int, not %s", fun_res->GetType().GetName().c_str() ) );
     throw E_BaseException(msg.c_str());
    }
  }
 else
  {
   LEM_STOPIT;
  }

 return 0;
}