Rosetta Code/Rank languages by popularity: Difference between revisions
Content added Content deleted
JoseAlexis (talk | contribs) |
|||
Line 34: | Line 34: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{libheader|AWS}} |
{{libheader|AWS}} |
||
<syntaxhighlight lang="ada"> |
<syntaxhighlight lang="ada">pragma Ada_2022; |
||
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; |
|||
with Ada.Strings.Fixed; use Ada.Strings.Fixed; |
with Ada.Strings.Fixed; use Ada.Strings.Fixed; |
||
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; |
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; |
||
Line 41: | Line 43: | ||
with Ada.Containers.Ordered_Sets; |
with Ada.Containers.Ordered_Sets; |
||
with Ada.Strings.Less_Case_Insensitive; |
with Ada.Strings.Less_Case_Insensitive; |
||
with Ada.Characters.Handling; use Ada.Characters.Handling; |
|||
with AWS.Client; |
with AWS.Client; use AWS.Client; |
||
with AWS.Messages; use AWS.Messages; |
|||
with AWS.Response; |
with AWS.Response; |
||
procedure |
procedure Rank_Languages_By_Popularity is |
||
use Ada.Strings; |
use Ada.Strings; |
||
function "+" (S : String) return Unbounded_String |
function "+" (S : String) return Unbounded_String |
||
renames To_Unbounded_String; |
|||
type A_Language_Count is |
type A_Language_Count is |
||
Line 57: | Line 62: | ||
end record; |
end record; |
||
overriding |
|||
function "=" (L, R : A_Language_Count) return Boolean is |
function "=" (L, R : A_Language_Count) return Boolean is |
||
begin |
begin |
||
Line 64: | Line 70: | ||
function "<" (L, R : A_Language_Count) return Boolean is |
function "<" (L, R : A_Language_Count) return Boolean is |
||
begin |
begin |
||
-- Sort by 'Count' and then by Language name |
-- Sort by 'Count' and then by Language name |
||
return L.Count < R.Count |
return L.Count < R.Count |
||
or else (L.Count = R.Count |
or else (L.Count = R.Count |
||
and then Less_Case_Insensitive |
and then Less_Case_Insensitive |
||
(Left => To_String (L.Language), |
|||
Right => To_String (R.Language))); |
|||
end "<"; |
end "<"; |
||
Line 77: | Line 84: | ||
procedure Find_Counts (S : String) is |
procedure Find_Counts (S : String) is |
||
Title_Str : constant String := "title=""Category:"; |
|||
End_A_Str : constant String := "</a> ("; |
|||
function Strip_Character (S : String; C : String) return String is |
|||
Comma_At : constant Natural := Ada.Strings.Fixed.Index (S, C); |
|||
begin |
|||
if Comma_At = 0 then |
|||
return S; |
|||
else |
|||
return Strip_Character (S (S'First .. Comma_At - 1) |
|||
& S (Comma_At + 1 .. S'Last), C); |
|||
end if; |
|||
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 I in Categories_To_Ignore'Range loop |
|||
declare |
|||
Category_At : constant Natural := |
|||
Index (+To_Lower (L), |
|||
To_Lower ( |
|||
To_String (Categories_To_Ignore (I)))); |
|||
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 |
begin |
||
if Title_At /= 0 then |
if Title_At /= 0 then |
||
declare |
declare |
||
Closing_Bracket_At : constant Natural := |
|||
Index (S (Title_At + Title_Str'Length .. S'Last), ">"); |
|||
Space_At : constant Natural := Index (S (End_A_At + End_A_Str'Length .. S'Last), " "); |
|||
End_A_Tag_At : constant Natural := |
|||
Index (S (Closing_Bracket_At + 1 .. S'Last), End_A_Tag_Str); |
|||
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 |
begin |
||
if |
if Closing_Bracket_At /= 0 |
||
and then End_A_Tag_At /= 0 |
|||
and then Space_Paren_At /= 0 |
|||
and then Space_At /= 0 |
|||
then |
|||
begin |
begin |
||
if Ignore_Category (Language) = False then |
|||
Counts.Insert (New_Item => (Count, +Language)); |
|||
end if; |
|||
exception |
exception |
||
when Constraint_Error => |
when Constraint_Error => |
||
Put_Line (Standard_Error, "Warning: repeated language: " & |
Put_Line (Standard_Error, "Warning: repeated language: " & |
||
Language); |
|||
-- Ignore repeated results. |
|||
null; |
null; |
||
end; |
end; |
||
end if; |
end if; |
||
-- Recursively parse the string for languages and counts |
-- Recursively parse the string for languages and counts |
||
Find_Counts (S (Space_At + 1 .. S'Last)); |
Find_Counts (S (Space_At + 1 .. S'Last)); |
||
end; |
end; |
||
Line 116: | Line 229: | ||
Place := Place + 1; |
Place := Place + 1; |
||
end Display; |
end Display; |
||
Http_Source : constant AWS.Response.Data := |
Http_Source : constant AWS.Response.Data := |
||
AWS.Client.Get ("http://rosettacode.org/ |
AWS.Client.Get ("http://rosettacode.org/w/index.php?" & |
||
"title=Special:Categories&limit=5000" |
|||
, Follow_Redirection => True); |
|||
Status : Status_Code; |
|||
begin |
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)); |
Find_Counts (AWS.Response.Message_Body (Http_Source)); |
||
Put_Line ("Displaying categories..."); |
|||
Counts.Reverse_Iterate (Display'Access); |
Counts.Reverse_Iterate (Display'Access); |
||
end Test; |
|||
Put_Line ("Process complete."); |
|||
end Rank_Languages_By_Popularity; |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||