Skip to content

Commit

Permalink
fix: gestes management
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasleger committed Dec 17, 2024
1 parent 0c8df3a commit 54e7013
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 18 deletions.
4 changes: 4 additions & 0 deletions lib/quote_reader/naive_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def self.find_raison_sociale(text)
text[/\A(?:.*\s)?+(#{forme_jurique_raison_sociale_regex}\s+.+)\s+/, 1] ||
text[/Raison sociale\s*:\s*(#{FRENCH_CHARACTER_REGEX}+)/i, 1]
end

def self.find_rge_numbers(text)
find_label_numbers(text).select { |label_number| label_number.start_with?(/(?:R|E-)?E?/i) }
end

def self.find_sirets(text)
text.scan(/\b(\d{3}\s*\d{3}\s*\d{3}\s*\d{5})\b/i).flatten.filter_map { |e| e&.strip }.uniq
Expand Down
6 changes: 4 additions & 2 deletions lib/quote_reader/qa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Qa < Text
def read
return {} if text.blank?

@read_attributes = llm_read_attributes
llm_read_attributes
end

def version
Expand All @@ -29,8 +29,10 @@ def llm_read_attributes
ErrorNotifier.notify(e)
end

@read_attributes = mistral.read_attributes
@read_attributes = TrackingHash.new(mistral.read_attributes)
@result = mistral.result

read_attributes
end

def prompt
Expand Down
2 changes: 1 addition & 1 deletion lib/quote_validator/chauffage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def validate_chauffage(geste, error)
error << "puissance_manquant" if geste[:puissance].blank?
error << "marque_isolation_manquant" if geste[:marque].blank?
error << "reference_isolation_manquant" if geste[:reference].blank?
error << "etas_chauffage_manquant" if geste[:ETAS].blank # en %
error << "etas_chauffage_manquant" if geste[:ETAS].blank? # en %

# TODO: à challenger
@warnings << "remplacement_chaudiere_condensation_manquant" unless geste[:remplacement_chaudiere_condensation]
Expand Down
18 changes: 5 additions & 13 deletions lib/quote_validator/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,20 @@ def validate_address(address, type)
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength
def validate_works
content = quote.dig("choices", 0, "message", "content")
content_json_result = content[/(\{.+\})/im, 1]
if content_json_result
content_json_result = JSON.parse(content_json_result)
works = content_json_result[:gestes] || []
else
works = []
end

isolation = Isolation.new(quote)
menuiserie = Menuiserie.new(quote)
chauffage = Chauffage.new(quote)
eau_chaude = EauChaude.new(quote)
ventilation = Ventilation.new(quote)

@errors += works.flat_map do |geste| # rubocop:disable Metrics/BlockLength
gestes = quote[:gestes] || []
@errors += gestes.flat_map do |geste| # rubocop:disable Metrics/BlockLength
case geste[:type]

# ISOLATION
when "isolation_mur_ite"
isolation.validate_isolation_ite(geste)
when "isolation_combles_perdues"
when "isolation_comble_perdu", "isolation_combles_perdues"
isolation.validate_isolation_combles(geste)
when "isolation_rampants-toiture"
isolation.validate_isolation_rampants(geste)
Expand Down Expand Up @@ -174,13 +166,13 @@ def validate_works
chauffage.validate_poele_insert(geste)
when "systeme_solaire_combine"
chauffage.validate_systeme_solaire_combine(geste)
when "pac"
when "pac", "pac_air_eau"
chauffage.validate_pac(geste)

# EAU CHAUDE SANITAIRE
when "chauffe_eau_solaire_individuel"
eau_chaude.validate_cesi(geste)
when "chauffe_eau_thermodynamique"
when "chauffe_eau_thermo", "chauffe_eau_thermodynamique"
eau_chaude.validate_chauffe_eau_thermodynamique(geste)

# VENTILATION
Expand Down
34 changes: 32 additions & 2 deletions lib/tracking_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,47 @@

# TrackingHash is a subclass of Hash that tracks the keys that are accessed.
class TrackingHash < Hash
# rubocop:disable Metrics/MethodLength
def initialize(constructor = {})
super()

@keys_accessed = Set.new

return unless constructor.is_a?(Hash)

constructor&.each do |key, value|
self[key] = value.is_a?(Hash) ? TrackingHash.new(value) : value
self[key] = if value.is_a?(Hash)
TrackingHash.new(value)
elsif value.is_a?(Array)
value.map { |subvalue| TrackingHash.new(subvalue) }
else
value
end
end
end
# rubocop:enable Metrics/MethodLength

def [](key)
@keys_accessed.add(key)
super || super(key.to_s)

unless key?(key)
return super(key.to_s) if key.is_a?(Symbol)

return super(key.to_sym) if key.is_a?(String)
end

super
end

def dig(*keys)
current = self
keys.each do |key|
return nil unless current.is_a?(Hash) || current.is_a?(Array)

current = current[key] # Reuse overwritten methods
end

current
end

def keys_accessed
Expand Down
30 changes: 30 additions & 0 deletions spec/lib/tracking_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@
hash = described_class.new(a: 1)
expect(hash[:a]).to eq 1
end

# rubocop:disable RSpec/MultipleExpectations
it "works like an indifferent Hash" do
expect(described_class.new(a: 1)["a"]).to eq 1
expect(described_class.new("a" => 1)[:a]).to eq 1
end
# rubocop:enable RSpec/MultipleExpectations

context "with an empty Hash and unknown key" do
it "works as usual" do
expect(described_class.new[:unknown_key]).to be_nil
end
end
end

describe "#dig" do
context "with an empty Hash" do
it "works as usual" do
hash = described_class.new
expect(hash.dig(nil)).to be_nil # rubocop:disable Style/SingleArgumentDig
end
end

# rubocop:disable RSpec/MultipleExpectations
it "works like with quotes and symbols" do
hash = described_class.new(subhash_symbol: [{ "key" => 1 }])
expect(hash.dig("subhash_symbol", 0, "key")).to eq 1
expect(hash.dig(:subhash_symbol, 0, :key)).to eq 1
end
# rubocop:enable RSpec/MultipleExpectations
end

describe "#keys_accessed" do
Expand Down

0 comments on commit 54e7013

Please sign in to comment.