<commit_before>class AnnotationsController < ApplicationController
  before_filter :authenticate_user!, :except => [:index, :show]
  after_filter :set_access_control_headers

  def index
    @annset, notice = get_annset(params[:annset_id])
    if @annset

      if (params[:pmdoc_id] || params[:pmcdoc_id])

        sourcedb, sourceid, serial = get_docspec(params)
        @doc, notice = get_doc(sourcedb, sourceid, serial, @annset)
        if @doc
          annotations = get_annotations(@annset, @doc, :encoding => params[:encoding])
          @text = annotations[:text]
          @catanns = annotations[:catanns]
          @insanns = annotations[:insanns]
          @relanns = annotations[:relanns]
          @modanns = annotations[:modanns]
        end

      else
        # retrieve annotatons to all the documents
        docs = @annset.docs
        anncollection = Array.new
        docs.each do |doc|
          anncollection.push (get_annotations(@annset, doc, :encoding => params[:encoding]))
        end
      end
    end

    respond_to do |format|
      if @annset and @text

        format.html { flash[:notice] = notice }
        format.json { render :json => annotations, :callback => params[:callback] }
        format.ttl  {
          if @annset.rdfwriter.empty?
            head :unprocessable_entity
          else
            render :text => get_conversion(annotations, @annset.rdfwriter, serial), :content_type => 'application/x-turtle'
          end
        }
        format.xml  {
          if @annset.xmlwriter.empty?
            head :unprocessable_entity
          else
            render :text => get_conversion(annotations, @annset.xmlwriter, serial), :content_type => 'application/xml;charset=urf-8'
          end
        }

      elsif anncollection

        format.json {
          file_name = (@annset)? @annset.name + ".zip" : "annotations.zip"
          t = Tempfile.new("pubann-temp-filename-#{Time.now}")
          Zip::ZipOutputStream.open(t.path) do |z|
            anncollection.each do |ann|
              title = "%s-%s-%02d-%s" % [ann[:source_db], ann[:source_id], ann[:division_id], ann[:section]]
              title.sub!(/\.$/, '')
              title.gsub!(' ', '_')
              title += ".json" unless title.end_with?(".json")
              z.put_next_entry(title)
              z.print ann.to_json
            end
          end
          send_file t.path, :type => 'application/zip',
                            :disposition => 'attachment',
                            :filename => file_name
          t.close
        }

      else
        format.html { flash[:notice] = notice }
        format.json { head :unprocessable_entity }
        format.ttl  { head :unprocessable_entity }<commit_msg>new function added to download annotations in turtle.<commit_after>require 'zip/zip'

class AnnotationsController < ApplicationController
  before_filter :authenticate_user!, :except => [:index, :show]
  after_filter :set_access_control_headers

  def index
    @annset, notice = get_annset(params[:annset_id])
    if @annset

      if (params[:pmdoc_id] || params[:pmcdoc_id])

        sourcedb, sourceid, serial = get_docspec(params)
        @doc, notice = get_doc(sourcedb, sourceid, serial, @annset)
        if @doc
          annotations = get_annotations(@annset, @doc, :encoding => params[:encoding])
          @text = annotations[:text]
          @catanns = annotations[:catanns]
          @insanns = annotations[:insanns]
          @relanns = annotations[:relanns]
          @modanns = annotations[:modanns]
        end

      else
        # retrieve annotatons to all the documents
        docs = @annset.docs
        anncollection = Array.new
        docs.each do |doc|
          anncollection.push (get_annotations(@annset, doc, :encoding => params[:encoding]))
        end
      end
    end

    respond_to do |format|
      if @annset and @text

        format.html { flash[:notice] = notice }
        format.json { render :json => annotations, :callback => params[:callback] }
        format.ttl  {
          if @annset.rdfwriter.empty?
            head :unprocessable_entity
          else
            render :text => get_conversion(annotations, @annset.rdfwriter), :content_type => 'application/x-turtle'
          end
        }
        format.xml  {
          if @annset.xmlwriter.empty?
            head :unprocessable_entity
          else
            render :text => get_conversion(annotations, @annset.xmlwriter, serial), :content_type => 'application/xml;charset=urf-8'
          end
        }

      elsif anncollection

        format.json {
          file_name = (@annset)? @annset.name + ".zip" : "annotations.zip"
          t = Tempfile.new("pubann-temp-filename-#{Time.now}")
          Zip::ZipOutputStream.open(t.path) do |z|
            anncollection.each do |ann|
              title = "%s-%s-%02d-%s" % [ann[:source_db], ann[:source_id], ann[:division_id], ann[:section]]
              title.sub!(/\.$/, '')
              title.gsub!(' ', '_')
              title += ".json" unless title.end_with?(".json")
              z.put_next_entry(title)
              z.print ann.to_json
            end
          end
          send_file t.path, :type => 'application/zip',
                            :disposition => 'attachment',
                            :filename => file_name
          t.close
        }
        format.ttl {
          ttl = ''
          anncollection.each_with_index do |ann, i|
            if i == 0 then ttl = get_conversion(ann, @annset.rdfwriter).split("\n")[0..8].join("\n") end
            ttl += "\n" + get_conversion(ann, @annset.rdfwriter).split("\n")[9..-1].join("\n")
          end
          render :text => ttl, :content_type => 'application/x-turtle', :filename => @annset.name
        }

      else
        format.html { flash[:notice] = notice }
        format.json { head :unprocessable_entity }
        format.ttl  { head :unprocessable_entity }
{'fraction_non_alphanumeric': 0.1243070720645053, 'fraction_numerical': 0.0018478078279858894, 'mean_word_length': 2.0470829068577276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6189395', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1721, 'n_words': 366}