tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
11
Task/Rosetta-Code-Count-examples/0DESCRIPTION
Normal file
11
Task/Rosetta-Code-Count-examples/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
Find the total number of programming examples for each [[:Category:Programming Tasks|task]] and the total for all tasks.
|
||||
|
||||
Essentially, count the number of occurrences of <tt>=={{header|</tt> on each task page.
|
||||
|
||||
Output:
|
||||
|
||||
<pre>100 doors: 20 examples.
|
||||
99 Bottles of Beer: 29 examples.
|
||||
Abstract type: 10 examples.
|
||||
|
||||
Total: X examples.</pre>
|
||||
2
Task/Rosetta-Code-Count-examples/1META.yaml
Normal file
2
Task/Rosetta-Code-Count-examples/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Rosetta Code related
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
UrlDownloadToFile
|
||||
, http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml
|
||||
, tasks.xml
|
||||
FileRead, tasks, tasks.xml
|
||||
pos = 0
|
||||
quote = " ; "
|
||||
regtitle := "<cm.*?title=" . quote . "(.*?)" . quote
|
||||
While, pos := RegExMatch(tasks, regtitle, title, pos + 1)
|
||||
{
|
||||
UrlDownloadToFile
|
||||
, % "http://www.rosettacode.org/w/index.php?title=" . title1 . "&action=raw"
|
||||
, task.xml
|
||||
FileRead, task, task.xml
|
||||
RegExReplace(task, "\{\{header\|", "", count)
|
||||
current := title1 . ": " . count . " examples.`n"
|
||||
output .= current
|
||||
TrayTip, current, % current
|
||||
}
|
||||
MsgBox % output
|
||||
Return
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
VDU 23,22,640;512;8,16,16,128+8 : REM Enable UTF-8 support
|
||||
|
||||
SYS "LoadLibrary", "URLMON.DLL" TO urlmon%
|
||||
SYS "GetProcAddress", urlmon%, "URLDownloadToFileA" TO UDTF
|
||||
special$ = "+()'"
|
||||
|
||||
url$ = "http://www.rosettacode.org/w/api.php?action=query" + \
|
||||
\ "&list=categorymembers&cmtitle=Category:Programming_Tasks" + \
|
||||
\ "&cmlimit=500&format=xml"
|
||||
file$ = @tmp$ + "tasks.xml"
|
||||
SYS UDTF, 0, url$, file$, 0, 0 TO fail%
|
||||
IF fail% ERROR 100, "File download failed (tasks)"
|
||||
|
||||
Total% = 0
|
||||
file% = OPENIN(file$)
|
||||
WHILE NOT EOF#file%
|
||||
a$ = GET$#file%
|
||||
i% = 0
|
||||
REPEAT
|
||||
i% = INSTR(a$, "title=", i%+1)
|
||||
IF i% THEN
|
||||
j% = INSTR(a$, ">", i%)
|
||||
title$ = MID$(a$, i%+7, j%-i%-10)
|
||||
REM Replace HTML codes:
|
||||
REPEAT
|
||||
k% = INSTR(title$, "&")
|
||||
IF k% THEN
|
||||
l% = INSTR(title$, ";", k%)
|
||||
title$ = LEFT$(title$,k%-1) + \
|
||||
\ FNhtml(MID$(title$,k%,l%-k%+1)) + MID$(title$,l%+1)
|
||||
ENDIF
|
||||
UNTIL k% = 0
|
||||
t$ = title$
|
||||
REM Substitute characters not allowed in a URL:
|
||||
FOR s% = 1 TO LEN(special$)
|
||||
REPEAT
|
||||
s$ = MID$(special$, s%, 1)
|
||||
k% = INSTR(t$, s$)
|
||||
IF k% t$ = LEFT$(t$,k%-1) + "%" + STR$~ASCs$ + MID$(t$,k%+1)
|
||||
UNTIL k% = 0
|
||||
NEXT
|
||||
url$ = "http://www.rosettacode.org/w/index.php?title=" + t$ + \
|
||||
\ "&action=raw"
|
||||
file$ = @tmp$ + "title.htm"
|
||||
SYS UDTF, 0, url$, file$, 0, 0 TO fail%
|
||||
IF fail% ERROR 100, "File download failed " + t$
|
||||
examples% = 0
|
||||
task% = OPENIN(file$)
|
||||
WHILE NOT EOF#task%
|
||||
IF INSTR(GET$#task%, "=={{header|") examples% += 1
|
||||
ENDWHILE
|
||||
CLOSE #task%
|
||||
Total% += examples%
|
||||
PRINT title$ ": " ; examples% " examples."
|
||||
ENDIF
|
||||
UNTIL i% = 0
|
||||
i% = INSTR(a$, "cmcontinue=")
|
||||
IF i% THEN
|
||||
CLOSE #file%
|
||||
j% = INSTR(a$, """", i%+1)
|
||||
k% = INSTR(a$, """", j%+1)
|
||||
url$ = "http://www.rosettacode.org/w/api.php?action=query" + \
|
||||
\ "&list=categorymembers&cmtitle=Category:Programming_Tasks" + \
|
||||
\ "&cmlimit=500&format=xml&cmcontinue=" + MID$(a$,j%+1,k%-j%)
|
||||
REPEAT
|
||||
i% = INSTR(url$, "|")
|
||||
IF i% url$ = LEFT$(url$,i%-1) + "%7C" + MID$(url$,i%+1)
|
||||
UNTIL i% = 0
|
||||
file$ = @tmp$ + "tasks.xml"
|
||||
SYS UDTF, 0, url$, file$, 0, 0 TO fail%
|
||||
IF fail% ERROR 100, "File download failed (continue)"
|
||||
file% = OPENIN(file$)
|
||||
ENDIF
|
||||
ENDWHILE
|
||||
CLOSE #file%
|
||||
PRINT ' "Total: " ; Total% " examples."
|
||||
END
|
||||
|
||||
DEF FNhtml(h$)
|
||||
IF LEFT$(h$,2) = "&#" THEN = CHR$(VALMID$(h$,3))
|
||||
CASE h$ OF
|
||||
WHEN """: = """"
|
||||
ENDCASE
|
||||
= h$
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
(ns count-examples
|
||||
(:import [java.net URLEncoder])
|
||||
(:use [clojure.contrib.http.agent :only (http-agent string)]
|
||||
[clojure.contrib.json :only (read-json)]
|
||||
[clojure.contrib.string :only (join)]))
|
||||
|
||||
(defn url-encode [v] (URLEncoder/encode (str v) "utf-8"))
|
||||
|
||||
(defn rosettacode-get [path params]
|
||||
(let [param-string (join "&" (for [[n v] params] (str (name n) "=" (url-encode v))))]
|
||||
(string (http-agent (format "http://www.rosettacode.org/w/%s?%s" path param-string)))))
|
||||
|
||||
(defn rosettacode-query [params]
|
||||
(read-json (rosettacode-get "api.php" (merge {:action "query" :format "json"} params))))
|
||||
|
||||
(defn list-cm
|
||||
([params] (list-cm params nil))
|
||||
([params continue]
|
||||
(let [cm-params (merge {:list "categorymembers"} params (or continue {}))
|
||||
result (rosettacode-query cm-params)]
|
||||
(concat (-> result (:query) (:categorymembers))
|
||||
(if-let [cmcontinue (-> result (:query-continue) (:categorymembers))]
|
||||
(list-cm params cmcontinue))))))
|
||||
|
||||
(defn programming-tasks []
|
||||
(let [result (list-cm {:cmtitle "Category:Programming_Tasks" :cmlimit 50})]
|
||||
(map #(:title %) result)))
|
||||
|
||||
(defn task-count [task]
|
||||
[task (count
|
||||
(re-seq #"==\{\{header"
|
||||
(rosettacode-get "index.php" {:action "raw" :title task})))])
|
||||
|
||||
(defn print-result []
|
||||
(let [task-counts (map task-count (programming-tasks))]
|
||||
(doseq [[task count] task-counts]
|
||||
(println (str task ":") count)
|
||||
(flush))
|
||||
(println "Total: " (reduce #(+ %1 (second %2)) 0 task-counts))))
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
count-examples> (print-result)
|
||||
100 doors: 73
|
||||
24 game: 18
|
||||
24 game/Solve: 14
|
||||
99 Bottles of Beer: 89
|
||||
Abstract type: 27
|
||||
Accumulator factory: 23
|
||||
Ackermann function: 73
|
||||
Active Directory/Connect: 4
|
||||
Active Directory/Search for a user: 3
|
||||
Active object: 14
|
||||
Add a variable to a class instance at runtime: 21
|
||||
Address of a variable: 20
|
||||
...
|
||||
Total: 11216
|
||||
nil
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
import tango.io.Stdout;
|
||||
import tango.net.http.HttpClient;
|
||||
import tango.net.http.HttpHeaders;
|
||||
import tango.text.xml.Document;
|
||||
import tango.text.Util;
|
||||
|
||||
alias HttpHeader.ContentLength CL;
|
||||
|
||||
auto url = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml";
|
||||
void main()
|
||||
{
|
||||
auto client = new HttpClient (HttpClient.Get, url);
|
||||
client.open();
|
||||
char[] mainData, tmp;
|
||||
int total, i;
|
||||
|
||||
void cat(void[] content) { tmp ~= cast(char[]) content; }
|
||||
|
||||
if (client.isResponseOK) {
|
||||
client.read(&cat, client.getResponseHeaders.getInt(CL));
|
||||
mainData = tmp;
|
||||
tmp = null;
|
||||
|
||||
auto doc = new Document!(char);
|
||||
doc.parse(mainData);
|
||||
foreach (n; doc.query.descendant("cm").attribute("title")) {
|
||||
auto subClient = new HttpClient(HttpClient.Get,
|
||||
"http://www.rosettacode.org/w/index.php?title=" ~
|
||||
replace(n.value.dup, ' ', '_') ~ "&action=raw");
|
||||
subClient.open();
|
||||
if (! subClient.isResponseOK) {
|
||||
Stderr (client.getResponse);
|
||||
break;
|
||||
}
|
||||
subClient.read(&cat, subClient.getResponseHeaders.getInt(CL));
|
||||
foreach (segment; patterns(cast(char[])tmp, "=={{header|")) i++;
|
||||
--i;
|
||||
if (i) --i;
|
||||
Stdout.formatln ("{0,-40} - {}", n.value, i);
|
||||
total += i;
|
||||
tmp = null;
|
||||
i = 0;
|
||||
}
|
||||
Stdout("total examples: ", total).newline;
|
||||
} else {
|
||||
Stderr (client.getResponse);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
package com.eglexamples.client;
|
||||
|
||||
import org.eclipse.edt.rui.widgets.*;
|
||||
|
||||
handler RosettaCodeHandler type RUIhandler{initialUI =[ui], title = "Rosetta Code Tasks and Counts"}
|
||||
|
||||
ui GridLayout{columns = 3, rows = 4, cellPadding = 4, children = [ b1, dg1, l1, l2, l3, l4 ]};
|
||||
|
||||
b1 Button{ layoutData = new GridLayoutData{ row = 1, column = 1 }, text = "Go!", onClick ::= b1_onClick };
|
||||
l1 TextLabel{ layoutData = new GridLayoutData{ row = 1, column = 2 }, text = "Total Tasks:" };
|
||||
l2 TextLabel{ layoutData = new GridLayoutData{ row = 1, column = 3 }, text = "0" };
|
||||
|
||||
l3 TextLabel{ layoutData = new GridLayoutData{ row = 2, column = 2 }, text = "Total Implementations:" };
|
||||
l4 TextLabel{ layoutData = new GridLayoutData{ row = 2, column = 3 }, text = "0" };
|
||||
|
||||
dg1 DataGrid{ layoutData = new GridLayoutData{ row = 3, column = 1, horizontalSpan = 3 },
|
||||
pageSize = 10, showScrollbar = true,
|
||||
columns = [ new DataGridColumn{name = "title", displayName = "Task", width=220},
|
||||
new DataGridColumn{name = "count", displayName = "Count", width=100} ] };
|
||||
|
||||
cmcontinue string?;
|
||||
title string?;
|
||||
allTasks Task[];
|
||||
|
||||
restBindingTasks IHttp? = new HttpRest{
|
||||
restType = eglx.rest.ServiceType.TrueRest,
|
||||
request.uri = "http://rosettacode.org/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=1&format=json"};
|
||||
|
||||
restBindingPageDetail IHttp? = new HttpRest{
|
||||
restType = eglx.rest.ServiceType.TrueRest,
|
||||
request.uri = "http://rosettacode.org/mw/index.php"};
|
||||
|
||||
function b1_onClick(event Event in)
|
||||
call ProxyFunctions.listTasks("") using restBindingTasks
|
||||
returning to listTasksCallBack onException exceptionHandler;
|
||||
end
|
||||
|
||||
function listTasksCallBack(retResult RosettaCodeJSON in)
|
||||
title = retResult.query.categorymembers[1].title;
|
||||
cmcontinue = retResult.queryContinue.categorymembers.cmcontinue;
|
||||
|
||||
call ProxyFunctions.fetchPageDetail(title) using restBindingPageDetail
|
||||
returning to pageDetailCallBack onException exceptionHandler;
|
||||
end
|
||||
|
||||
function pageDetailCallBack(pageResults string in)
|
||||
count int = countSubstring("=={{header", pageResults);
|
||||
allTasks.appendElement(new Task { title = title, count = count });
|
||||
l2.text = l2.text as int + 1;
|
||||
l4.text = l4.text as int + count;
|
||||
|
||||
if(cmcontinue != null)
|
||||
call ProxyFunctions.listTasks(cmcontinue) using restBindingTasks
|
||||
returning to listTasksCallBack onException exceptionHandler;
|
||||
else
|
||||
dg1.data = allTasks as any[];
|
||||
end
|
||||
end
|
||||
|
||||
function countSubstring(substr string in, str string in) returns(int)
|
||||
if(str.length() > 0 and substr.length() > 0)
|
||||
return (str.length() - str.replaceStr(subStr, "").length()) / subStr.length();
|
||||
else
|
||||
return 0;
|
||||
end
|
||||
end
|
||||
|
||||
function exceptionHandler(exp AnyException in)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
record Task
|
||||
title string;
|
||||
count int;
|
||||
end
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
package com.eglexamples.client;
|
||||
|
||||
library ProxyFunctions
|
||||
|
||||
function listTasks(continueLocation String in) returns (RosettaCodeJSON) {
|
||||
@Rest{method = _GET, uriTemplate = "&cmcontinue={continueLocation}",
|
||||
requestFormat = None, responseFormat = JSON}
|
||||
}
|
||||
end
|
||||
|
||||
function fetchPageDetail(title String in) returns (String) {
|
||||
@Rest{method = _GET, uriTemplate = "?title={title}&action=raw",
|
||||
requestFormat = None, responseFormat = None}
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
record RosettaCodeJSON
|
||||
query Query;
|
||||
queryContinue QueryContinue{JSONName = "query-continue"};
|
||||
end
|
||||
|
||||
record Query
|
||||
categorymembers Categorymembers[];
|
||||
end
|
||||
|
||||
record Categorymembers
|
||||
cmcontinue string?;
|
||||
pageid int?;
|
||||
ns int?;
|
||||
title string?;
|
||||
end
|
||||
|
||||
record QueryContinue
|
||||
categorymembers Categorymembers;
|
||||
end
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
-module(rosseta_examples).
|
||||
-include_lib("xmerl/include/xmerl.hrl").
|
||||
|
||||
-export([main/0]).
|
||||
|
||||
main() ->
|
||||
application:start(inets),
|
||||
Titles = read_titles(empty),
|
||||
Result = lists:foldl(fun(Title,Acc) -> Acc + calculate_one(Title) end, 0, Titles),
|
||||
io:format("Total: ~p examples.\n",[Result]),
|
||||
application:stop(inets).
|
||||
|
||||
read_titles(CurrentContinue) ->
|
||||
URL0 = "http://rosettacode.org/mw/api.php?" ++
|
||||
"action=query&list=categorymembers&cmtitle=Category:Programming_Tasks" ++
|
||||
"&cmlimit=500&format=xml",
|
||||
URL =
|
||||
case CurrentContinue of
|
||||
empty -> URL0;
|
||||
_ -> URL0 ++ "&cmcontinue=" ++ CurrentContinue
|
||||
end,
|
||||
{ok,Answer} = httpc:request(URL),
|
||||
{Document,_} = xmerl_scan:string(lists:last(tuple_to_list(Answer))),
|
||||
Continue =
|
||||
[Value || #xmlAttribute{value = Value} <- xmerl_xpath:string("//@cmcontinue", Document)],
|
||||
Titles =
|
||||
[Value || #xmlAttribute{value = Value} <- xmerl_xpath:string("//@title", Document)],
|
||||
case Continue of
|
||||
[]->
|
||||
Titles;
|
||||
[ContValue | _] ->
|
||||
Titles ++ read_titles(ContValue)
|
||||
end.
|
||||
|
||||
calculate_one(Title0) ->
|
||||
Title = replace_chars(Title0),
|
||||
URL = "http://www.rosettacode.org/w/index.php?title=" ++
|
||||
Title ++ "&action=raw",
|
||||
case httpc:request(URL) of
|
||||
{ok,Result} ->
|
||||
{match,Found} =
|
||||
re:run(lists:last(tuple_to_list(Result)), "\n=={{header(|)", [global]),
|
||||
io:format("~ts: ~p examples.\n",[Title0,length(Found)]),
|
||||
length(Found);
|
||||
{error,socket_closed_remotely} ->
|
||||
io:format("Socket closed remotely. Retry.\n"),
|
||||
calculate_one(Title0)
|
||||
end.
|
||||
|
||||
replace_chars(String) ->
|
||||
replace_chars(String,[]).
|
||||
|
||||
replace_chars([$ | T],Acc) ->
|
||||
replace_chars(T, [$_| Acc]);
|
||||
replace_chars([$+| T],Acc) ->
|
||||
replace_chars(T, lists:reverse("%2B") ++ Acc);
|
||||
replace_chars([8211| T],Acc) ->
|
||||
replace_chars(T, lists:reverse("%E2%80%93") ++ Acc);
|
||||
replace_chars([Other| T],Acc) ->
|
||||
replace_chars(T, [Other| Acc]);
|
||||
replace_chars([],Acc) ->
|
||||
lists:reverse(Acc).
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func req(u string, foundCm func(string)) string {
|
||||
resp, err := http.Get(u)
|
||||
if err != nil {
|
||||
fmt.Println(err) // connection or request fail
|
||||
return ""
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
for p := xml.NewDecoder(resp.Body); ; {
|
||||
t, err := p.RawToken()
|
||||
switch s, ok := t.(xml.StartElement); {
|
||||
case err == io.EOF:
|
||||
return ""
|
||||
case err != nil:
|
||||
fmt.Println(err)
|
||||
return ""
|
||||
case !ok:
|
||||
continue
|
||||
case s.Name.Local == "cm":
|
||||
for _, a := range s.Attr {
|
||||
if a.Name.Local == "title" {
|
||||
foundCm(a.Value)
|
||||
}
|
||||
}
|
||||
case s.Name.Local == "categorymembers" && len(s.Attr) > 0 &&
|
||||
s.Attr[0].Name.Local == "cmcontinue":
|
||||
return url.QueryEscape(s.Attr[0].Value)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
taskQuery := "http://rosettacode.org/mw/api.php?action=query" +
|
||||
"&format=xml&list=categorymembers&cmlimit=500" +
|
||||
"&cmtitle=Category:Programming_Tasks"
|
||||
continueAt := req(taskQuery, count)
|
||||
for continueAt > "" {
|
||||
continueAt = req(taskQuery+"&cmcontinue="+continueAt, count)
|
||||
}
|
||||
fmt.Printf("Total: %d examples.\n", total)
|
||||
}
|
||||
|
||||
var marker = []byte("=={{header|")
|
||||
var total int
|
||||
|
||||
func count(cm string) {
|
||||
taskFmt := "http://rosettacode.org/mw/index.php?title=%s&action=raw"
|
||||
taskEsc := url.QueryEscape(strings.Replace(cm, " ", "_", -1))
|
||||
resp, err := http.Get(fmt.Sprintf(taskFmt, taskEsc))
|
||||
var page []byte
|
||||
if err == nil {
|
||||
page, err = ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
examples := bytes.Count(page, marker)
|
||||
fmt.Printf("%s: %d\n", cm, examples)
|
||||
total += examples
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
import Network.Browser
|
||||
import Network.HTTP
|
||||
import Network.URI
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
import Text.XML.Light
|
||||
import Control.Arrow
|
||||
|
||||
justifyR w = foldl ((.return).(++).tail) (replicate w ' ')
|
||||
showFormatted t n = t ++ ": " ++ justifyR 4 (show n)
|
||||
|
||||
getRespons url = do
|
||||
rsp <- Network.Browser.browse $ do
|
||||
setAllowRedirects True
|
||||
setOutHandler $ const (return ()) -- quiet
|
||||
request $ getRequest url
|
||||
return $ rspBody $ snd rsp
|
||||
|
||||
getNumbOfExampels p = do
|
||||
let pg = intercalate "_" $ words p
|
||||
rsp <- getRespons $ "http://www.rosettacode.org/w/index.php?title=" ++ pg ++ "&action=raw"
|
||||
let taskPage = rsp
|
||||
countEx = length $ filter (=="=={{header|") $ takeWhile(not.null) $ unfoldr (Just. (take 11 &&& drop 1)) taskPage
|
||||
return countEx
|
||||
|
||||
progTaskExamples = do
|
||||
rsp <- getRespons "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"
|
||||
|
||||
let xmls = onlyElems $ parseXML $ rsp
|
||||
tasks = concatMap (map (fromJust.findAttr (unqual "title")). filterElementsName (== unqual "cm")) xmls
|
||||
|
||||
taskxx <- mapM getNumbOfExampels tasks
|
||||
let ns = taskxx
|
||||
tot = sum ns
|
||||
|
||||
mapM_ putStrLn $ zipWith showFormatted tasks ns
|
||||
putStrLn $ ("Total: " ++) $ show tot
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
*Main> progTaskExamples
|
||||
100 doors: 56
|
||||
24 game: 11
|
||||
24 game Player: 9
|
||||
99 Bottles of Beer: 73
|
||||
Abstract type: 23
|
||||
Ackermann Function: 61
|
||||
Active object: 9
|
||||
...
|
||||
Total: 9156
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
$define RCINDEX "http://rosettacode.org/mw/api.php?format=xml&action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500"
|
||||
$define RCTASK "http://rosettacode.org/mw/index.php?action=raw&title="
|
||||
$define RCUA "User-Agent: Unicon Rosetta 0.1"
|
||||
$define RCXUA "X-Unicon: http://unicon.org/"
|
||||
$define TASKTOT "* Total Tasks *"
|
||||
$define TOTTOT "* Total Headers*"
|
||||
|
||||
link strings
|
||||
link hexcvt
|
||||
|
||||
procedure main(A) # simple single threaded read all at once implementation
|
||||
|
||||
index := ReadURL(RCINDEX) # 1. read the index
|
||||
|
||||
pages := []
|
||||
index ? while tab(find("<cm ") & find(s :="title=\"")+*s) do
|
||||
put(pages,tab(find("\""))) # 2. extract the pages
|
||||
|
||||
Tasks := table(0)
|
||||
every p := !pages do { # 3. process each page
|
||||
|
||||
if p << A[1] then next # for tests on small #s
|
||||
|
||||
page := ReadURL(url := RCTASK||CleanURI(p))
|
||||
Tasks[TASKTOT] +:= 1 # . count pages (tasks)
|
||||
every find("=={{header|",page) do { # . count headers
|
||||
Tasks[p] +:= 1
|
||||
Tasks[TOTTOT] +:= 1
|
||||
}
|
||||
}
|
||||
|
||||
every insert(O := set(),key(Tasks)) # 4. extract & sort keys
|
||||
O := put(sort(O--set(TOTTOT,TASKTOT)),TASKTOT,TOTTOT) # move totals at the end
|
||||
|
||||
every write(k := !O, " : ", Tasks[k]," examples.") # 5. report
|
||||
end
|
||||
|
||||
procedure CleanURI(u) #: clean up a URI
|
||||
static tr,dxml # xml & http translation
|
||||
initial {
|
||||
tr := table()
|
||||
every c := !string(~(&digits++&letters++'-_.!~*()/\'')) do
|
||||
tr[c] := "%"||hexstring(ord(c),2)
|
||||
every /tr[c := !string(&cset)] := c
|
||||
tr[" "] := "_" # wiki convention
|
||||
every push(dxml := [],"&#"||right(ord(c := !"&<>'\""),3,"0")||";",c)
|
||||
}
|
||||
|
||||
dxml[1] := u # insert URI as 1st arg
|
||||
u := replacem!dxml # de-xml it
|
||||
every (c := "") ||:= tr[!u] # reencode everything
|
||||
return c
|
||||
end
|
||||
|
||||
procedure ReadURL(url) #: read URL into string
|
||||
write(&errout,"Opening ",image(url))
|
||||
page := open(url,"m",RCUA,RCXUA) | stop("Unable to open ",url)
|
||||
text := ""
|
||||
if page["Status-Code"] < 300 then
|
||||
while text ||:= reads(page,-1)
|
||||
else
|
||||
stop(page["Status-Code"]," ",page["Reason-Phrase"])
|
||||
close(page)
|
||||
return text
|
||||
end
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
require 'web/gethttp'
|
||||
|
||||
getAllTaskSolnCounts=: monad define
|
||||
tasks=. getCategoryMembers 'Programming_Tasks'
|
||||
counts=. getTaskSolnCounts &> tasks
|
||||
tasks;counts
|
||||
)
|
||||
|
||||
getTaskSolnCounts=: monad define
|
||||
makeuri=. 'http://www.rosettacode.org/w/index.php?title=' , ,&'&action=raw'
|
||||
wikidata=. gethttp makeuri urlencode y
|
||||
([: +/ '{{header|'&E.) wikidata
|
||||
)
|
||||
|
||||
formatSolnCounts=: monad define
|
||||
'tasks counts'=. y
|
||||
tasks=. tasks , &.>':'
|
||||
res=. ;:^:_1 tasks ,. (8!:0 counts) ,. <'examples.'
|
||||
res , 'Total examples: ' , ": +/counts
|
||||
)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
formatSolnCounts getAllTaskSolnCounts ''
|
||||
100 doors: 61 examples.
|
||||
24 game: 15 examples.
|
||||
24 game Player: 11 examples.
|
||||
99 Bottles of Beer: 76 examples.
|
||||
...
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
import java.util.ArrayList;
|
||||
import ScreenScrape;
|
||||
|
||||
public class CountProgramExamples {
|
||||
private static final String baseURL = "http://rosettacode.org/wiki/";
|
||||
private static final String rootURL = "http://www.rosettacode.org/w/"
|
||||
+ "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\">";
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
int exTotal = 0;
|
||||
try {
|
||||
// Get root query results
|
||||
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));
|
||||
}
|
||||
// Loop through each task and print count
|
||||
for (String task : tasks) {
|
||||
String title = task.replaceAll("'", "'");
|
||||
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.");
|
||||
}
|
||||
// Print total
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
function c = count_examples(url)
|
||||
c = 0;
|
||||
[s, success] = urlread (url);
|
||||
if ~success, return; end;
|
||||
c = length(strfind(s,'<h2><span class='));
|
||||
end;
|
||||
|
||||
% script
|
||||
s = urlread ('http://rosettacode.org/wiki/Category:Programming_Tasks');
|
||||
pat = '<li><a href="/wiki/';
|
||||
ix = strfind(s,pat)+length(pat)-6;
|
||||
for k = 1:length(ix);
|
||||
% look through all tasks
|
||||
e = find(s(ix(k):end)==34,1)-2;
|
||||
t = s(ix(k)+[0:e]); % task
|
||||
c = count_examples(['http://rosettacode.org',t]);
|
||||
printf('Task "%s" has %i examples.\n',t(7:end), c);
|
||||
end;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
TaskList = Flatten[
|
||||
Import["http://rosettacode.org/wiki/Category:Programming_Tasks", "Data"][[1, 1]]];
|
||||
|
||||
Print["Task \"", StringReplace[#, "_" -> " "], "\" has ",
|
||||
Length@Select[Import["http://rosettacode.org/wiki/" <> #, "Data"][[1,2]],
|
||||
StringFreeQ[#, __ ~~ "Programming Task" | __ ~~ "Omit"]& ], " example(s)"]&
|
||||
~Map~ StringReplace[TaskList, " " -> "_"]
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
open Http_client.Convenience
|
||||
|
||||
|
||||
let repl_quote s =
|
||||
let reg = Str.regexp_string "'" in
|
||||
(Str.global_replace reg "%27" s)
|
||||
|
||||
let repl_space s =
|
||||
let s = String.copy s in
|
||||
for i = 0 to pred(String.length s) do
|
||||
if s.[i] = ' ' then s.[i] <- '_'
|
||||
done;
|
||||
(s)
|
||||
(* or in OCaml 4.00+:
|
||||
let repl_space = String.map (fun c -> if c = ' ' then '_' else c)
|
||||
*)
|
||||
|
||||
let count_ex s =
|
||||
let pat = Str.regexp_string "=={{header|" in
|
||||
let rec aux n p =
|
||||
try
|
||||
let p = Str.search_forward pat s p in
|
||||
aux (n+1) (p+1)
|
||||
with Not_found -> (n)
|
||||
in
|
||||
aux 0 0
|
||||
|
||||
let get_child child xml =
|
||||
let child =
|
||||
List.find
|
||||
(function Xml.Element (tag,_,_) when tag = child -> true | _ -> false) xml
|
||||
in
|
||||
Xml.children child
|
||||
|
||||
let () =
|
||||
let url = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&\
|
||||
cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml" in
|
||||
|
||||
let xml = Xml.parse_string (http_get url) in
|
||||
|
||||
let total = ref 0 in
|
||||
at_exit (fun () -> Printf.printf "\n Total: %d\n" !total);
|
||||
|
||||
let f = function
|
||||
| Xml.Element ("cm", attrs, _) ->
|
||||
(try
|
||||
let _title = List.assoc "title" attrs in
|
||||
let title = repl_quote (repl_space _title) in
|
||||
let url = "http://www.rosettacode.org/w/index.php?title="^ title ^"&action=raw" in
|
||||
let n = count_ex (http_get url) in
|
||||
Printf.printf "%s: %d\n%!" _title n;
|
||||
total := n + !total;
|
||||
with Http_client.Http_error (404, _) -> ())
|
||||
| _ -> ()
|
||||
in
|
||||
|
||||
match xml with
|
||||
| Xml.Element ("api", _, ch) ->
|
||||
let query = get_child "query" ch in
|
||||
let catmb = get_child "categorymembers" query in
|
||||
List.iter f catmb
|
||||
| _ -> ()
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
declare
|
||||
[HTTPClient] = {Module.link ['x-ozlib://mesaros/net/HTTPClient.ozf']}
|
||||
[XMLParser] = {Module.link ['x-oz://system/xml/Parser.ozf']}
|
||||
[StringX] = {Module.link ['x-oz://system/String.ozf']}
|
||||
[Regex] = {Module.link ['x-oz://contrib/regex']}
|
||||
|
||||
AllTasksUrl = "http://rosettacode.org/mw/api.php?action=query&list="#
|
||||
"categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"
|
||||
|
||||
proc {Main}
|
||||
AllTasks = {Parse {GetPage AllTasksUrl}}
|
||||
TaskTitles = {GetTitles AllTasks}
|
||||
Total = {NewCell 0}
|
||||
in
|
||||
for Task in TaskTitles do
|
||||
TaskPage = {GetPage {TaskUrl Task}}
|
||||
RE = {Regex.compile "{{header\\|" [extended newline icase]}
|
||||
NumMatches = {Length {Regex.allMatches RE TaskPage}}
|
||||
in
|
||||
{System.showInfo Task#": "#NumMatches#" examples."}
|
||||
Total := @Total + NumMatches
|
||||
end
|
||||
{System.showInfo "Total: "#@Total#" examples."}
|
||||
end
|
||||
|
||||
fun {TaskUrl Task}
|
||||
"http://rosettacode.org/mw/index.php?"#
|
||||
"title="#{PercentEncode {StringX.replace Task " " "_"}}#
|
||||
"&action=raw"
|
||||
end
|
||||
|
||||
%% GetPage
|
||||
local
|
||||
Client = {New HTTPClient.urlGET init(inPrms(toFile:false toStrm:true) _)}
|
||||
in
|
||||
fun {GetPage RawUrl}
|
||||
Url = {VirtualString.toString RawUrl}
|
||||
OutParams
|
||||
HttpResponseParams
|
||||
in
|
||||
{Client getService(Url ?OutParams ?HttpResponseParams)}
|
||||
OutParams.sOut
|
||||
end
|
||||
end
|
||||
|
||||
%% Parse
|
||||
local
|
||||
Parser = {New XMLParser.parser init}
|
||||
in
|
||||
fun {Parse Xs} {Parser parseVS(Xs $)} end
|
||||
end
|
||||
|
||||
fun {GetTitles Doc}
|
||||
CMs = Doc.2.1.children.1.children.1.children
|
||||
fun {Attributes element(attributes:As ...)} As end
|
||||
fun {IsTitle attribute(name:N ...)} N == title end
|
||||
in
|
||||
{Map {Filter {Flatten {Map CMs Attributes}} IsTitle}
|
||||
fun {$ A} {Atom.toString A.value} end}
|
||||
end
|
||||
|
||||
fun {PercentEncode Xs}
|
||||
case Xs of nil then nil
|
||||
[] X|Xr then
|
||||
if {Char.isDigit X} orelse {Member X [&- &_ &. &~]}
|
||||
orelse X >= &a andthen X =< &z
|
||||
orelse X >= &z andthen X =< &Z then
|
||||
X|{PercentEncode Xr}
|
||||
else
|
||||
{Append &%|{ToHex2 X} {PercentEncode Xr}}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
fun {ToHex2 X}
|
||||
[{ToHex1 X div 16} {ToHex1 X mod 16}]
|
||||
end
|
||||
|
||||
fun {ToHex1 X}
|
||||
if X >= 0 andthen X =< 9 then &0 + X
|
||||
elseif X >= 10 andthen X =< 15 then &A + X - 10
|
||||
end
|
||||
end
|
||||
in
|
||||
{Main}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
use LWP::Simple;
|
||||
|
||||
my $site = "http://rosettacode.org";
|
||||
my $list_url = "/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml";
|
||||
|
||||
for (get("$site$list_url") =~ /cm.*?title="(.*?)"/g) {
|
||||
(my $slug = $_) =~ tr/ /_/;
|
||||
my $count = () = get("$site/wiki/$slug") =~ /toclevel-1/g;
|
||||
print "$_: $count examples\n";
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
use v5.10;
|
||||
use Mojo::UserAgent;
|
||||
|
||||
my $site = "http://rosettacode.org";
|
||||
my $list_url = "/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml";
|
||||
|
||||
my $ua = Mojo::UserAgent->new;
|
||||
$ua->get("$site$list_url")->res->dom->find('cm')->each(sub {
|
||||
(my $slug = $_->{title}) =~ tr/ /_/;
|
||||
my $count = $ua->get("$site/wiki/$slug")->res->dom->find("#toc .toclevel-1")->size;
|
||||
say "$_->{title}: $count examples";
|
||||
});
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
(load "@lib/http.l")
|
||||
|
||||
(client "rosettacode.org" 80
|
||||
"mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"
|
||||
(while (from " title=\"")
|
||||
(let Task (till "\"")
|
||||
(client "rosettacode.org" 80 (pack "wiki/" (replace Task " " "_"))
|
||||
(let Cnt 0
|
||||
(while (from "<span class=\"tocnumber\">")
|
||||
(unless (sub? "." (till "<" T))
|
||||
(inc 'Cnt) ) )
|
||||
(out NIL (prinl (ht:Pack Task) ": " Cnt)) ) ) ) ) )
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
OpenConsole()
|
||||
URLDownloadToFile_( #Null, "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml", "tasks.xml", 0, #Null)
|
||||
ReadFile(0, "tasks.xml")
|
||||
x1$ = ReadString(0)
|
||||
Repeat
|
||||
x2 = FindString(x1$, "title=", x2 + 1)
|
||||
If x2
|
||||
title$ = Mid(x1$, x2 + 7, 99)
|
||||
title$ = Left(title$, FindString(title$, ">", 1) - 4)
|
||||
URLDownloadToFile_( #Null, "http://www.rosettacode.org/w/index.php?title=" + title$ + "&action=raw", "task.xml", 0, #Null)
|
||||
ReadFile(1, "task.xml")
|
||||
While Not Eof(1)
|
||||
y1$ = ReadString(1)
|
||||
If FindString(y1$, "=={{header|", 1)
|
||||
j + 1
|
||||
EndIf
|
||||
Wend
|
||||
PrintN( title$ +": " + Str(j) + " examples")
|
||||
k + j
|
||||
j = 0
|
||||
CloseFile(1)
|
||||
EndIf
|
||||
Until x2 = 0
|
||||
PrintN("Total: " + Str(k) + " examples")
|
||||
Input()
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import urllib, xml.dom.minidom
|
||||
|
||||
x = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml")
|
||||
|
||||
tasks = []
|
||||
for i in xml.dom.minidom.parseString(x.read()).getElementsByTagName("cm"):
|
||||
t = i.getAttribute('title').replace(" ", "_")
|
||||
y = urllib.urlopen("http://www.rosettacode.org/w/index.php?title=%s&action=raw" % t.encode('utf-8'))
|
||||
tasks.append( y.read().lower().count("{{header|") )
|
||||
print t.replace("_", " ") + ": %d examples." % tasks[-1]
|
||||
|
||||
print "\nTotal: %d examples." % sum(tasks)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
library(XML)
|
||||
library(RCurl)
|
||||
doc <- xmlInternalTreeParse("http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml")
|
||||
nodes <- getNodeSet(doc,"//cm")
|
||||
titles = as.character( sapply(nodes, xmlGetAttr, "title") )
|
||||
headers <- list()
|
||||
counts <- list()
|
||||
for (i in 1:length(titles)){
|
||||
headers[[i]] <- getURL( paste("http://rosettacode.org/mw/index.php?title=", gsub(" ", "_", titles[i]), "&action=raw", sep="") )
|
||||
counts[[i]] <- strsplit(headers[[i]],split=" ")[[1]]
|
||||
counts[[i]] <- grep("\\{\\{header", counts[[i]])
|
||||
cat(titles[i], ":", length(counts[[i]]), "examples\n")
|
||||
}
|
||||
cat("Total: ", length(unlist(counts)), "examples\n")
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
require 'open-uri'
|
||||
require 'rexml/document'
|
||||
|
||||
module RosettaCode
|
||||
|
||||
URL_ROOT = "http://rosettacode.org/mw"
|
||||
|
||||
def self.get_url(page, query)
|
||||
begin
|
||||
# Ruby 1.9.2
|
||||
pstr = URI.encode_www_form_component(page)
|
||||
qstr = URI.encode_www_form(query)
|
||||
rescue NoMethodError
|
||||
require 'cgi'
|
||||
pstr = CGI.escape(page)
|
||||
qstr = query.map {|k,v|
|
||||
"%s=%s" % [CGI.escape(k.to_s), CGI.escape(v.to_s)]}.join("&")
|
||||
end
|
||||
url = "#{URL_ROOT}/#{pstr}?#{qstr}"
|
||||
p url if $DEBUG
|
||||
url
|
||||
end
|
||||
|
||||
def self.get_api_url(query)
|
||||
get_url "api.php", query
|
||||
end
|
||||
|
||||
def self.category_members(category)
|
||||
query = {
|
||||
"action" => "query",
|
||||
"list" => "categorymembers",
|
||||
"cmtitle" => "Category:#{category}",
|
||||
"format" => "xml",
|
||||
"cmlimit" => 500,
|
||||
}
|
||||
while true
|
||||
url = get_api_url query
|
||||
doc = REXML::Document.new open(url)
|
||||
|
||||
REXML::XPath.each(doc, "//cm") do |task|
|
||||
yield task.attribute("title").value
|
||||
end
|
||||
|
||||
continue = REXML::XPath.first(doc, "//query-continue")
|
||||
break if continue.nil?
|
||||
cm = REXML::XPath.first(continue, "categorymembers")
|
||||
query["cmcontinue"] = cm.attribute("cmcontinue").value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
require 'rosettacode'
|
||||
|
||||
total_examples = 0
|
||||
|
||||
RosettaCode.category_members("Programming_Tasks") do |task|
|
||||
url = RosettaCode.get_url("index.php", {"action" => "raw", "title" => task})
|
||||
examples = open(url).read.scan("=={{header").length
|
||||
puts "#{task}: #{examples}"
|
||||
total_examples += examples
|
||||
end
|
||||
|
||||
puts
|
||||
puts "Total: #{total_examples}"
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
html "<table border=1><tr bgcolor=wheat align=center><td>Num</td><td>Task</td><td>Examples</td></tr>"
|
||||
|
||||
a$ = httpGet$("http://rosettacode.org/wiki/Category:Programming_Tasks")
|
||||
a$ = word$(a$,1,"</table></div>")
|
||||
i = instr(a$,"<a href=""/wiki/")
|
||||
i = instr(a$,"<a href=""/wiki/",i+1)
|
||||
while i > 0
|
||||
count = count + 1
|
||||
i = instr(a$,"<a href=""/wiki/",i+1)
|
||||
j = instr(a$,">",i+5)
|
||||
a1$ = mid$(a$,i+15,j-i)
|
||||
taskId$ = word$(a1$,1,"""")
|
||||
task$ = word$(a1$,3,"""")
|
||||
url$ = "http://rosettacode.org/wiki/";taskId$
|
||||
a2$ = httpGet$(url$)
|
||||
ii = instr(a2$,"<span class=""tocnumber"">")
|
||||
jj = 0
|
||||
while ii > 0
|
||||
jj = ii
|
||||
ii = instr(a2$,"<span class=""tocnumber"">",ii+10)
|
||||
wend
|
||||
if jj = 0 then
|
||||
examp = 0
|
||||
else
|
||||
kk = instr(a2$,"<",jj+24)
|
||||
examp = int(val(mid$(a2$,jj+24,kk-jj-24)))
|
||||
end if
|
||||
html "<tr><td align=right>";count;"</td><td>";task$;"</td><td align=right>";examp;"</td></tr>"
|
||||
totExamp = totExamp + examp
|
||||
wend
|
||||
html "<tr bgcolor=wheat><td>**</td><td>** Total **</td><td align=right>";totExamp;"</td></tr></table>"
|
||||
end
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
import java.net.{URL, URLEncoder}
|
||||
import scala.io.Source.fromURL
|
||||
|
||||
val allTasksURL = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"
|
||||
val allTasks = xml.parsing.XhtmlParser(fromURL(new URL(allTasksURL)))
|
||||
|
||||
val regexExpr = "(?i)==\\{\\{header\\|".r
|
||||
def oneTaskURL(title: String) = "http://www.rosettacode.org/w/index.php?title=%s&action=raw" format URLEncoder.encode(title.replace(' ', '_'), "UTF-8")
|
||||
def count(title: String) = regexExpr findAllIn fromURL(new URL(oneTaskURL(title)))(io.Codec.UTF8).mkString length
|
||||
|
||||
val counts = for (task <- allTasks \\ "cm" \\ "@title" map (_.text)) yield scala.actors.Futures.future((task, count(task)))
|
||||
|
||||
counts map (_.apply) map Function.tupled("%s: %d examples." format (_, _)) foreach println
|
||||
println("\nTotal: %d examples." format (counts map (_.apply._2) sum))
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
$$ MODE TUSCRIPT
|
||||
url="http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"
|
||||
data=REQUEST (url)
|
||||
|
||||
BUILD S_TABLE beg=*
|
||||
DATA :title=":
|
||||
BUILD S_TABLE end=*
|
||||
DATA :":
|
||||
|
||||
titles=EXTRACT (data,beg|,end,1,0,"~~")
|
||||
titles=SPLIT (titles,":~~:")
|
||||
sz_titles=SIZE (titles)
|
||||
|
||||
BUILD R_TABLE header=":==\{\{header|:"
|
||||
all=*
|
||||
|
||||
ERROR/STOP CREATE ("tasks",seq-e,-std-)
|
||||
|
||||
COMPILE
|
||||
LOOP title=titles
|
||||
ask=*
|
||||
ask =SET_VALUE(ask,"title",title)
|
||||
ask =SET_VALUE(ask,"action","raw")
|
||||
ask =ENCODE (ask,cgi)
|
||||
http ="http://www.rosettacode.org/mw/index.php"
|
||||
url =CONCAT (http,"?",ask)
|
||||
data =REQUEST (url)
|
||||
header =FILTER_INDEX (data,header,-)
|
||||
sz_header=SIZE(header)
|
||||
line =CONCAT (title,"=",sz_header," members")
|
||||
FILE "tasks" = line
|
||||
all =APPEND(all,sz_header)
|
||||
ENDLOOP
|
||||
|
||||
ENDCOMPILE
|
||||
all =JOIN(all),sum=SUM(all),time=time()
|
||||
line=CONCAT (time,": ", sz_titles, " Programing Tasks: ", sum, " solutions")
|
||||
|
||||
FILE "tasks" = line
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package require Tcl 8.5
|
||||
package require http
|
||||
package require json
|
||||
|
||||
fconfigure stdout -buffering none
|
||||
|
||||
proc get_tasks {category} {
|
||||
set start [clock milliseconds]
|
||||
puts -nonewline "getting $category members..."
|
||||
set base_url http://www.rosettacode.org/w/api.php
|
||||
set query {action query list categorymembers cmtitle Category:%s format json cmlimit 500}
|
||||
set this_query [dict create {*}[split [format $query $category]]]
|
||||
set tasks [list]
|
||||
|
||||
while {1} {
|
||||
set url [join [list $base_url [http::formatQuery {*}$this_query]] ?]
|
||||
set response [http::geturl $url]
|
||||
if {[set s [http::status $response]] ne "ok" || [http::ncode $response] != 200} {
|
||||
error "Oops: url=$url\nstatus=$s\nhttp code=[http::code $response]"
|
||||
}
|
||||
set data [json::json2dict [http::data $response]]
|
||||
http::cleanup $response
|
||||
|
||||
# add tasks to list
|
||||
foreach task [dict get $data query categorymembers] {
|
||||
lappend tasks [dict get [dict create {*}$task] title]
|
||||
}
|
||||
|
||||
if {[catch {dict get $data query-continue categorymembers cmcontinue} continue_task] != 0} {
|
||||
# no more continuations, we're done
|
||||
break
|
||||
}
|
||||
dict set this_query cmcontinue $continue_task
|
||||
}
|
||||
puts " found [llength $tasks] tasks in [expr {[clock milliseconds] - $start}] milliseconds"
|
||||
return $tasks
|
||||
}
|
||||
|
||||
# This proc can be replaced by a single regexp command:
|
||||
# set count [regexp -all "***=$needle" $haystack]
|
||||
# However this proc is more efficient -- we're dealing with plain strings only.
|
||||
proc count_substrings {needle haystack} {
|
||||
set count 0
|
||||
set idx 0
|
||||
while {[set idx [string first $needle $haystack $idx]] != -1} {
|
||||
incr count
|
||||
incr idx
|
||||
}
|
||||
return $count
|
||||
}
|
||||
|
||||
set total 0
|
||||
foreach task [get_tasks Programming_Tasks] {
|
||||
set url [format "http://www.rosettacode.org/w/index.php?title=%s&action=raw" [string map {{ } _} $task]]
|
||||
set response [http::geturl $url]
|
||||
if {[set s [http::status $response]] ne "ok" || [http::ncode $response] != 200} {
|
||||
error "Oops: url=$url\nstatus=$s\nhttp code=[http::code $response]"
|
||||
}
|
||||
set count [count_substrings "\{\{header|" [http::data $response]]
|
||||
puts [format "%3d examples in %s" $count $task]
|
||||
http::cleanup $response
|
||||
incr total $count
|
||||
}
|
||||
|
||||
puts "\nTotal: $total examples"
|
||||
Loading…
Add table
Add a link
Reference in a new issue