<commit_before>  # encoding: utf-8

class Lom

  ####################
  ## LOM Metadata
  ####################

  # Metadata based on LOM (Learning Object Metadata) standard
  # LOM final draft: http://ltsc.ieee.org/wg12/files/LOM_1484_12_1_v1_Final_Draft.pdf
  def self.generateMetadata(ao, options={})
    _LOMschema = "custom"

    supportedLOMSchemas = ["custom","loose","ODS","ViSH"]
    if supportedLOMSchemas.include? options[:LOMschema]
      _LOMschema = options[:LOMschema]
    end

    if options[:target]
      myxml = ::Builder::XmlMarkup.new(:indent => 2, :target => options[:target])
    else
      myxml = ::Builder::XmlMarkup.new(:indent => 2)
      myxml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
    end
   
    #Select LOM Header options
    lomHeaderOptions = {}

    case _LOMschema
    when "loose","custom"
      lomHeaderOptions =  { 'xmlns' => "http://ltsc.ieee.org/xsd/LOM",
                            'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
                            'xsi:schemaLocation' => "http://ltsc.ieee.org/xsd/LOM lom.xsd"
                          }
    when "ODS"
      lomHeaderOptions =  { 'xmlns' => "http://ltsc.ieee.org/xsd/LOM",
                            'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
                            'xsi:schemaLocation' => "http://ltsc.ieee.org/xsd/LOM lomODS.xsd"
                          }
    else
      #Extension not supported/recognized
      lomHeaderOptions = {}
    end


    myxml.tag!("lom",lomHeaderOptions) do

      #Calculate some recurrent vars

      #Location
      loLocation = ao.getUrl
      loMetadataLocation = ao.getMetadataUrl

      #Identifier
      loId = loLocation
      loIdIsURI = true
      loIdIsURN = false

      begin
        loUri = URI.parse(loId)
        if %w( http https ).include?(loUri.scheme)
          loIdIsURI = true
        elsif %w( urn ).include?(loUri.scheme)
          loIdIsURN = true
        end
      rescue
      end
      if !loIdIsURI and !loIdIsURN
        #Build URN
        loId = "urn:ViSH:"+ao.id
      end

      #Language (LO language and metadata language)
      loLanguage = getLoLanguage(ao.language, _LOMschema)
      if loLanguage.nil?
        loLanOpts = {}
      else
        loLanOpts = { :language=> loLanguage }
      end
      metadataLanguage = "en"

      #Author name
      authorName = nil
      unless ao.author.nil? or ao.author.name.nil?
        authorName = ao.author.name
      end

      # loDate 
      # According to ISO 8601 (e.g. 2014-06-23)
      loCreateDate = ao.created_at.strftime("%Y-%m-%d").to_s
      loUpdateDate = ao.updated_at.nil? ? loCreateDate : ao.updated_at.strftime("%Y-%m-%d").to_s

      # loKeywords
      loKeywords = []
      if ao.tags && ao.tags.kind_of?(Array)
        loKeywords = ao.tags
      end

      #Building LOM XML

      myxml.general do
        
        unless loId.nil?
          myxml.identifier do
            if loIdIsURI
              myxml.catalog("URI")
            else
              myxml.catalog("URN")
            end
            myxml.entry(loId)
          end
        end

        myxml.title do
          if ao.title
            myxml.string(ao.title, loLanOpts)
          else
            myxml.string("Untitled", :language=> metadataLanguage)
          end
        end

        if loLanguage
          myxml.language(loLanguage)
        end
        
        myxml.description do
          if ao.description
            myxml.string(ao.description, loLanOpts)
          elsif ao.title
            myxml.string(ao.title + ". A resource provided by " + Vish::Application.config.full_domain + ".", :language=> metadataLanguage)
          else
            myxml.string("Resource provided by " + Vish::Application.config.full_domain + ".", :language=> metadataLanguage)
          end
        end
        unless loKeywords.blank?
          loKeywords.each do |tag|
            myxml.keyword do
              myxml.string(tag.to_s, loLanOpts)
            end
          end
        end
      end

      myxml.lifeCycle do
        myxml.version do
          myxml.string("v"+loUpdateDate.gsub("-","."), :language=>metadataLanguage)
        end
        myxml.status do
          myxml.source("LOMv1.0")
          if ao.public_scope?
            myxml.value("published")
          else
            myxml.value("unpublished")
          end<commit_msg>Fix on AO LOM metadata<commit_after>  # encoding: utf-8

class Lom

  ####################
  ## LOM Metadata
  ####################

  # Metadata based on LOM (Learning Object Metadata) standard
  # LOM final draft: http://ltsc.ieee.org/wg12/files/LOM_1484_12_1_v1_Final_Draft.pdf
  def self.generateMetadata(ao, options={})
    _LOMschema = "custom"

    supportedLOMSchemas = ["custom","loose","ODS","ViSH"]
    if supportedLOMSchemas.include? options[:LOMschema]
      _LOMschema = options[:LOMschema]
    end

    if options[:target]
      myxml = ::Builder::XmlMarkup.new(:indent => 2, :target => options[:target])
    else
      myxml = ::Builder::XmlMarkup.new(:indent => 2)
      myxml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
    end
   
    #Select LOM Header options
    lomHeaderOptions = {}

    case _LOMschema
    when "loose","custom"
      lomHeaderOptions =  { 'xmlns' => "http://ltsc.ieee.org/xsd/LOM",
                            'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
                            'xsi:schemaLocation' => "http://ltsc.ieee.org/xsd/LOM lom.xsd"
                          }
    when "ODS"
      lomHeaderOptions =  { 'xmlns' => "http://ltsc.ieee.org/xsd/LOM",
                            'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
                            'xsi:schemaLocation' => "http://ltsc.ieee.org/xsd/LOM lomODS.xsd"
                          }
    else
      #Extension not supported/recognized
      lomHeaderOptions = {}
    end


    myxml.tag!("lom",lomHeaderOptions) do

      #Calculate some recurrent vars

      #Location
      loLocation = ao.getUrl
      loMetadataLocation = ao.getMetadataUrl

      #Identifier
      loId = loLocation
      loIdIsURI = true
      loIdIsURN = false

      begin
        loUri = URI.parse(loId)
        if %w( http https ).include?(loUri.scheme)
          loIdIsURI = true
        elsif %w( urn ).include?(loUri.scheme)
          loIdIsURN = true
        end
      rescue
      end
      if !loIdIsURI and !loIdIsURN
        #Build URN
        loId = "urn:ViSH:"+ao.id
      end

      #Language (LO language and metadata language)
      loLanguage = getLoLanguage(ao.language, _LOMschema)
      if loLanguage.nil?
        loLanOpts = {}
      else
        loLanOpts = { :language=> loLanguage }
      end
      metadataLanguage = "en"

      #Author name
      authorName = nil
      unless ao.author.nil? or ao.author.name.nil?
        authorName = ao.author.name
      end

      # loDate 
      # According to ISO 8601 (e.g. 2014-06-23)
      loCreateDate = ao.created_at.strftime("%Y-%m-%d").to_s
      loUpdateDate = ao.updated_at.nil? ? loCreateDate : ao.updated_at.strftime("%Y-%m-%d").to_s

      # loKeywords
      loKeywords = []
      if ao.tags && ao.tags.kind_of?(Array)
        loKeywords = ao.tags
      end

      #Building LOM XML

      myxml.general do
        
        unless loId.nil?
          myxml.identifier do
            if loIdIsURI
              myxml.catalog("URI")
            else
              myxml.catalog("URN")
            end
            myxml.entry(loId)
          end
        end

        myxml.title do
          if ao.title
            myxml.string(ao.title, loLanOpts)
          else
            myxml.string("Untitled", :language=> metadataLanguage)
          end
        end

        if loLanguage
          myxml.language(loLanguage)
        end
        
        myxml.description do
          if ao.description
            myxml.string(ao.description, loLanOpts)
          elsif ao.title
            myxml.string(ao.title + ". A resource provided by " + Vish::Application.config.full_domain + ".", :language=> metadataLanguage)
          else
            myxml.string("Resource provided by " + Vish::Application.config.full_domain + ".", :language=> metadataLanguage)
          end
        end
        unless loKeywords.blank?
          loKeywords.each do |tag|
            myxml.keyword do
              myxml.string(tag.to_s, loLanOpts)
            end
          end
        end
      end

      myxml.lifeCycle do
        myxml.version do
          myxml.string("v"+loUpdateDate.gsub("-","."), :language=>metadataLanguage)
        end
        myxml.status do
          myxml.source("LOMv1.0")
          if ao.public_scope?
            myxml.value("final")
          else
            myxml.value("draft")
          end
{'fraction_non_alphanumeric': 0.11976862878530112, 'fraction_numerical': 0.009073380968583419, 'mean_word_length': 1.768602825745683, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 62}, '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': '7440305', 'n_tokens_mistral': 2830, 'n_tokens_neox': 2730, 'n_words': 539}