<commit_before>module Healthy
  module A19
    class Transformer
      attr_reader :message

      def initialize(message)
        @message = message
      end

      def to_patient
        patient = {}
        %w(status channel source identities birthdate gender
           firstname initials lastname display_name email
           address_type street city zipcode country).map(&:to_sym).each do |key|
          begin
            patient[key] = send(key)
          rescue NameError
            patient[key] = :todo
          end
        end
        patient
      end

      def source
        message.fetch('MSH').fetch('MSH.4').fetch('MSH.4.1')
      end

      def identities
        message.fetch('PID').fetch('PID.3').map do |identity|
          next if identity.fetch('PID.3.1') == "\"\""
          {ident: identity.fetch('PID.3.1'), authority: identity.fetch('PID.3.5')}
        end.compact
      end

      def firstname
        # TODO UMCG
        return unless names[:nick]
        names[:nick].fetch('PID.5.2')
      end

      def initials
        # TODO UMCG
        "#{names[:legal].fetch('PID.5.2')} #{names[:legal].fetch('PID.5.3')}".strip
      end

      def lastname
        # TODO UMCG
        #                   // lastname zit in 5.1.3, maar "<NAME>" zit misschien in 5.1.2
        #                   lastname        = clean(legal_name['PID.5.1']['PID.5.1.3'].toString()); // Fictief
        #                   lastname_prefix = clean(legal_name['PID.5.1']['PID.5.1.2'].toString()); // <NAME>
        #                   if (lastname_prefix != "") {
        #                       tmp['lastname'] = lastname_prefix + " " + lastname;
        #                   } else {
        #                       tmp['lastname'] = lastname;
        #                   }
        prefix   = clean(names[:legal].fetch('PID.5.1').fetch('PID.5.1.2'))
        lastname = clean(names[:legal].fetch('PID.5.1').fetch('PID.5.1.3'))
        "#{prefix} #{lastname}".strip
      end

      def display_name
        return unless names[:display]
        names[:display].fetch('PID.5.1')
      end

#           if (nick_name) {
#               tmp['firstname'] = clean(nick_name['PID.5.2'].toString());
#           }

      def birthdate
        message.fetch('PID').fetch('PID.7').fetch('PID.7.1')
      end

      def email
        message.fetch('PID').fetch('PID.13').find do |record|
          record.fetch('PID.13.2') == 'NET'
        end.fetch('PID.13.1')
      end

      def gender
        message.fetch('PID').fetch('PID.8').fetch('PID.8.1')
      end

      private

      def names
        names = {}
        message.fetch('PID').fetch('PID.5').each do |record|
          case record.fetch('PID.5.7')
          when 'L'
            names[:legal] = record
          when 'D'
            names[:display] = record<commit_msg>remove comments<commit_after>module Healthy
  module A19
    class Transformer
      attr_reader :message

      def initialize(message)
        @message = message
      end

      def to_patient
        patient = {}
        %w(status channel source identities birthdate gender
           firstname initials lastname display_name email
           address_type street city zipcode country).map(&:to_sym).each do |key|
          begin
            patient[key] = send(key)
          rescue NameError
            patient[key] = :todo
          end
        end
        patient
      end

      def source
        message.fetch('MSH').fetch('MSH.4').fetch('MSH.4.1')
      end

      def identities
        message.fetch('PID').fetch('PID.3').map do |identity|
          next if identity.fetch('PID.3.1') == "\"\""
          {ident: identity.fetch('PID.3.1'), authority: identity.fetch('PID.3.5')}
        end.compact
      end

      def firstname
        # TODO UMCG
        return unless names[:nick]
        names[:nick].fetch('PID.5.2')
      end

      def initials
        # TODO UMCG
        "#{names[:legal].fetch('PID.5.2')} #{names[:legal].fetch('PID.5.3')}".strip
      end

      def lastname
        # TODO UMCG
        prefix   = clean(names[:legal].fetch('PID.5.1').fetch('PID.5.1.2'))
        lastname = clean(names[:legal].fetch('PID.5.1').fetch('PID.5.1.3'))
        "#{prefix} #{lastname}".strip
      end

      def display_name
        return unless names[:display]
        names[:display].fetch('PID.5.1')
      end

      def birthdate
        message.fetch('PID').fetch('PID.7').fetch('PID.7.1')
      end

      def email
        message.fetch('PID').fetch('PID.13').find do |record|
          record.fetch('PID.13.2') == 'NET'
        end.fetch('PID.13.1')
      end

      def gender
        message.fetch('PID').fetch('PID.8').fetch('PID.8.1')
      end

      private

      def names
        names = {}
        message.fetch('PID').fetch('PID.5').each do |record|
          case record.fetch('PID.5.7')
          when 'L'
            names[:legal] = record
          when 'D'
            names[:display] = record
{'fraction_non_alphanumeric': 0.14825990746328707, 'fraction_numerical': 0.02253067793200563, 'mean_word_length': 1.670247046186896, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2950733', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1478, 'n_words': 290}