Rosetta Code/Rank languages by popularity: Difference between revisions

m (→‎Raku: Using the API: New URL for relocated site)
Line 2,603:
 
=={{header|Julia}}==
Uses the API for the language list and page scraping for the example counts for each language.
<syntaxhighlight lang="julia">using HTTP
<syntaxhighlight lang="julia">""" Rosetta code task rosettacode.org/wiki/Rosetta_Code/Rank_languages_by_popularity """
 
using Dates
try
using DataFrames
response = HTTP.request("GET", "http://rosettacode.org/mw/index.php?title=Special:Categories&limit=5000")
using HTTP
langcount = Dict{String, Int}()
using JSON3
for mat in eachmatch(r"<li><a href[^\>]+>([^\<]+)</a>[^1-9]+(\d+)[^\w]+member.?.?</li>", String(response.body))
 
if match(r"^Programming", mat.captures[1]) == nothing
""" Get listing of all tasks and draft tasks with authors and dates created, with the counts as popularity """
langcount[mat.captures[1]] = parse(Int, mat.captures[2])
function rosetta_code_language_example_counts(verbose = false)
URL = "https://rosettacode.org/w/api.php?"
LANGPARAMS = ["action" => "query", "format" => "json", "formatversion" => "2", "generator" => "categorymembers",
"gcmtitle" => "Category:Programming_Languages", "gcmlimit" => "500", "rawcontinue" => "", "prop" => "title"]
queryparams, lang_cuid = copy(LANGPARAMS), Pair{String, Int}[]
df = empty!(DataFrame([[""], [0]], ["ProgrammingLanguage", "ExampleCount"]))
 
while true # get all the languages listed, with curid, eg rosettacode.org/w/index.php?curid=196 for C
resp = HTTP.get(URL * join(map(p -> p[1] * (p[2] == "" ? "" : ("=" * p[2])), queryparams), "&"))
json = JSON3.read(String(resp.body))
pages = json.query.pages
reg = r"The following \d+ pages are in this category, out of ([\d\,]+) total"
for p in pages
lang = replace(p.title, "Category:" => "")
langpage = String(HTTP.get("https://rosettacode.org/w/index.php?curid=" * string(p.pageid)).body)
if !((m = match(reg, langpage)) isa Nothing)
push!(df, [lang, parse(Int, replace(m.captures[1], "," => ""))])
verbose && println("Language: $lang, count: ", m.captures[1])
end
end
!haskey(json, "query-continue") && break # break if no more pages, else continue to next pages
queryparams = vcat(LANGPARAMS, "gcmcontinue" => json["query-continue"]["categorymembers"]["gcmcontinue"])
end
 
langs = sort(collect(keys(langcount)), lt=(x, y)->langcount[x]<langcount[y], rev=true)
return sort!(df, :ExampleCount, rev = true)
for (i, lang) in enumerate(langs)
println("Language $lang can be ranked #$i at $(langcount[lang]).")
end
catch y
println("HTTP request failed: $y.")
exit()
end
</syntaxhighlight>{{out}
<pre>
Language Phix can be ranked #1 at 995.
Language Racket can be ranked #2 at 989.
Language Perl can be ranked #3 at 969.
Language Julia can be ranked #4 at 968.
Language C can be ranked #5 at 944.
Language Tcl can be ranked #6 at 930.
Language Zkl can be ranked #7 at 919.
Language J can be ranked #8 at 905.
Language Java can be ranked #9 at 900.
Language REXX can be ranked #10 at 892.
Language D can be ranked #11 at 874.
Language Ruby can be ranked #12 at 869.
Language Haskell can be ranked #13 at 853.
Language Scala can be ranked #14 at 792.
Language Sidef can be ranked #15 at 788.
Language PicoLisp can be ranked #16 at 775.
Language C sharp can be ranked #17 at 763.
Language Mathematica can be ranked #18 at 743.
Language C++ can be ranked #19 at 738.
Language Common Lisp can be ranked #20 at 667.
Language Ada can be ranked #21 at 656.
Language AutoHotkey can be ranked #22 at 628.
Language JavaScript can be ranked #23 at 619.
Language Lua can be ranked #24 at 618.
Language WikiStubs can be ranked #25 at 614.
...
</pre>
 
println("Date: ", now())
===Julia: Using web scraping===
println(rosetta_code_language_example_counts())
{{trans|Python}}
</syntaxhighlight lang="julia">{{out}}
using HTTP, Dates
response = HTTP.request("GET", "http://rosettacode.org/wiki/Category:Programming_Languages")
languages = Set(m.captures[1] for m in eachmatch(r"title=\"Category:(.*?)\">",String(response.body)))
response = HTTP.request("GET", "http://rosettacode.org/mw/index.php?title=Special:Categories&limit=5000")
response = replace(String(response.body),"," => "")
reg = r"<li><a[^>]+>([^<]+)</a>[^(]*[\(](\d+) member[s]?[)]</li>"
ms = eachmatch(reg,response)
members = [[1,parse(Int,m.captures[2]),m.captures[1]] for m in ms]
filter!(x -> x[3] in languages, members)
sort!(members, by = x -> (-x[2],x[3]))
for i in 2:length(members)
if members[i-1][2] == members[i][2]
members[i][1] = members[i-1][1]
else
members[i][1] = i
end
end
println("Sample output on ", Dates.day(now()), " ", Dates.monthname(now()), " ", Dates.year(now()), ":\n")
for (rank,entries,name) in members[1:10]
println("Rank: ", lpad(rank,4), lpad(" ($entries entries) ",16), name)
end
</syntaxhighlight>
{{out}}
<pre>
Sample output on 22 July 2019:
 
Rank: 1 (1154 entries) Go
Rank: 2 (1089 entries) Perl 6
Rank: 3 (1070 entries) Julia
Rank: 4 (1066 entries) Python
Rank: 5 (1062 entries) Phix
Rank: 6 (1045 entries) Kotlin
Rank: 7 (1026 entries) Perl
Rank: 8 (991 entries) Racket
Rank: 9 (952 entries) C
Rank: 10 (945 entries) J
</pre>
 
4,105

edits