<commit_before>module Puree

  module XMLExtractor

    # Publication XML extractor.
    #
    class Publication < Puree::XMLExtractor::Resource

      def initialize(xml:)
        @resource_type = :publication
        super
      end

      # @return [String, nil]
      def bibliographical_note
        xpath_query_for_single_value('/bibliographicalNote')
      end

      # @return [String, nil]
      def category
        xpath_query_for_single_value '/publicationCategory/publicationCategory/term/localizedString'
      end

      # @return [String, nil]
      def description
        xpath_query_for_single_value '/abstract/localizedString'
      end

      # @return [Array<String>, nil]
      def dois
        xpath_query_for_multi_value '/electronicVersionAssociations/electronicVersionDOIAssociations/electronicVersionDOIAssociation/doi'
      end

      # @return [Array<Puree::Model::File>]
      def files
        xpath_result = xpath_query '/electronicVersionAssociations/electronicVersionFileAssociations/electronicVersionFileAssociation'
        docs = []
        xpath_result.each do |d|
          model = Puree::Model::File.new
          model.name = d.xpath('file/fileName').text.strip
          model.mime = d.xpath('file/mimeType').text.strip
          model.size = d.xpath('file/size').text.strip.to_i
          model.url = d.xpath('file/url').text.strip
          document_license = d.xpath('licenseType')
          if !document_license.empty?
            license = Puree::Model::CopyrightLicense.new
            license.name = document_license.xpath('term/localizedString').text.strip
            license.url = document_license.xpath('description/localizedString').text.strip
            model.license = license if license.data?
          end
          docs << model
        end
        docs.uniq { |d| d.url }
      end

      # @return [Array<String>]
      def keywords
        xpath_result =  xpath_query '/keywordGroups/keywordGroup/keyword/userDefinedKeyword/freeKeyword'
        data_arr = xpath_result.map { |i| i.text.strip }
        data_arr.uniq
      end

      # @return [String, nil]
      def language
        xpath_query_for_single_value '/language/term/localizedString'
      end

      # @return [Array<String>, nil]
      def links
        xpath_query_for_multi_value '/electronicVersionAssociations/electronicVersionLinkAssociations/electronicVersionLinkAssociation/link'
      end

      # @return [Array<Puree::Model::OrganisationHeader>]
      def organisations
        xpath_result = xpath_query '/organisations/association/organisation'
        Puree::XMLExtractor::Shared.organisation_multi_header xpath_result
      end

      # @return [Puree::Model::OrganisationHeader, nil]
      def owner
        xpath_result = xpath_query '/owner'
        Puree::XMLExtractor::Shared.organisation_header xpath_result
      end

      # @return [Array<Puree::Model::EndeavourPerson>]
      def persons_internal
        persons 'internal'
      end

      # @return [Array<Puree::Model::EndeavourPerson>]
      def persons_external
        persons 'external'
      end

      # @return [Array<Puree::Model::EndeavourPerson>]
      def persons_other
        persons 'other'
      end

      # @return [String, nil]
      def publication_place
        xpath_query_for_single_value '/associatedPublisher/placeOfPublication'
      end

      # @return [String, nil]
      def publisher
        xpath_query_for_single_value '/associatedPublisher/publisher/name'
      end

      # @return [Array<Puree::Model::PublicationStatus>]
      def statuses
        xpath_result = xpath_query '/publicationStatuses/publicationStatus'
        data = []<commit_msg>handles variations in xpath for publication<commit_after>module Puree

  module XMLExtractor

    # Publication XML extractor.
    #
    class Publication < Puree::XMLExtractor::Resource

      def initialize(xml:)
        @resource_type = :publication
        super
      end

      # @return [String, nil]
      def bibliographical_note
        xpath_query_for_single_value('/bibliographicalNote')
      end

      # @return [String, nil]
      def category
        xpath_query_for_single_value '/publicationCategory/publicationCategory/term/localizedString'
      end

      # @return [String, nil]
      def description
        xpath_query_for_single_value '/abstract/localizedString'
      end

      # @return [Array<String>, nil]
      def dois
        xpath_query_for_multi_value '/electronicVersionAssociations/electronicVersionDOIAssociations/electronicVersionDOIAssociation/doi'
      end

      # @return [Array<Puree::Model::File>]
      def files
        xpath_result = xpath_query '/electronicVersionAssociations/electronicVersionFileAssociations/electronicVersionFileAssociation'
        docs = []
        xpath_result.each do |d|
          model = Puree::Model::File.new
          model.name = d.xpath('file/fileName').text.strip
          model.mime = d.xpath('file/mimeType').text.strip
          model.size = d.xpath('file/size').text.strip.to_i
          model.url = d.xpath('file/url').text.strip
          document_license = d.xpath('licenseType')
          if !document_license.empty?
            license = Puree::Model::CopyrightLicense.new
            license.name = document_license.xpath('term/localizedString').text.strip
            license.url = document_license.xpath('description/localizedString').text.strip
            model.license = license if license.data?
          end
          docs << model
        end
        docs.uniq { |d| d.url }
      end

      # @return [Array<String>]
      def keywords
        xpath_result =  xpath_query '/keywordGroups/keywordGroup/keyword/userDefinedKeyword/freeKeyword'
        data_arr = xpath_result.map { |i| i.text.strip }
        data_arr.uniq
      end

      # @return [String, nil]
      def language
        xpath_query_for_single_value '/language/term/localizedString'
      end

      # @return [Array<String>, nil]
      def links
        xpath_query_for_multi_value '/electronicVersionAssociations/electronicVersionLinkAssociations/electronicVersionLinkAssociation/link'
      end

      # @return [Array<Puree::Model::OrganisationHeader>]
      def organisations
        xpath_result = xpath_query '/organisations/association/organisation'
        Puree::XMLExtractor::Shared.organisation_multi_header xpath_result
      end

      # @return [Puree::Model::OrganisationHeader, nil]
      def owner
        xpath_result = xpath_query '/owner'
        Puree::XMLExtractor::Shared.organisation_header xpath_result
      end

      # @return [Array<Puree::Model::EndeavourPerson>]
      def persons_internal
        persons 'internal'
      end

      # @return [Array<Puree::Model::EndeavourPerson>]
      def persons_external
        persons 'external'
      end

      # @return [Array<Puree::Model::EndeavourPerson>]
      def persons_other
        persons 'other'
      end

      # @return [String, nil]
      def publication_place
        # handles variations in path
        xpath_result = xpath_query_for_single_value '/associatedPublisher/placeOfPublication'
        xpath_result = xpath_query_for_single_value '/associatedPublishers/placeOfPublication' if !xpath_result
        xpath_result
      end

      # @return [String, nil]
      def publisher
        # handles variations in path
        xpath_result = xpath_query_for_single_value '/associatedPublisher/publisher/name'
        xpath_result = xpath_query_for_single_value '/associatedPublishers/publisher/name' if !xpath_result
        xpath_result
      end

      # @return [Array<Puree::Model::PublicationStatus>]
      def statuses
        xpath_result = xpath_query '/publicationStatuses/publicationStatus'
        data = []
{'fraction_non_alphanumeric': 0.08815958815958816, 'fraction_numerical': 0.0, 'mean_word_length': 2.7414540202214734, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3504371', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2195, 'n_words': 397}