<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * Licensed 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.
 */

#define DEBUG_THESAURUS 0
#define DEBUG_READ_THESAURUS 0

#include <fstream>

#include <zorba/store_consts.h>

#include "store/api/iterator.h"
#include "store/api/store.h"
#include "util/fs_util.h"
#if DEBUG_THESAURUS || DEBUG_READ_THESAURUS
#include "util/oseparator.h"
#endif
#include "util/stl_util.h"
#include "util/uri_util.h"
#include "util/utf8_util.h"
#include "zorbaerrors/error_manager.h"

#include "xqftts_thesaurus.h"

using namespace std;
using namespace zorba::locale;

namespace zorba {
namespace xqftts {

///////////////////////////////////////////////////////////////////////////////

inline zstring const& get_local_name( store::Item_t const &item ) {
  return item->getNodeName()->getLocalName();
}

inline bool is_element( store::Item_t const &item ) {
  return  item->isNode() &&
          item->getNodeKind() == store::StoreConsts::elementNode;
}

///////////////////////////////////////////////////////////////////////////////

thesaurus::candidate_queue_t::value_type const thesaurus::LevelMarker =
  make_pair( static_cast<synonym*>(0), iso2788::neutral );

thesaurus::thesaurus( zstring const &path, iso639_1::type lang,
                      zstring const &phrase, zstring const &query_rel_string,
                      ft_int at_least, ft_int at_most ) :
  at_least_( at_least ), at_most_( at_most ), level_( 0 )
{
  read_xqftts_file( path );
# if DEBUG_THESAURUS
  cout << "==================================================" << endl;
  cout << "query phrase: " << phrase << endl;
# endif
  //
  // Always include the original phrase in the results.
  //
  result_queue_.push_back( phrase );
  synonyms_seen_.insert( phrase );

  //
  // Lookup the phrase in the thesaurus.
  //
  thesaurus_t::const_iterator const entry = thesaurus_.find( phrase );
  if ( entry != thesaurus_.end() ) {
    relationship const query_rel( query_rel_string );
    iso2788::rel_dir const query_dir =
      iso2788::get_dir( query_rel.get_iso2788() );
    FOR_EACH( synonym_set_t, i, entry->second ) {
      synonym const *const candidate_ptr = *i;
      //
      // For each synonym: if either the query didn't specify a relationship or
      // it did and a candidate's relationship matches, add the synonym as a
      // candidate result.
      //
      FOR_EACH( relationship_set_t, r, candidate_ptr->relationships ) {
        if ( query_rel.empty() || *r == query_rel ) {
          candidate_queue_.push_back(
            make_pair(
              candidate_ptr, query_dir + iso2788::get_dir( r->get_iso2788() )
            )
          );
        }
      }
    }
    //
    // All the candidates just added constitute a "level" so add the
    // LevelMarker to the queue.
    //
    candidate_queue_.push_back( LevelMarker );
  }
}

thesaurus::~thesaurus() {
  MUTATE_EACH( thesaurus_t, entry, thesaurus_ )
    delete_ptr_seq( entry->second );
}

bool thesaurus::next( zstring *result ) {
  while ( result_queue_.empty() ) {
#   if DEBUG_THESAURUS
    cout << "--------------------------------------------------" << endl;
    cout << "result_queue is empty" << endl;
#   endif

    if ( candidate_queue_.empty() ) {
#     if DEBUG_THESAURUS
      cout << "candidate_queue is empty --> no more synonyms" << endl;
#     endif
      return false;
    }

#   if DEBUG_THESAURUS
    oseparator comma;
    cout << "candidate_queue is: ";
    FOR_EACH( candidate_queue_t, i, candidate_queue_ )
      cout << comma << (i->first ? i->first->term.c_str() : "<LEVEL>");
    cout << endl;
#endif

    candidate_t const candidate( pop_front( candidate_queue_ ) );
    synonym const *const candidate_ptr = candidate.first;
    iso2788::rel_dir const candidate_dir = candidate.second;

    if ( candidate_ptr == LevelMarker.first ) {
#     if DEBUG_THESAURUS
      cout << "+ found LevelMarker" << endl;
#     endif
      if ( ++level_ > at_most_ ) {<commit_msg>Cosmetic fix.<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * Licensed 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.
 */

#define DEBUG_THESAURUS 0
#define DEBUG_READ_THESAURUS 0

#include <fstream>

#include <zorba/store_consts.h>

#include "store/api/iterator.h"
#include "store/api/store.h"
#include "util/fs_util.h"
#if DEBUG_THESAURUS || DEBUG_READ_THESAURUS
#include "util/oseparator.h"
#endif
#include "util/stl_util.h"
#include "util/uri_util.h"
#include "util/utf8_util.h"
#include "zorbaerrors/error_manager.h"

#include "xqftts_thesaurus.h"

using namespace std;
using namespace zorba::locale;

namespace zorba {
namespace xqftts {

///////////////////////////////////////////////////////////////////////////////

inline zstring const& get_local_name( store::Item_t const &item ) {
  return item->getNodeName()->getLocalName();
}

inline bool is_element( store::Item_t const &item ) {
  return  item->isNode() &&
          item->getNodeKind() == store::StoreConsts::elementNode;
}

///////////////////////////////////////////////////////////////////////////////

thesaurus::candidate_queue_t::value_type const thesaurus::LevelMarker =
  make_pair( static_cast<synonym*>(0), iso2788::neutral );

thesaurus::thesaurus( zstring const &path, iso639_1::type lang,
                      zstring const &phrase, zstring const &query_rel_string,
                      ft_int at_least, ft_int at_most ) :
  at_least_( at_least ), at_most_( at_most ), level_( 0 )
{
  read_xqftts_file( path );
# if DEBUG_THESAURUS
  cout << "==================================================" << endl;
  cout << "query phrase: " << phrase << endl;
# endif
  //
  // Always include the original phrase in the results.
  //
  result_queue_.push_back( phrase );
  synonyms_seen_.insert( phrase );

  //
  // Lookup the phrase in the thesaurus.
  //
  thesaurus_t::const_iterator const entry = thesaurus_.find( phrase );
  if ( entry != thesaurus_.end() ) {
    relationship const query_rel( query_rel_string );
    iso2788::rel_dir const query_dir =
      iso2788::get_dir( query_rel.get_iso2788() );
    FOR_EACH( synonym_set_t, i, entry->second ) {
      synonym const *const candidate_ptr = *i;
      //
      // For each synonym: if either the query didn't specify a relationship or
      // it did and a candidate's relationship matches, add the synonym as a
      // candidate result.
      //
      FOR_EACH( relationship_set_t, r, candidate_ptr->relationships ) {
        if ( query_rel.empty() || *r == query_rel ) {
          candidate_queue_.push_back(
            make_pair(
              candidate_ptr, query_dir + iso2788::get_dir( r->get_iso2788() )
            )
          );
        }
      }
    }
    //
    // All the candidates just added constitute a "level" so add the
    // LevelMarker to the queue.
    //
    candidate_queue_.push_back( LevelMarker );
  }
}

thesaurus::~thesaurus() {
  MUTATE_EACH( thesaurus_t, entry, thesaurus_ )
    delete_ptr_seq( entry->second );
}

bool thesaurus::next( zstring *result ) {
  while ( result_queue_.empty() ) {
#   if DEBUG_THESAURUS
    cout << "--------------------------------------------------" << endl;
    cout << "result_queue is empty" << endl;
#   endif

    if ( candidate_queue_.empty() ) {
#     if DEBUG_THESAURUS
      cout << "candidate_queue is empty --> no more synonyms" << endl;
#     endif
      return false;
    }

#   if DEBUG_THESAURUS
    oseparator comma;
    cout << "candidate_queue is: ";
    FOR_EACH( candidate_queue_t, i, candidate_queue_ )
      cout << comma << (i->first ? i->first->term.c_str() : "<LEVEL>");
    cout << endl;
#   endif

    candidate_t const candidate( pop_front( candidate_queue_ ) );
    synonym const *const candidate_ptr = candidate.first;
    iso2788::rel_dir const candidate_dir = candidate.second;

    if ( candidate_ptr == LevelMarker.first ) {
#     if DEBUG_THESAURUS
      cout << "+ found LevelMarker" << endl;
#     endif
      if ( ++level_ > at_most_ ) {
{'fraction_non_alphanumeric': 0.16594227125821912, 'fraction_numerical': 0.01092165385044021, 'mean_word_length': 2.6778688524590164, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6810548', 'n_tokens_mistral': 2862, 'n_tokens_neox': 2774, 'n_words': 741}