Rosetta Code/Rank languages by popularity: Difference between revisions

 
(One intermediate revision by one other user not shown)
Line 43:
 
with Ada.Containers.Ordered_Sets;
with Ada.Strings.Unbounded.Less_Case_Insensitive;
with Ada.Characters.Handling; use Ada.Characters.Handling;
 
Line 59:
type A_Language_Count is
record
Count : IntegerNatural := 0;
Language : Unbounded_String;
end record;
 
overriding
function "=" (L, R : A_Language_Count) return Boolean is
begin
Line 75 ⟶ 74:
or else (L.Count = R.Count
and then Less_Case_Insensitive
(Left => To_String (L.Language),
Right => To_String (R.Language)));
end "<";
 
Line 86 ⟶ 85:
procedure Find_Counts (S : String) is
 
function Strip_Character (S : String; C : StringCharacter) return String is
Comma_AtS_Copy_Str : constantString Natural(1 := Ada.Strings.Fixed.Index (S, C'Length);
S_Copy_Index : Natural := 0;
begin
iffor Comma_AtI =in 0S'Range thenloop
returnif S; (I) /= C then
S_Copy_Index := S_Copy_Index + 1;
else
return Strip_Character (S S_Copy_Str (S'FirstS_Copy_Index) ..:= Comma_AtS - 1(I);
else end if;
& S (Comma_At + 1 .. S'Last), C);
end ifloop;
 
return S_Copy_Str (S_Copy_Str'First .. S_Copy_Index);
end Strip_Character;
 
Line 101 ⟶ 103:
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",
Line 150 ⟶ 152:
];
begin
for ICategory inof Categories_To_Ignore'Range loop
declare
Category_At : constant Natural :=
Index (+To_Lower (L),
To_Lower (To_String (Category)));
To_String (Categories_To_Ignore (I))));
begin
if Category_At /= 0 then
Line 166 ⟶ 167:
end Ignore_Category;
 
Title_Str : constant String := "title=""Category:";
End_A_Tag_Str : constant String := "</a>";
Space_Paren_Str : constant String := " (";
 
Line 192 ⟶ 193:
 
Count : constant Natural :=
Natural'Value (Strip_Character (
S (Space_Paren_At + Space_Paren_Str'Length Strip_Character (
.. Space_At - 1), S (Space_Paren_At +
",")); Space_Paren_Str'Length
& S (Comma_At + 1 .. S'Last),Space_At C- 1);,
To_String (Categories_To_Ignore (I))','));
begin
if Closing_Bracket_At /= 0
Line 233 ⟶ 236:
Http_Source : constant AWS.Response.Data :=
AWS.Client.Get ("http://rosettacode.org/w/index.php?" &
"title=Special:Categories&limit=5000",
, Follow_Redirection => True);
Status : Status_Code;
begin
Line 255 ⟶ 258:
Put_Line ("Process complete.");
end Rank_Languages_By_Popularity;
 
</syntaxhighlight>
{{out|Sample output}}
Line 2,577 ⟶ 2,579:
 
=={{header|Java}}==
Tested with Java 1.722. Uses the api.<br/>
<syntaxhighlight lang="java">import java.net.URL;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
Line 2,625 ⟶ 2,629:
{
 
URL url = new URLURI( path ).toURL();
URLConnection rc = url.openConnection();
// Rosetta Code objects to the default Java user agant so use a blank one
rc.setRequestProperty( "User-Agent", "" );
BufferedReader bfr = new BufferedReader( new InputStreamReader( rc.getInputStream() ) );
Line 2,636 ⟶ 2,638:
while( line != null )
{
line = line.trim().replaceAll( "[\",]", "" );
if ( line.startsWith( "[title]" ) )
{
// have a programming language - should look like "[title] =>: Category:languageName"
languageName = after( after( line, ':' ), ':' ).trim();
}
else if( line.startsWith( "[pages]" ) )
{
// number of pages the language has (probably)
String pageCount = after( line, '>:' ).trim();
if( pageCount.compareTo( "Array{" ) != 0 )
{
// haven't got "[pages]: => Array{" - must be a number of pages
languageList.add( ( (char) Integer.parseInt( pageCount ) ) + languageName );
languageName = "?";
} // if [pageCount.compareTo( "Array{" ) != 0
}
else if( line.startsWith( "[gcmcontinue]" ) )
{
// have an indication of wether there is more data or not
gcmcontinue[0] = after( line, '>:' ).trim().replaceAll( "[|]", "%7C" );
} // if various line starts
line = bfr.readLine();
Line 2,676 ⟶ 2,678:
do
{
String path = ( "httphttps://www.rosettacode.org/mww/api.php?action=query"
+ "&generator=categorymembers"
+ "&gcmtitle=Category:Programming%20Languages"
Line 2,682 ⟶ 2,684:
+ ( gcmcontinue[0].compareTo( "" ) == 0 ? "" : ( "&gcmcontinue=" + gcmcontinue[0] ) )
+ "&prop=categoryinfo"
+ "&format=txtjsonfm"
);
parseContent( path, gcmcontinue, languageList );
Line 2,708 ⟶ 2,710:
} // for lPos
} // main
} // GetRCLanguages</syntaxhighlight>
</syntaxhighlight>
{{out}}
Top 10 languages as at 27th1st AugustJune 20152024
<pre>
1: 8831675: TclPhix
21: 8751675: RacketWren
3: 8371650: PythonJulia
4: 7991620: JRaku
5: 7721576: RubyNim
6: 7631549: Perl 6Go
7: 7561542: CPerl
8: 7421514: GoPython
9: 7371413: DJ
10: 7071346: PerlJava
...
</pre>
9

edits