technician1 commited on
Commit
868bca5
·
1 Parent(s): 45f18d3

Upload 2 files

Browse files
Files changed (1) hide show
  1. ChatIPC.cpp +60 -2
ChatIPC.cpp CHANGED
@@ -308,7 +308,7 @@ static std::vector<std::string> tokenize_whitespace(const std::string &s){
308
  return out;
309
  }
310
 
311
- static std::vector<std::string> tokenize_dictionary_expansion(const std::string &s) {
312
  std::vector<std::string> out;
313
  std::string cur;
314
 
@@ -445,7 +445,7 @@ static void build_def_tokens_cache(){
445
 
446
  auto &defs = global_def_tokens_cache[key];
447
  for (const auto &def : entry.definitions){
448
- auto toks = tokenize_dictionary_expansion(def);
449
  defs.insert(defs.end(), toks.begin(), toks.end());
450
  }
451
  }
@@ -769,6 +769,9 @@ static std::string best_candidate_by_similarity(
769
  std::vector<std::uint64_t> agg_words(words, 0ULL);
770
 
771
  auto add_token_and_defs = [&](StrPtr t){
 
 
 
772
  TokenId tid = interner.id_of(t);
773
  if (tid != TOKEN_ID_INVALID) bitset_set(agg_words.data(), words, tid);
774
 
@@ -779,6 +782,32 @@ static std::string best_candidate_by_similarity(
779
  if (did != TOKEN_ID_INVALID) bitset_set(agg_words.data(), words, did);
780
  }
781
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
782
  };
783
 
784
  for (StrPtr t : prompt_ptrs) add_token_and_defs(t);
@@ -794,9 +823,11 @@ static std::string best_candidate_by_similarity(
794
  std::uint64_t *row = cand_words.data() + i * words;
795
  const StrPtr cand = cands[i];
796
 
 
797
  TokenId cid = interner.id_of(cand);
798
  if (cid != TOKEN_ID_INVALID) bitset_set(row, words, cid);
799
 
 
800
  auto it = def_index.find(cand);
801
  if (it != def_index.end()){
802
  for (StrPtr d : it->second){
@@ -805,6 +836,33 @@ static std::string best_candidate_by_similarity(
805
  }
806
  }
807
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808
  cand_counts[i] = bitset_count(row, words);
809
  }
810
 
 
308
  return out;
309
  }
310
 
311
+ static std::vector<std::string> tokenize_other(const std::string &s) {
312
  std::vector<std::string> out;
313
  std::string cur;
314
 
 
445
 
446
  auto &defs = global_def_tokens_cache[key];
447
  for (const auto &def : entry.definitions){
448
+ auto toks = tokenize_other(def);
449
  defs.insert(defs.end(), toks.begin(), toks.end());
450
  }
451
  }
 
769
  std::vector<std::uint64_t> agg_words(words, 0ULL);
770
 
771
  auto add_token_and_defs = [&](StrPtr t){
772
+ if (!t) return;
773
+
774
+ // 1. Process the main token and its direct definitions
775
  TokenId tid = interner.id_of(t);
776
  if (tid != TOKEN_ID_INVALID) bitset_set(agg_words.data(), words, tid);
777
 
 
782
  if (did != TOKEN_ID_INVALID) bitset_set(agg_words.data(), words, did);
783
  }
784
  }
785
+
786
+ // 2. Tokenize the expanded dictionary string to process sub-tokens
787
+ std::vector<std::string> sub_tokens = tokenize_other(*t);
788
+ for (const std::string& sub_tok : sub_tokens) {
789
+
790
+ // Use id_of to check if the sub-token exists without adding a new string to the pool
791
+ TokenId sub_tid = interner.id_of(sub_tok);
792
+
793
+ if (sub_tid != TOKEN_ID_INVALID) {
794
+ // Add the sub-token itself to the bitset
795
+ bitset_set(agg_words.data(), words, sub_tid);
796
+
797
+ // Retrieve the interned pointer so we can look it up in def_index
798
+ StrPtr sub_ptr = interner.ptr_from_id(sub_tid);
799
+ if (sub_ptr) {
800
+ auto sub_it = def_index.find(sub_ptr);
801
+ if (sub_it != def_index.end()) {
802
+ // Add the sub-token's definitions to the bitset
803
+ for (StrPtr d : sub_it->second) {
804
+ TokenId did = interner.id_of(d);
805
+ if (did != TOKEN_ID_INVALID) bitset_set(agg_words.data(), words, did);
806
+ }
807
+ }
808
+ }
809
+ }
810
+ }
811
  };
812
 
813
  for (StrPtr t : prompt_ptrs) add_token_and_defs(t);
 
823
  std::uint64_t *row = cand_words.data() + i * words;
824
  const StrPtr cand = cands[i];
825
 
826
+ // 1. Tag the candidate word itself
827
  TokenId cid = interner.id_of(cand);
828
  if (cid != TOKEN_ID_INVALID) bitset_set(row, words, cid);
829
 
830
+ // 2. Expand the candidate's direct definition
831
  auto it = def_index.find(cand);
832
  if (it != def_index.end()){
833
  for (StrPtr d : it->second){
 
836
  }
837
  }
838
 
839
+ // 3. NEW: Tokenize the candidate to process sub-tokens (e.g., camelCase, hyphens)
840
+ std::vector<std::string> sub_tokens = tokenize_other(*cand);
841
+ for (const std::string& sub_tok : sub_tokens) {
842
+
843
+ // Check if the sub-token exists in our knowledge base
844
+ TokenId sub_tid = interner.id_of(sub_tok);
845
+
846
+ if (sub_tid != TOKEN_ID_INVALID) {
847
+ // Add the sub-token itself to the candidate's semantic fingerprint (row)
848
+ bitset_set(row, words, sub_tid);
849
+
850
+ // Convert the ID back to a pointer so we can check the dictionary
851
+ StrPtr sub_ptr = interner.ptr_from_id(sub_tid);
852
+ if (sub_ptr) {
853
+ auto sub_it = def_index.find(sub_ptr);
854
+ if (sub_it != def_index.end()) {
855
+ // Add the sub-token's deeper definitions to the candidate's row
856
+ for (StrPtr d : sub_it->second) {
857
+ TokenId did = interner.id_of(d);
858
+ if (did != TOKEN_ID_INVALID) bitset_set(row, words, did);
859
+ }
860
+ }
861
+ }
862
+ }
863
+ }
864
+
865
+ // 4. Finally, count the bits for the Jaccard similarity math
866
  cand_counts[i] = bitset_count(row, words);
867
  }
868