<commit_before>
local U = require "Quanta.Util"
local O = require "Quanta.Object"
local Entity = require "Quanta.Entity"
local Measurement = require "Quanta.Measurement"
local M = U.module("Quanta.Instance")

U.class(M)

function M:__init(obj, search_in, controllers)
	self.name = ""
	self.name_hash = O.NAME_NULL
	-- Entity or Unit
	self.entity = nil
	self.source = 0
	self.sub_source = 0
	self.source_certain = true
	self.sub_source_certain = true
	self.variant_certain = true
	self.presence_certain = true
	self.measurements = {}
	self.selection = {}
	self.modifiers = {}

	if obj then
		self:from_object(obj, search_in, controllers)
	end
end

function M:set_name(name)
	self.name = name
	-- utf8(¿) => C2 BF
	if string.sub(self.name, -2) == "¿" then
		self.name = string.sub(
			self.name, 1,
			string.sub(self.name, -3, -3) == "_" and -4 or -3
		)
		self.variant_certain = false
	else
		self.variant_certain = true
	end
	self.name_hash = O.hash_name(self.name)
end

function M:from_object(obj, search_in, controllers)
	U.type_assert(obj, "userdata")
	U.type_assert(search_in, "table")
	U.type_assert(controllers, "table")

	self:set_name(O.is_identifier(obj) and O.identifier(obj) or "")
	self.entity = nil
	self.source = O.source(obj)
	self.sub_source = O.sub_source(obj)
	self.source_certain = not O.marker_source_uncertain(obj)
	self.sub_source_certain = not O.marker_sub_source_uncertain(obj)
	self.presence_certain = not (O.marker_value_uncertain(obj) or O.marker_value_guess(obj))

	self.measurements = {}
	self.selection = {}
	self.modifiers = {}

	if O.has_quantity(obj) then
		local quantity = O.quantity(obj)
		if not O.is_expression(quantity) and O.has_children(quantity) then
			for _, sub in O.children(quantity) do
				local m = Measurement(sub)
				if not m:is_empty() then
					table.insert(self.measurements, m)
				end
			end
		else
			local m = Measurement(quantity)
			if not m:is_empty() then
				table.insert(self.measurements, m)
			end
		end
	end

	if self.name_hash ~= O.NAME_NULL then
		for _, s in pairs(search_in) do
			if Entity.is_universe(s) then
				self.entity = s.index_hash[self.name_hash]
			else
				U.type_assert(s, "table")
				self.entity = s[self.name_hash]
			end
			if self.entity then
				break
			end
		end
	end

	for _, sub in O.children(obj) do
		if O.is_named(sub) then
			-- TODO: pattern matching
			if O.name_hash(sub) == O.name_hash("d") then
				self.description = O.text(sub)
			end
		else
			table.insert(self.selection, M(sub, search_in, controllers))
		end<commit_msg>lib/lua/Instance: renamed entity property to item.<commit_after>
local U = require "Quanta.Util"
local O = require "Quanta.Object"
local Entity = require "Quanta.Entity"
local Measurement = require "Quanta.Measurement"
local M = U.module("Quanta.Instance")

U.class(M)

function M:__init(obj, search_in, controllers)
	self.name = ""
	self.name_hash = O.NAME_NULL
	-- Entity or Unit
	self.item = nil
	self.source = 0
	self.sub_source = 0
	self.source_certain = true
	self.sub_source_certain = true
	self.variant_certain = true
	self.presence_certain = true
	self.measurements = {}
	self.selection = {}
	self.modifiers = {}

	if obj then
		self:from_object(obj, search_in, controllers)
	end
end

function M:set_name(name)
	self.name = name
	-- utf8(¿) => C2 BF
	if string.sub(self.name, -2) == "¿" then
		self.name = string.sub(
			self.name, 1,
			string.sub(self.name, -3, -3) == "_" and -4 or -3
		)
		self.variant_certain = false
	else
		self.variant_certain = true
	end
	self.name_hash = O.hash_name(self.name)
end

function M:from_object(obj, search_in, controllers)
	U.type_assert(obj, "userdata")
	U.type_assert(search_in, "table")
	U.type_assert(controllers, "table")

	self:set_name(O.is_identifier(obj) and O.identifier(obj) or "")
	self.item = nil
	self.source = O.source(obj)
	self.sub_source = O.sub_source(obj)
	self.source_certain = not O.marker_source_uncertain(obj)
	self.sub_source_certain = not O.marker_sub_source_uncertain(obj)
	self.presence_certain = not (O.marker_value_uncertain(obj) or O.marker_value_guess(obj))

	self.measurements = {}
	self.selection = {}
	self.modifiers = {}

	if O.has_quantity(obj) then
		local quantity = O.quantity(obj)
		if not O.is_expression(quantity) and O.has_children(quantity) then
			for _, sub in O.children(quantity) do
				local m = Measurement(sub)
				if not m:is_empty() then
					table.insert(self.measurements, m)
				end
			end
		else
			local m = Measurement(quantity)
			if not m:is_empty() then
				table.insert(self.measurements, m)
			end
		end
	end

	if self.name_hash ~= O.NAME_NULL then
		for _, s in pairs(search_in) do
			if Entity.is_universe(s) then
				self.item = s.index_hash[self.name_hash]
			else
				U.type_assert(s, "table")
				self.item = s[self.name_hash]
			end
			if self.item then
				break
			end
		end
	end

	for _, sub in O.children(obj) do
		if O.is_named(sub) then
			-- TODO: pattern matching
			if O.name_hash(sub) == O.name_hash("d") then
				self.description = O.text(sub)
			end
		else
			table.insert(self.selection, M(sub, search_in, controllers))
		end
{'fraction_non_alphanumeric': 0.1218314010611122, 'fraction_numerical': 0.003930045195519749, 'mean_word_length': 3.779342723004695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6178012', 'n_tokens_mistral': 2103, 'n_tokens_neox': 1880, 'n_words': 432}