Task: Rosetta Code_Count examples





Java: import java.util.ArrayList;
import ScreenScrape;

public class CountProgramExamples {
    private static final String baseURL = "http:
    private static final String rootURL = "http:
        + "api.php?action=query&list=categorymembers"
        + "&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml";
    private static final String taskBegin = "title=\"";
    private static final String taskEnd = "\"";
    private static final String exmplBegin = "<span class=\"tocnumber\">";
    private static final String exmplEnd = "</span>";
    private static final String editBegin = "<span class=\"editsection\">";

    
    public static void main(String[] args) {
        int exTotal = 0;
        try {
            
            ArrayList<String> tasks = new ArrayList<String>();
            ScreenScrape ss = new ScreenScrape();
            String rootPage = ss.read(rootURL);
            while (rootPage.contains(taskBegin)) {
                rootPage = rootPage.substring(rootPage.indexOf(taskBegin)
                    + taskBegin.length());
                String title = rootPage.substring(0, rootPage.indexOf(taskEnd));
                if (!title.contains("Category:")) {
                    tasks.add(title);
                }
                rootPage = rootPage.substring(rootPage.indexOf(taskEnd));
            }
            
            for (String task : tasks) {
                String title = task.replaceAll("&#039;", "'");
                String taskPage = ss.read(baseURL + title.replaceAll(" ", "_"));
                int exSubTot;
                if (taskPage.contains(exmplBegin)) {
                    int startPos = taskPage.lastIndexOf(exmplBegin)
                        + exmplBegin.length();
                    String countStr = taskPage.substring(startPos,
                        taskPage.indexOf(exmplEnd, startPos));
                    exSubTot = Integer
                        .parseInt(countStr.contains(".") ? countStr
                            .substring(0, countStr.indexOf("."))
                            : countStr);
                } else {
                    exSubTot = 0;
                    while (taskPage.contains(editBegin)) {
                        taskPage = taskPage.substring(taskPage
                            .indexOf(editBegin) + editBegin.length());
                        exSubTot++;
                    }
                }
                exTotal += exSubTot;
                System.out.println(title + ": " + exSubTot + " examples.");
            }
            
            System.out.println("\nTotal: " + exTotal + " examples.");
        } catch (Exception e) {
            System.out.println(title);
            System.out.println(startPos + ":"
                + taskPage.indexOf(exmplEnd, startPos));
            System.out.println(taskPage);
            e.printStackTrace(System.out);
        }
    }
}







Ada: with Aws.Client, Aws.Messages, Aws.Response, Aws.Resources, Aws.Url;
with Dom.Readers, Dom.Core, Dom.Core.Documents, Dom.Core.Nodes, Dom.Core.Attrs;
with Input_Sources.Strings, Unicode, Unicode.Ces.Utf8;
with Ada.Strings.Unbounded, Ada.Strings.Fixed, Ada.Text_IO, Ada.Command_Line;
with Ada.Containers.Vectors;

use  Aws.Client, Aws.Messages, Aws.Response, Aws.Resources, Aws.Url;
use Dom.Readers, Dom.Core, Dom.Core.Documents, Dom.Core.Nodes, Dom.Core.Attrs;
use Aws, Ada.Strings.Unbounded, Ada.Strings.Fixed, Input_Sources.Strings;
use Ada.Text_IO, Ada.Command_Line;

procedure Count_Examples is

   package Members_Vectors is new Ada.Containers.Vectors (
      Index_Type => Positive,
      Element_Type => Unbounded_String);
   use Members_Vectors;

   Exemples      : Vector;
   Nbr_Lg, Total : Natural := 0;

   procedure Get_Vector (Category : in String; Mbr_Vector : in out Vector) is
      Reader  : Tree_Reader;
      Doc     : Document;
      List    : Node_List;
      N       : Node;
      A       : Attr;
      Page    : Aws.Response.Data;
      Uri_Xml : constant String :=
         "http://rosettacode.org/mw/api.php?action=query&list=categorymembers"
         &
         "&format=xml&cmlimit=500&cmtitle=Category:";
   begin
      Page := Client.Get (Uri_Xml & Category);
      if Response.Status_Code (Page) not  in Messages.Success then
         raise Client.Connection_Error;
      end if;
      declare
         Xml    : constant String := Message_Body (Page);
         Source : String_Input;
      begin
         Open
           (Xml'Unrestricted_Access,
            Unicode.Ces.Utf8.Utf8_Encoding,
            Source);
         Parse (Reader, Source);
         Close (Source);
      end;
      Doc  := Get_Tree (Reader);
      List := Get_Elements_By_Tag_Name (Doc, "cm");
      for Index in 1 .. Length (List) loop
         N := Item (List, Index - 1);
         A := Get_Named_Item (Attributes (N), "title");
         Append (Mbr_Vector, To_Unbounded_String (Value (A)));
      end loop;
      Free (List);
      Free (Reader);
   end Get_Vector;

   function Scan_Page (Title : String) return Natural is
      Page                      : Aws.Response.Data;
      File                      : Aws.Resources.File_Type;
      Buffer                    : String (1 .. 1024);
      Languages, Position, Last : Natural := 0;
   begin
      Page :=
         Client.Get
           ("http://rosettacode.org/mw/index.php?title=" &
            Aws.Url.Encode (Title) &
            "&action=raw");
      Response.Message_Body (Page, File);
      while not End_Of_File (File) loop
         Resources.Get_Line (File, Buffer, Last);
         Position :=
            Index
              (Source  => Buffer (Buffer'First .. Last),
               Pattern => "=={{header|");
         if Position > 0 then
            Languages := Languages + 1;
         end if;
      end loop;
      Close (File);
      return Languages;
   end Scan_Page;

begin
   Get_Vector ("Programming_Tasks", Exemples);

   for I in First_Index (Exemples) .. Last_Index (Exemples) loop
      declare
         Title : constant String :=
            To_String (Members_Vectors.Element (Exemples, I));
      begin
         Nbr_Lg := Scan_Page (Title);
         Total  := Total + Nbr_Lg;
         Put_Line (Title & " :" & Integer'Image (Nbr_Lg) & " exemples.");
      end;
   end loop;

   Put_Line ("Total :" & Integer'Image (Total) & " exemples.");
end Count_Examples;