How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the jq programming language

Table of Contents

Problem Statement

Find all     tags without a language specified in the text of a page.
Display counts by language section: should display something like

Allow multiple files to be read.   Summarize all results by language:

Use the   Media Wiki API   to test actual RC tasks.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the jq programming language

Source code in the jq programming language

def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;

def trim: sub("^[ \t]+";"") | sub("[ \t]+$";"");

# Insert into a sorted list using bsearch
def binsert($x):
  (-bsearch($x) - 1) as $ix
  | if $ix < 0 then .
    else .[:$ix] + [$x] + .[$ix:]
    end;

def report:
  def header:
    "==\\s*{{\\s*header\\s*\\|\\s*(?<title>[^\\s\\}]+)\\s*}}\\s*==";

  reduce inputs as $line ( { bareCount:0, bareLang: {} };
      if .fileName != input_filename
      then .lastHeader = "No language"
      | .fileName = input_filename
      else .
      end
      | .line = ($line|trim)
      |  if .line | length == 0 then .
         else .header = ((.line | capture(header)) // null)
         | if .header
           then .lastHeader = .header.title
           elif .line|startswith("<lang>")
           then .bareCount += 1
           | .bareLang[.lastHeader][0] += 1
           | .fileName as $fileName
           | .bareLang[.lastHeader][1] |= binsert($fileName)
           else .
           end
         end )
  | "\(.bareCount) bare language tags:",
    (.bareLang
     | to_entries[] as {"key": $lang, "value": $value}
     | $value[0] as $count
     | $value[1] as $names
     | ("\($count|lpad(3)) in \($lang|lpad(15))" + ": " + ($names | join(", ")) )) ;

report

  

You may also check:How to resolve the algorithm Sum and product of an array step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Word frequency step by step in the Clojure programming language
You may also check:How to resolve the algorithm Calendar step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm First-class functions step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Bitmap/Write a PPM file step by step in the LiveCode programming language