<commit_before>/*
 * 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.
 */

#include <cstddef>
#include <fstream>
#include <math.h>
#include <unistd.h>
#include <sstream>
#include <iostream>
#include <vector>
#include <list>

#include "AnsHeap.h"
#include "AnswerTree.h"
#include "Bchart.h"
#include "Bst.h"
#include "ChartBase.h"
#include "ECArgs.h"
#include "ECString.h"
#include "ewDciTokStrm.h"
#include "extraMain.h"
#include "GotIter.h"
#include "InputTree.h"
#include "Link.h"
#include "MeChart.h"
#include "Params.h"
#include "ParseStats.h"
#include "ScoreTree.h"
#include "SentRep.h"
#include "TimeIt.h"
#include "UnitRules.h"
#include "utils.h"
#include "Wrd.h"

int sentenceCount;
static const double log600 = log2(600.0);
typedef pair<double,InputTree*> ScoredTree;

class ParserError {
    public:
        const char* description;

        ParserError(string msg) {
            this->description = msg.c_str();
        }

        ParserError(const char *filename, int filelinenum, const char *msg) {
            stringstream description;
            description << "[";
            description << filename;
            description << ":";
            description << filelinenum;
            description << "]: ";
            description << msg;

            this->description = description.str().c_str();
        }
};

class LabeledSpan {
    public:
        int start;
        int end;
        int termIndex;

        LabeledSpan() : start(-1), end(-1), termIndex(-1) {}
        LabeledSpan(int startIndex, int endIndex, string termName)
            : start(startIndex), end(endIndex) {
                assert (startIndex < endIndex);
                termIndex = Term::get(termName)->toInt();
        }
        int size() {
            return end - start;
        }
        bool disrupts(int otherStart, int otherEnd) {
            assert (otherStart < otherEnd);
            if (start == otherStart) {
                // special case -- they only intersect if end == otherEnd
                return end == otherEnd;
            }

            // canonicalize order so that A is at least as early as B
            int startB, endA, endB;
            if (start < otherStart) {
                endA = end;
                startB = otherStart;
                endB = otherEnd;
            } else { // start > otherStart
                endA = otherEnd;
                startB = start;
                endB = end;
            }
            /* now it's something like this:
                    [startA ----- endA)
                        [startB -?????- endB)
               where endB could be anywhere after startB and startA might
               be the same as startB. */

            // see if Bs start is within A (startB must be > startA)
            if (startB < endA) {
                // fine as long as B also ends within A
                if (endB <= endA) {
                    return false;
                } else {
                    return true;
                }
            }

            return false;
        }
        bool operator==(const LabeledSpan& other) const {
            return (start == other.start) &&
                (end == other.end) &&
                (termIndex == other.termIndex);
        }
        bool operator!=(const LabeledSpan& other) const {
            return !(*this == other);
        }
        bool operator<(const LabeledSpan& other) const {
            if (start < other.start) {
                return true;
            } else if (start == other.start) {
                if (end < other.end) {
                    return true;
                } else if (end == other.end) {
                    return (termIndex < other.termIndex);
                }
            }
            return false;
        }
        friend ostream& operator<<(ostream& os, const LabeledSpan& span) {
            os << "LabeledSpan(" << span.start << ", " << span.end << ", "
               << span.termIndex << ")";
            return os;
        }
};

class SpanConstraints: public vector<LabeledSpan> {
    public:
        bool decodeOnly;
        int minSizeForParsing;
        bool sorted;
        SpanConstraints() {
            decodeOnly = false;
            sorted = false;
            minSizeForParsing = 0;
        }
        void addConstraint(int start, int end, string term) {
            LabeledSpan sp(start, end, term);
            this->push_back(sp);<commit_msg>first-stage/PARSE/SimpleAPI.C: Remove old decodeOnly option This option was used in a previous (uncommitted) implementation of parser constraints.<commit_after>/*
 * 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.
 */

#include <cstddef>
#include <fstream>
#include <math.h>
#include <unistd.h>
#include <sstream>
#include <iostream>
#include <vector>
#include <list>

#include "AnsHeap.h"
#include "AnswerTree.h"
#include "Bchart.h"
#include "Bst.h"
#include "ChartBase.h"
#include "ECArgs.h"
#include "ECString.h"
#include "ewDciTokStrm.h"
#include "extraMain.h"
#include "GotIter.h"
#include "InputTree.h"
#include "Link.h"
#include "MeChart.h"
#include "Params.h"
#include "ParseStats.h"
#include "ScoreTree.h"
#include "SentRep.h"
#include "TimeIt.h"
#include "UnitRules.h"
#include "utils.h"
#include "Wrd.h"

int sentenceCount;
static const double log600 = log2(600.0);
typedef pair<double,InputTree*> ScoredTree;

class ParserError {
    public:
        const char* description;

        ParserError(string msg) {
            this->description = msg.c_str();
        }

        ParserError(const char *filename, int filelinenum, const char *msg) {
            stringstream description;
            description << "[";
            description << filename;
            description << ":";
            description << filelinenum;
            description << "]: ";
            description << msg;

            this->description = description.str().c_str();
        }
};

class LabeledSpan {
    public:
        int start;
        int end;
        int termIndex;

        LabeledSpan() : start(-1), end(-1), termIndex(-1) {}
        LabeledSpan(int startIndex, int endIndex, string termName)
            : start(startIndex), end(endIndex) {
                assert (startIndex < endIndex);
                termIndex = Term::get(termName)->toInt();
        }
        int size() {
            return end - start;
        }
        bool disrupts(int otherStart, int otherEnd) {
            assert (otherStart < otherEnd);
            if (start == otherStart) {
                // special case -- they only intersect if end == otherEnd
                return end == otherEnd;
            }

            // canonicalize order so that A is at least as early as B
            int startB, endA, endB;
            if (start < otherStart) {
                endA = end;
                startB = otherStart;
                endB = otherEnd;
            } else { // start > otherStart
                endA = otherEnd;
                startB = start;
                endB = end;
            }
            /* now it's something like this:
                    [startA ----- endA)
                        [startB -?????- endB)
               where endB could be anywhere after startB and startA might
               be the same as startB. */

            // see if Bs start is within A (startB must be > startA)
            if (startB < endA) {
                // fine as long as B also ends within A
                if (endB <= endA) {
                    return false;
                } else {
                    return true;
                }
            }

            return false;
        }
        bool operator==(const LabeledSpan& other) const {
            return (start == other.start) &&
                (end == other.end) &&
                (termIndex == other.termIndex);
        }
        bool operator!=(const LabeledSpan& other) const {
            return !(*this == other);
        }
        bool operator<(const LabeledSpan& other) const {
            if (start < other.start) {
                return true;
            } else if (start == other.start) {
                if (end < other.end) {
                    return true;
                } else if (end == other.end) {
                    return (termIndex < other.termIndex);
                }
            }
            return false;
        }
        friend ostream& operator<<(ostream& os, const LabeledSpan& span) {
            os << "LabeledSpan(" << span.start << ", " << span.end << ", "
               << span.termIndex << ")";
            return os;
        }
};

class SpanConstraints: public vector<LabeledSpan> {
    public:
        int minSizeForParsing;
        bool sorted;
        SpanConstraints() {
            sorted = false;
            minSizeForParsing = 0;
        }
        void addConstraint(int start, int end, string term) {
            LabeledSpan sp(start, end, term);
            this->push_back(sp);
{'fraction_non_alphanumeric': 0.10820895522388059, 'fraction_numerical': 0.003227107704719645, 'mean_word_length': 1.7103033615742007, 'pattern_counts': {'":': 2, '<': 97, '<?xml version=': 0, '>': 35, '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': '4095751', 'n_tokens_mistral': 2760, 'n_tokens_neox': 2669, 'n_words': 839}