<commit_before>//
// Copyleft 2011 RIME Developers
// License: GPLv3
//
// 2011-06-19 <NAME> <<EMAIL>>
//
#include <boost/foreach.hpp>
#include <rime/candidate.h>
#include <rime/composition.h>
#include <rime/menu.h>

namespace rime {

Composition::Composition() {
}

bool Composition::HasFinishedComposition() const {
  if (empty()) return false;
  size_t k = size() - 1;
  if (k > 0 && at(k).start == at(k).end)
    --k;
  return at(k).status >= Segment::kSelected;
}

void Composition::GetPreedit(Preedit *preedit) const {
  if (!preedit)
    return;
  preedit->text.clear();
  preedit->sel_start = preedit->sel_end = 0;
  if (empty())
    return;
  size_t text_len = 0;
  size_t start = 0;
  size_t end = 0;
  for (size_t i = 0; i < size(); ++i) {
    start = end;
    shared_ptr<Candidate> cand(at(i).GetSelectedCandidate());
    if (i < size() - 1) {  // converted
      if (cand) {
        end = cand->end();
        preedit->text += cand->text();
        text_len = preedit->text.length();
      }
      else {  // raw input
        end = at(i).end;
        preedit->text += input_.substr(start, end - start);
        text_len = preedit->text.length();
      }
    }
    else {  // highlighted
      preedit->sel_start = text_len;
      if (cand && !cand->preedit().empty()) {
        end = cand->end();
        preedit->text += cand->preedit();
      }
      else {
        end = at(i).end;
        preedit->text += input_.substr(start, end - start);
      }
      if (!at(i).prompt.empty()) {  // show prompt
        preedit->text += at(i).prompt;
      }
      text_len = preedit->text.length();
      preedit->sel_end = text_len;
    }
  }
  if (input_.length() > end) {
    preedit->text += input_.substr(end);
  }
}

std::string Composition::GetCommitText() const {
  std::string result;
  size_t end = 0;
  BOOST_FOREACH(const Segment &seg, *this) {
    shared_ptr<Candidate> cand(seg.GetSelectedCandidate());
    if (cand) {
      end = cand->end();
      result += cand->text();
    }
    else {
      end = seg.end;
      result += input_.substr(seg.start, seg.end - seg.start);
    }
  }
  if (input_.length() > end) {
    result += input_.substr(end);
  }
  return result;
}

std::string Composition::GetScriptText() const {
  std::string result;
  size_t start = 0;
  size_t end = 0;
  BOOST_FOREACH(const Segment &seg, *this) {
    shared_ptr<Candidate> cand(seg.GetSelectedCandidate());
    start = end;
    end = cand ? cand->end() : seg.end;
    if (cand && !cand->preedit().empty())
      result += cand->preedit();
    else
      result += input_.substr(start, end - start);
  }
  if (input_.length() > end) {
    result += input_.substr(end);
  }
  return result;
}

std::string Composition::GetDebugText() const {
  std::string result;
  int i = 0;
  BOOST_FOREACH(const Segment &seg, *this) {
    if (i++ > 0)
      result += "|";
    shared_ptr<Candidate> cand(seg.GetSelectedCandidate());
    if (cand)
      result += cand->text();
    else
      result += input_.substr(seg.start, seg.end - seg.start);
  }
  return result;
}

}  // namespace rime
<commit_msg>composition: phony segments commit nothing.<commit_after>//
// Copyleft 2011 RIME Developers
// License: GPLv3
//
// 2011-06-19 <NAME> <<EMAIL>>
//
#include <boost/foreach.hpp>
#include <rime/candidate.h>
#include <rime/composition.h>
#include <rime/menu.h>

namespace rime {

Composition::Composition() {
}

bool Composition::HasFinishedComposition() const {
  if (empty()) return false;
  size_t k = size() - 1;
  if (k > 0 && at(k).start == at(k).end)
    --k;
  return at(k).status >= Segment::kSelected;
}

void Composition::GetPreedit(Preedit *preedit) const {
  if (!preedit)
    return;
  preedit->text.clear();
  preedit->sel_start = preedit->sel_end = 0;
  if (empty())
    return;
  size_t text_len = 0;
  size_t start = 0;
  size_t end = 0;
  for (size_t i = 0; i < size(); ++i) {
    start = end;
    shared_ptr<Candidate> cand(at(i).GetSelectedCandidate());
    if (i < size() - 1) {  // converted
      if (cand) {
        end = cand->end();
        preedit->text += cand->text();
        text_len = preedit->text.length();
      }
      else {  // raw input
        end = at(i).end;
        if (!at(i).HasTag("phony")) {
          preedit->text += input_.substr(start, end - start);
          text_len = preedit->text.length();
        }
      }
    }
    else {  // highlighted
      preedit->sel_start = text_len;
      if (cand && !cand->preedit().empty()) {
        end = cand->end();
        preedit->text += cand->preedit();
      }
      else {
        end = at(i).end;
        preedit->text += input_.substr(start, end - start);
      }
      if (!at(i).prompt.empty()) {  // show prompt
        preedit->text += at(i).prompt;
      }
      text_len = preedit->text.length();
      preedit->sel_end = text_len;
    }
  }
  if (input_.length() > end) {
    preedit->text += input_.substr(end);
  }
}

std::string Composition::GetCommitText() const {
  std::string result;
  size_t end = 0;
  BOOST_FOREACH(const Segment &seg, *this) {
    shared_ptr<Candidate> cand(seg.GetSelectedCandidate());
    if (cand) {
      end = cand->end();
      result += cand->text();
    }
    else {
      end = seg.end;
      if (!seg.HasTag("phony")) {
        result += input_.substr(seg.start, seg.end - seg.start);
      }
    }
  }
  if (input_.length() > end) {
    result += input_.substr(end);
  }
  return result;
}

std::string Composition::GetScriptText() const {
  std::string result;
  size_t start = 0;
  size_t end = 0;
  BOOST_FOREACH(const Segment &seg, *this) {
    shared_ptr<Candidate> cand(seg.GetSelectedCandidate());
    start = end;
    end = cand ? cand->end() : seg.end;
    if (cand && !cand->preedit().empty())
      result += cand->preedit();
    else
      result += input_.substr(start, end - start);
  }
  if (input_.length() > end) {
    result += input_.substr(end);
  }
  return result;
}

std::string Composition::GetDebugText() const {
  std::string result;
  int i = 0;
  BOOST_FOREACH(const Segment &seg, *this) {
    if (i++ > 0)
      result += "|";
    shared_ptr<Candidate> cand(seg.GetSelectedCandidate());
    if (cand)
      result += cand->text();
    else
      result += input_.substr(seg.start, seg.end - seg.start);
  }
  return result;
}

}  // namespace rime

{'fraction_non_alphanumeric': 0.17363751584283904, 'fraction_numerical': 0.008238276299112801, 'mean_word_length': 2.546629213483146, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7513971', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2224, 'n_words': 482}