<commit_before>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor:
// - <NAME> <EMAIL>

package scribe

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

// Load a scribe JSON or YAML document from the reader specified by r.
// Returns a Document type that can be passed to AnalyzeDocument().
// On error, LoadDocument() returns the error that occurred.
func LoadDocument(r io.Reader) (Document, error) {
	var ret Document

	debugPrint("loading new document\n")
	b, err := ioutil.ReadAll(r)
	if err != nil {
		return ret, err
	}
	// clean up leading spaces, tabs and newlines
	b = bytes.TrimLeft(b, " \n\t")
	if len(b) < 10 {
		return ret, fmt.Errorf("the document is too small to be valid (%d bytes)", len(b))
	}
	switch b[0] {
	case '{', '[':
		debugPrint("document is in JSON format\n")
		err = json.Unmarshal(b, &ret)
	default:
		debugPrint("document is in YAML format\n")
		err = yaml.Unmarshal(b, &ret)
	}
	if err != nil {
		return ret, err
	}
	debugPrint("new document has %v test(s)\n", len(ret.Tests))
	debugPrint("new document has %v object(s)\n", len(ret.Objects))
	debugPrint("new document has %v variable(s)\n", len(ret.Variables))
	debugPrint("loaded: %+v\n", ret)

	debugPrint("validating document...\n")
	err = ret.Validate()
	if err != nil {
		return ret, err
	}

	return ret, nil
}

// Analyze a scribe document on the host system. The will prepare and
// execute all tests specified in the scribe document. Returns an error
// if a fatal error occurs.
//
// Note that an error in an individual test does not neccessarily represent
// a fatal error condition. In these cases, the test itself will be marked
// as having an error condition (stored in the Err field of the Test).
func AnalyzeDocument(d Document) error {
	debugPrint("preparing objects...\n")
	err := d.prepareObjects()
	if err != nil {
		return err
	}
	debugPrint("analyzing document...\n")
	return d.runTests()
}<commit_msg>fixes a spelling error in comment<commit_after>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor:
// - <NAME> <EMAIL>

package scribe

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

// Load a scribe JSON or YAML document from the reader specified by r.
// Returns a Document type that can be passed to AnalyzeDocument().
// On error, LoadDocument() returns the error that occurred.
func LoadDocument(r io.Reader) (Document, error) {
	var ret Document

	debugPrint("loading new document\n")
	b, err := ioutil.ReadAll(r)
	if err != nil {
		return ret, err
	}
	// clean up leading spaces, tabs and newlines
	b = bytes.TrimLeft(b, " \n\t")
	if len(b) < 10 {
		return ret, fmt.Errorf("the document is too small to be valid (%d bytes)", len(b))
	}
	switch b[0] {
	case '{', '[':
		debugPrint("document is in JSON format\n")
		err = json.Unmarshal(b, &ret)
	default:
		debugPrint("document is in YAML format\n")
		err = yaml.Unmarshal(b, &ret)
	}
	if err != nil {
		return ret, err
	}
	debugPrint("new document has %v test(s)\n", len(ret.Tests))
	debugPrint("new document has %v object(s)\n", len(ret.Objects))
	debugPrint("new document has %v variable(s)\n", len(ret.Variables))
	debugPrint("loaded: %+v\n", ret)

	debugPrint("validating document...\n")
	err = ret.Validate()
	if err != nil {
		return ret, err
	}

	return ret, nil
}

// Analyze a scribe document on the host system. The will prepare and
// execute all tests specified in the scribe document. Returns an error
// if a fatal error occurs.
//
// Note that an error in an individual test does not necessarily represent
// a fatal error condition. In these cases, the test itself will be marked
// as having an error condition (stored in the Err field of the Test).
func AnalyzeDocument(d Document) error {
	debugPrint("preparing objects...\n")
	err := d.prepareObjects()
	if err != nil {
		return err
	}
	debugPrint("analyzing document...\n")
	return d.runTests()
}
{'fraction_non_alphanumeric': 0.13103935894414329, 'fraction_numerical': 0.0037709168041480086, 'mean_word_length': 3.4957627118644066, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '279331', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1389, 'n_words': 539}