Rosetta Code/Rank languages by popularity: Difference between revisions
Rosetta Code/Rank languages by popularity (view source)
Revision as of 15:37, 3 June 2024
, 27 days ago→{{header|Ada}}
Thundergnat (talk | contribs) m (→Raku: Using the API: New URL for relocated site) |
JoseAlexis (talk | contribs) |
||
(19 intermediate revisions by 9 users not shown) | |||
Line 33:
=={{header|Ada}}==
===Ada 2022: using web scraping===
{{libheader|AWS}}
<syntaxhighlight lang="ada">
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 40 ⟶ 43:
with Ada.Containers.Ordered_Sets;
with Ada.Strings.Unbounded.Less_Case_Insensitive;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with AWS.Client; use AWS.Client;
with AWS.Messages; use AWS.Messages;
with AWS.Response;
procedure
use Ada.Strings;
function "+" (S : String) return Unbounded_String
renames To_Unbounded_String;
type A_Language_Count is
record
Count :
Language : Unbounded_String;
end record;
Line 64 ⟶ 70:
function "<" (L, R : A_Language_Count) return Boolean is
begin
-- Sort by 'Count' and then by Language name
return L.Count < R.Count
or else (L.Count = R.Count
and then Less_Case_Insensitive
(Left
Right => R.Language));
end "<";
Line 77 ⟶ 84:
procedure Find_Counts (S : String) is
function Strip_Character (S : String; C : Character) return String is
S_Copy_Str : String (1 .. S'Length);
S_Copy_Index : Natural := 0;
begin
for I in S'Range loop
if S (I) /= C then
S_Copy_Index := S_Copy_Index + 1;
S_Copy_Str (S_Copy_Index) := S (I);
end if;
end loop;
return S_Copy_Str (S_Copy_Str'First .. S_Copy_Index);
end Strip_Character;
function Ignore_Category (L : String) return Boolean is
type Unbounded_String_Array is array (Positive range <>)
of Unbounded_String;
-- This list is quite comprehensive, but not complete
Categories_To_Ignore : Unbounded_String_Array := [
+"Pages with syntax highlighting errors",
+"Programming",
+"Examples needing attention",
+"Tasks needing attention",
+"Language users",
+"Implementations",
+"Solutions by ",
+"Maintenance/OmitCategoriesCreated",
+"Collection Members",
+"Pages with too many expensive parser function calls",
+"Garbage collection",
+" User",
+"SQL User",
+"Typing",
+"Parameter passing",
+"Execution method",
+"Unimplemented tasks by language",
+"Wolfram Language",
+"/Omit",
+"Wren-",
+"WrenGo",
+"Phix/",
+"PhixClass",
+"Basic language learning",
+"Encyclopedia",
+"RCTemplates",
+"SysUtils",
+"Action! ",
+"Text processing",
+"Image processing",
+"Scala Digital Signal Processing",
+"List processing",
+"Digital signal processing",
+"Processing Python",
+"Classic CS problems and programs",
+"Brainf*** related",
+"Data Structures",
+"Perl modules",
+"Perl/",
+"Perl:LWP",
+"Perl 6 related",
+"Flow control",
+"Excessively difficult task",
+"WikiStubs",
+"Impl needed",
+"Recursion"
];
begin
for Category of Categories_To_Ignore loop
declare
Category_At : constant Natural :=
Index (+To_Lower (L),
To_Lower (To_String (Category)));
begin
if Category_At /= 0 then
return True;
end if;
end;
end loop;
return False;
end Ignore_Category;
Title_Str : constant String := "title=""Category:";
End_A_Tag_Str : constant String := "</a>";
Space_Paren_Str : constant String := " (";
Title_At : constant Natural := Index (S, Title_Str);
begin
if Title_At /= 0 then
declare
Language : constant String :=
S (Closing_Bracket_At + 1 .. End_A_Tag_At - 1);
Space_Paren_At : constant Natural :=
Index (S (End_A_Tag_At + 1 .. S'Last), Space_Paren_Str);
Space_At : constant Natural :=
Index (S (Space_Paren_At + Space_Paren_Str'Length + 1
.. S'Last),
" ");
Count : constant Natural :=
Natural'Value (
Strip_Character (
S (Space_Paren_At +
Space_Paren_Str'Length
.. Space_At - 1),
','));
begin
if
and then End_A_Tag_At /= 0
and then Space_Paren_At /= 0
and then Space_At /= 0
then
begin
Counts.Insert (New_Item => (Count, +Language));
end if;
exception
when Constraint_Error =>
Put_Line (Standard_Error, "Warning: repeated language: " &
-- Ignore repeated results.
null;
end;
end if;
-- Recursively parse the string for languages and counts
Find_Counts (S (Space_At + 1 .. S'Last));
end;
Line 116 ⟶ 233:
Place := Place + 1;
end Display;
Http_Source : constant AWS.Response.Data :=
AWS.Client.Get ("http://rosettacode.org/
"title=Special:Categories&limit=5000",
Follow_Redirection => True);
Status : Status_Code;
begin
Put_Line ("Getting website data...");
Status := AWS.Response.Status_Code (Http_Source);
if Status not in Success then
Put_Line ("Unable to retrieve data => Status Code :" &
Image (Status) &
" Reason :" & Reason_Phrase (Status));
raise Connection_Error;
end if;
Put_Line ("Finding categories...");
Find_Counts (AWS.Response.Message_Body (Http_Source));
Put_Line ("Displaying categories...");
Counts.Reverse_Iterate (Display'Access);
Put_Line ("Process complete.");
end Rank_Languages_By_Popularity;
</syntaxhighlight>
{{out|Sample output}}
<pre>
1. 1683 - Phix
2. 1676 - Wren
3. 1653 - Julia
4. 1623 - Raku
5. 1577 - Nim
6. 1553 - Go
7. 1548 - Perl
8. 1532 - Python
9. 1416 - J
10. 1349 - Java
11. 1333 - FreeBASIC
12. 1300 - C
13. 1282 - C++
14. 1239 - Ruby
15. 1209 - Mathematica
16. 1175 - Haskell
17. 1156 - REXX
18. 1152 - Kotlin
19. 1149 - Jq
20. 1102 - Racket
21. 1055 - Sidef
22. 1021 - ALGOL 68
23. 1017 - 11l
24. 1012 - Zkl
25. 1004 - Factor
26. 993 - D
27. 989 - C sharp
28. 987 - Tcl
29. 973 - Scala
30. 959 - Ada
31. 954 - Rust
32. 949 - Delphi
33. 947 - Lua
34. 868 - F Sharp
35. 867 - XPL0
</pre>
=={{header|ALGOL 68}}==
Line 1,176 ⟶ 1,349:
=={{header|C sharp|C#}}==
Sorting only programming languages.<br>
Note that this does not distinguish between tasks and non-task categories, so the counts can exceed the total number of tasks.<br>
Note this may need to set the Security Protocol type to Tls12 - see the section Problem running the C# entry in [[Talk:Rosetta_Code/Count_examples]]
<syntaxhighlight lang="csharp">
using System;
using System.Collections;
using System.Collections.Generic;
Line 1,188 ⟶ 1,364:
static void Main(string[] args)
{
string
);
ArrayList langs = new ArrayList();
Dictionary<string, int> qtdmbr = new Dictionary<string, int>();
string cmcontinue = "";
do
{
string get1 = new WebClient().DownloadString("http://www.rosettacode.org/w/api.php?"
+"action=query&list=categorymembers"
+"&cmtitle=Category:Programming_Languages"
+"&cmlimit=500&format=json"
+cmcontinue
);
cmcontinue = "";
MatchCollection languageMatch = new Regex("\"title\":\"Category:(.+?)\"").Matches(get1);
MatchCollection cmcontinueMatch = new Regex("cmcontinue\":\"([a-z0-9A-Z|]*)\"").Matches(get1);
foreach (Match lang in languageMatch) langs.Add(lang.Groups[1].Value);
foreach (Match more in cmcontinueMatch) cmcontinue = "&cmcontinue=" + more.Groups[1].Value;
}
while( cmcontinue != "" );
MatchCollection match2 =
new Regex("title=\"Category:(.+?)\">.+?</a>[^(]*\\(([\\d,.]+) members\\)").Matches(get2);
foreach (Match match in match2)
Line 1,203 ⟶ 1,396:
if (langs.Contains(match.Groups[1].Value))
{
qtdmbr.Add(match.Groups[1].Value, Int32.Parse(match.Groups[2].Value
.Replace(",",string.Empty)
.Replace(".",string.Empty)));
}
}
string[] test =
qtdmbr.OrderByDescending(x => x.Value).Select(x => String.Format("{0,4} {1}", x.Value, x.Key)).ToArray();
int count = 1;
Line 1,213 ⟶ 1,409:
foreach (string i in test)
{
Console.WriteLine("{0,
count++;
}
}
}
</syntaxhighlight>
{{out|Output (as of September 11, 2023)}}
<pre>
7: 1537 Perl
8: 1487 Python
9: 1376 J
10: 1287 C
. . .
</pre>
===Object-oriented solution===
<syntaxhighlight lang="csharp">using System;
Line 2,379 ⟶ 2,579:
=={{header|Java}}==
Tested with Java 1.
<syntaxhighlight lang="java">
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
Line 2,427 ⟶ 2,629:
{
URL url = new
URLConnection rc = url.openConnection();
BufferedReader bfr = new BufferedReader( new InputStreamReader( rc.getInputStream() ) );
Line 2,438 ⟶ 2,638:
while( line != null )
{
line = line.trim().replaceAll( "[\",]", "" );
if ( line.startsWith( "
{
// have a programming language - should look like "
languageName = after( after( line, ':' ), ':' ).trim();
}
else if( line.startsWith( "
{
// number of pages the language has (probably)
String pageCount = after( line, '
if( pageCount.compareTo( "
{
// haven't got "
languageList.add( ( (char) Integer.parseInt( pageCount ) ) + languageName );
languageName = "?";
} // if [pageCount.compareTo( "
}
else if( line.startsWith( "
{
// have an indication of wether there is more data or not
gcmcontinue[0] = after( line, '
} // if various line starts
line = bfr.readLine();
Line 2,478 ⟶ 2,678:
do
{
String path = ( "
+ "&generator=categorymembers"
+ "&gcmtitle=Category:Programming%20Languages"
Line 2,484 ⟶ 2,684:
+ ( gcmcontinue[0].compareTo( "" ) == 0 ? "" : ( "&gcmcontinue=" + gcmcontinue[0] ) )
+ "&prop=categoryinfo"
+ "&format=
);
parseContent( path, gcmcontinue, languageList );
Line 2,510 ⟶ 2,710:
} // for lPos
} // main
} // GetRCLanguages
</syntaxhighlight>
{{out}}
Top 10 languages as at
<pre>
1:
3:
4:
5:
6:
7:
8:
9:
10:
...
</pre>
Line 2,603 ⟶ 2,804:
=={{header|Julia}}==
Uses the API for the language list and page scraping for the example counts for each language.
<syntaxhighlight lang="julia">""" Rosetta code task rosettacode.org/wiki/Rosetta_Code/Rank_languages_by_popularity """
using Dates
using DataFrames
using HTTP
using JSON3
""" Get listing of all tasks and draft tasks with authors and dates created, with the counts as popularity """
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 = copy(LANGPARAMS)
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
return sort!(df, :ExampleCount, rev = true)
end
println("Top 20 Programming Languages on Rosetta Code by Number of Examples, As of: ", now())
println(rosetta_code_language_example_counts()[begin:begin+19, :])
</syntaxhighlight>{{out}}
<pre>
Top 20 Programming Languages on Rosetta Code by Number of Examples, As of: 2022-09-05T14:59:56.316
20×2 DataFrame
Row │ ProgrammingLanguage ExampleCount
│ String Int64
─────┼───────────────────────────────────
1 │ Wren 1569
2 │ Phix 1569
3 │ Julia 1537
4 │ Raku 1517
5 │ Go 1496
6 │ Perl 1460
7 │ Python 1404
8 │ Nim 1402
9 │ J 1275
10 │ C 1210
11 │ Mathematica 1178
12 │ REXX 1149
13 │ Haskell 1139
14 │ Java 1136
15 │ Kotlin 1133
16 │ C++ 1120
17 │ Ruby 1103
18 │ Racket 1089
19 │ FreeBASIC 1069
20 │ Zkl 1011
</pre>
Line 3,137 ⟶ 3,317:
=={{header|Nim}}==
<syntaxhighlight lang="nim">import std/[Algorithm, httpclient, json, re, strformat, strutils
const
LangSite = "http://www.rosettacode.org/
CatSite = "http://www.rosettacode.org/
let regex = re"title=""Category:(.*?)"">.+?</a>.*\((.*) members\)"
Line 3,152 ⟶ 3,332:
proc add(langs: var seq[string]; fromJson: JsonNode) =
for entry in fromJson{"query", "categorymembers"}:
if title.startsWith("Category:"):
langs.add title[9..^1]
var client = newHttpClient()
Line 3,179 ⟶ 3,361:
</syntaxhighlight>
Output:
<pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 896 - Rust
32 893 - Lua
33 849 - PicoLisp
34 843 - F Sharp
35 824 - AutoHotkey
36 818 - Ring
37 793 - Delphi
38 785 - JavaScript
39 778 - Arturo
40 776 - XPL0
...</pre>
Line 3,655 ⟶ 3,847:
</pre>
Note: If MC++ and µC++ are the same, they should/could be added together to get 501 languages.
=={{header|PascalABC.NET}}==
Using web scraping. May 2024.
<syntaxhighlight lang="delphi">
uses System.Net;
begin
ServicePointManager.SecurityProtocol := SecurityProtocolType(3072);
var wc := new WebClient;
wc.Encoding := Encoding.UTF8;
var s := wc.DownloadString('https://rosettacode.org/wiki/Special:Categories?limit=5000');
s.Matches('([^><]+)</a>.+\(([\d,]+) member')
.Select(x -> KV(x.Groups[1].Value,x.Groups[2].Value.Replace(',','').ToInteger))
.Where(x -> not x.Key.StartsWith('Pages'))
.OrderByDescending(pair -> pair.Value).Numerate.Take(10)
.PrintLines(x -> $'Rank: {x[0],3} ({x[1].Value} entries) {x[1].Key}')
end.
</syntaxhighlight>
{{out}}
<pre>
Rank: 1 (1682 entries) Phix
Rank: 2 (1675 entries) Wren
Rank: 3 (1652 entries) Julia
Rank: 4 (1622 entries) Raku
Rank: 5 (1577 entries) Nim
Rank: 6 (1552 entries) Go
Rank: 7 (1548 entries) Perl
Rank: 8 (1531 entries) Python
Rank: 9 (1416 entries) J
Rank: 10 (1343 entries) Java
</pre>
=={{header|Oz}}==
Line 3,719 ⟶ 3,942:
my $api =
MediaWiki::API->new( { api_url => 'http://rosettacode.org/
my @languages;
Line 3,756 ⟶ 3,979:
</syntaxhighlight>
{{out}}
<pre> 1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
...</pre>
Line 4,028 ⟶ 4,251:
$languages = [regex]::matches($response,'title="Category:(.*?)">') | foreach {$_.Groups[1].Value}
$response = [Net.WebClient]::new().DownloadString("http://rosettacode.org/
$response = [regex]::Replace($response,'(\d+),(\d+)','$1$2')
Line 4,060 ⟶ 4,283:
Rank: 10 (960 entries) Zkl
</pre>
===PowerShell: Using MediaWiki API method===
{{trans|Python}}
Line 4,498 ⟶ 4,722:
=={{header|R}}==
<syntaxhighlight lang="rsplus">
library(rvest)
library(dplyr)
options(stringsAsFactors=FALSE)
# getting the required table from the rosetta website
langUrl <- "https://rosettacode.org/wiki/Rosetta_Code/Rank_languages_by_popularity/Full_list"
langs <- read_html(langUrl) %>%
html_nodes(xpath='/html/body/div/div/div[1]/div[3]/main/div[2]/div[3]/div[1]/table') %>%
html_table() %>%
data.frame() %>%
select(c("Rank","TaskEntries","Language"))
# changing the columns to required format
langs$Rank = paste("Rank: ",langs$Rank)
langs$TaskEntries = paste0("(", format(langs$TaskEntries, big.mark = ",")
," entries", ")")
names(langs) <- NULL
langs[1:10,]
</syntaxhighlight>
{{out|Output (as of
<pre>
1 Rank: 1 (1,589 entries) Phix
3 Rank: 3 (1,552 entries) Julia
4 Rank: 4 (1,535 entries) Raku
5 Rank: 5 (1,500 entries) Go
6 Rank: 6 (1,485 entries) Perl
7 Rank: 7 (1,422 entries) Python
8 Rank: 8 (1,402 entries) Nim
9 Rank: 9 (1,293 entries) J
10 Rank: 10 (1,213 entries) C
</pre>
Line 6,339 ⟶ 6,566:
0 Examples - WML
0 Examples - VAX Assembly</pre>
=={{header|Visual Basic .NET}}==
Uses Web scrapping to get the information from the Languages category page - as this now shows the number of pages in each category with the non-task pages separated, there is no need to access other pages.<br>
It may be necessary to set the Security Protocol to Tls12 at the start of the Main method in order to run this - see the "Problem running the C# entry" section on the [[Talk:Rosetta Code/Count examples]] page.
<syntaxhighlight lang="vbnet">
Imports System.Collections
Imports System.Collections.Generic
Imports System.Net
Imports System.Text.RegularExpressions
Module RankLanguagesByPopularity
Private Class LanguageStat
Public name As String
Public count As Integer
Public Sub New(name As String, count As Integer)
Me.name = name
Me.count = count
End Sub
End Class
Private Function CompareLanguages(x As LanguageStat, y As languageStat) As Integer
Dim result As Integer = 0
If x IsNot Nothing Or y IsNot Nothing Then
If x Is Nothing Then
result = -1
ElseIf y Is Nothing
result = 1
Else
result = - x.count.CompareTo(y.count) ' Sort descending count
If result = 0 Then
result = x.name.CompareTo(y.name) ' Sort ascending name
End If
End If
End If
Return result
End Function
Public Sub Main(ByVal args As String())
Dim languages As New List(Of LanguageStat)
Dim basePage As String = "https://rosettacode.org/wiki/Category:Programming_Languages"
Dim nextPage As String = basePage
Dim nextPageEx = New RegEx("<a href=""/wiki/Category:Programming_Languages([?]subcatfrom=[^""]*)""")
Dim languageStatEx = _
New Regex(">([^<]+?)</a>[^<]*<span title=""Contains *[0-9,.]* *[a-z]*, *([0-9,.]*) *page")
Do While nextPage <> ""
Dim wc As New WebClient()
Dim page As String = wc.DownloadString(nextPage)
nextPage = ""
For Each link In nextPageEx.Matches(page)
nextPage = basePage & link.Groups(1).Value
Next link
For Each language In languageStatEx.Matches(page)
Dim lCount As Integer = 0
Dim lName As String = language.Groups(1).Value
Dim countText As String = language.Groups(2).Value.Replace(",", "").Replace(".", "")
If Not Integer.TryParse(countText, lCount)
Console.Out.WriteLine(lName & "Invalid page count: <<" & countText & ">>")
Else
languages.Add(New LanguageStat(lName, lCount))
End If
Next language
Loop
languages.Sort(AddressOf CompareLanguages)
Dim prevCount As Integer = -1
Dim prevPosition As Integer = -1
Dim position As Integer = 0
For Each stat As LanguageStat In languages
position += 1
If stat.count <> prevCount Then
prevPosition = position
prevCount = stat.Count
End If
Console.Out.WriteLine(prevPosition.ToString.PadLeft(4) & ": " &
stat.count.ToString.PadLeft(4) & " " & stat.name)
Next stat
End Sub
End Module
</syntaxhighlight>
{{out}}
Sample output as at 11th September 2023
<pre>
1: 1652 Phix
1: 1652 Wren
3: 1614 Julia
4: 1595 Raku
5: 1576 Nim
6: 1546 Go
7: 1531 Perl
8: 1470 Python
9: 1375 J
10: 1285 C
. . .
</pre>
=={{header|Wren}}==
Line 6,345 ⟶ 6,675:
{{libheader|Wren-fmt}}
An embedded program so we can use libcurl.
<syntaxhighlight lang="
import "./pattern" for Pattern
Line 6,383 ⟶ 6,713:
}
var p1 = Pattern.new("> <a href/=\"//wiki//Category:+1^\"\" title/=\"Category:[+1^\"]\">+1^,, [+1^ ] page")
var p2 = Pattern.new("subcatfrom/=[+1^#/#mw-subcategories]\"")
var
var url = "https://rosettacode.org/w/index.php?title=Category:Programming_Languages"
var subcatfrom = ""
var langs = []
while (true) {
var content = getContent.call(url + subcatfrom)
var matches1 = p1.findAll(content)
for (m in matches1) {
var name = m.capsText[0]
var tasks = Num.fromString(m.capsText[1].replace(",", ""))
langs.add([name, tasks])
}
var m2 = p2.find(content)
if (m2) subcatfrom = "&subcatfrom=%(m2.capsText[0])" else break
}
return langs
}
var langs = findLangs.call()
langs.sort { |a, b| a[1] > b[1] }
System.print("Languages with most examples as at 3 February, 2024:")
var rank = 0
var lastScore = 0
var lastRank = 0
for (i in 0...
var pair =
var eq = " "
rank = i + 1
Line 6,437 ⟶ 6,755:
<br>
We now embed this script in the following C program, build and run.
<syntaxhighlight lang="c">/* gcc
#include <stdio.h>
Line 6,611 ⟶ 6,929:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 6,632 ⟶ 6,950:
Showing just the top thirty.
<pre>
Languages with most
1
3 Julia 1,
4 Raku 1,
5
6
7
8 Python 1,
9
10
11
12
13
14 Mathematica 1,
15
16
17
18
19
20
22
23
24
25
26
27
28
29
30
30 = Rust 940
</pre>
|