<commit_before>package ccd

import (
	"errors"
	"io"
	"strings"

	"github.com/jteeuwen/go-pkg-xmlx"
)

var (
	DefaultParsers = []Parser{
		PatientParser, AllergiesParser, ImmunizationsParser,
		MedicationsParser, ProblemsParser,
		ResultsParser, VitalSignsParser, SocialHistoryParser,
	}
)

type Code struct {
	CodeSystemName string
	Type           string
	CodeSystem     string
	Code           string
	DisplayName    string
}

func (c *Code) decode(n *xmlx.Node) {
	if n == nil {
		return
	}
	c.CodeSystem = n.As("*", "codeSystem")
	// code.CodeSystemName, _ = codeSystemToName(code.CodeSystem)
	c.Code = n.As("*", "code")
	c.DisplayName = n.As("*", "displayName")
	c.Type = n.As("*", "type")
}

type CCD struct {
	Patient       Patient
	Immunizations []Immunization
	Medications   []Medication
	Problems      []Problem
	Results       []Result
	VitalSigns    []VitalSign
	Allergies     []Allergy
	SocialHistory []SocialHistory

	// Right now doc_parsers will only have one map entry "*"
	doc_parsers     map[string]Parsers
	section_parsers map[string]Parsers
}

// New CCD object with no parsers. Use NewDefaultCCD()
// or add your own parsers with AddParsers() if you want
// to actually parse anything.
func NewCCD() *CCD {
	c := &CCD{}
	c.doc_parsers = make(map[string]Parsers)
	c.section_parsers = make(map[string]Parsers)
	return c
}

// New CCD object with all the default parsers
func NewDefaultCCD() *CCD {
	c := NewCCD()
	c.AddParsers(DefaultParsers...)
	return c
}

func (c *CCD) AddParsers(parsers ...Parser) {
	for _, p := range parsers {
		if p.Organization == "" {
			p.Organization = "*"
		}

		p.Organization = strings.ToLower(p.Organization)

		if p.Type == PARSE_DOC {
			if p.Values == nil {
				p.Values = []string{"*"}
			}

			for _, v := range p.Values {
				c.doc_parsers[v] = insertSortParser(p, c.doc_parsers[v])
			}
		} else if p.Type == PARSE_SECTION {
			if p.Values == nil {
				panic("ccd: Section parser cannot have an empty Value.")
			}

			for _, v := range p.Values {
				c.section_parsers[v] = insertSortParser(p, c.section_parsers[v])
			}
		} else {
			panic("ccd: Unknown parser type.")
		}
	}
}

func (c *CCD) ParseFile(filename string) error {
	doc := xmlx.New()
	err := doc.LoadFile(filename, nil)
	if err != nil {
		return err
	}

	return c.ParseDoc(doc)
}

func (c *CCD) ParseStream(r io.Reader) error {
	doc := xmlx.New()
	err := doc.LoadStream(r, nil)
	if err != nil {
		return err
	}

	return c.ParseDoc(doc)
}

func (c *CCD) Parse(data []byte) error {
	doc := xmlx.New()
	err := doc.LoadBytes(data, nil)
	if err != nil {
		return err
	}

	return c.ParseDoc(doc)
}

type ParseType int

const (<commit_msg>added charset decoding for ccds<commit_after>package ccd

import (
	"errors"
	"io"
	"strings"

	"golang.org/x/net/html/charset"

	"github.com/jteeuwen/go-pkg-xmlx"
)

var (
	DefaultParsers = []Parser{
		PatientParser, AllergiesParser, ImmunizationsParser,
		MedicationsParser, ProblemsParser,
		ResultsParser, VitalSignsParser, SocialHistoryParser,
	}
)

type Code struct {
	CodeSystemName string
	Type           string
	CodeSystem     string
	Code           string
	DisplayName    string
}

func (c *Code) decode(n *xmlx.Node) {
	if n == nil {
		return
	}
	c.CodeSystem = n.As("*", "codeSystem")
	// code.CodeSystemName, _ = codeSystemToName(code.CodeSystem)
	c.Code = n.As("*", "code")
	c.DisplayName = n.As("*", "displayName")
	c.Type = n.As("*", "type")
}

type CCD struct {
	Patient       Patient
	Immunizations []Immunization
	Medications   []Medication
	Problems      []Problem
	Results       []Result
	VitalSigns    []VitalSign
	Allergies     []Allergy
	SocialHistory []SocialHistory

	// Right now doc_parsers will only have one map entry "*"
	doc_parsers     map[string]Parsers
	section_parsers map[string]Parsers
}

// New CCD object with no parsers. Use NewDefaultCCD()
// or add your own parsers with AddParsers() if you want
// to actually parse anything.
func NewCCD() *CCD {
	c := &CCD{}
	c.doc_parsers = make(map[string]Parsers)
	c.section_parsers = make(map[string]Parsers)
	return c
}

// New CCD object with all the default parsers
func NewDefaultCCD() *CCD {
	c := NewCCD()
	c.AddParsers(DefaultParsers...)
	return c
}

func (c *CCD) AddParsers(parsers ...Parser) {
	for _, p := range parsers {
		if p.Organization == "" {
			p.Organization = "*"
		}

		p.Organization = strings.ToLower(p.Organization)

		if p.Type == PARSE_DOC {
			if p.Values == nil {
				p.Values = []string{"*"}
			}

			for _, v := range p.Values {
				c.doc_parsers[v] = insertSortParser(p, c.doc_parsers[v])
			}
		} else if p.Type == PARSE_SECTION {
			if p.Values == nil {
				panic("ccd: Section parser cannot have an empty Value.")
			}

			for _, v := range p.Values {
				c.section_parsers[v] = insertSortParser(p, c.section_parsers[v])
			}
		} else {
			panic("ccd: Unknown parser type.")
		}
	}
}

func (c *CCD) ParseFile(filename string) error {
	doc := xmlx.New()
	err := doc.LoadFile(filename, charset.NewReaderLabel)
	if err != nil {
		return err
	}

	return c.ParseDoc(doc)
}

func (c *CCD) ParseStream(r io.Reader) error {
	doc := xmlx.New()
	err := doc.LoadStream(r, charset.NewReaderLabel)
	if err != nil {
		return err
	}

	return c.ParseDoc(doc)
}

func (c *CCD) Parse(data []byte) error {
	doc := xmlx.New()
	err := doc.LoadBytes(data, charset.NewReaderLabel)
	if err != nil {
		return err
	}

	return c.ParseDoc(doc)
}

type ParseType int

const (
{'fraction_non_alphanumeric': 0.13581841668204467, 'fraction_numerical': 0.0, 'mean_word_length': 3.336, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '450449', 'n_tokens_mistral': 2153, 'n_tokens_neox': 1987, 'n_words': 505}