Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

48
LINES

< > BotCompany Repo | #1007924 // Test querying Simple Wikipedia [WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1569L/11K/36K).

1  
!7
2  
3  
sclass WikiResult {
4  
  S rawPage, firstPart;
5  
  
6  
  S firstSentence() {
7  
    L<S> lines = toLines(firstPart);
8  
    ret get(lines, indexOfNotStartingWith(lines, "[["));
9  
  }
10  
}
11  
12  
WikiResult > Redirect {
13  
  S toWord;
14  
}
15  
16  
p {
17  
  testWord("fun");
18  
  testWord("recreation");
19  
}
20  
21  
svoid testWord(S word) {
22  
  print("= " + word + " =");
23  
  WikiResult result = querySimpleWikipedia(word);
24  
  print(result.rawPage);
25  
  print();
26  
  if (result >> Redirect)
27  
    print("Redirect to: " + quote(result/Redirect.toWord));
28  
  else
29  
    print("First sentence:\n" + indent(dropContainerTags(result.firstSentence())));
30  
  print();
31  
}
32  
33  
static WikiResult querySimpleWikipedia(S word) {
34  
  S raw = loadPageSilently("https://simple.wikipedia.org/wiki/" + urlencode(word) + "?action=raw"), rawPage = raw;
35  
  
36  
  // check for redirect
37  
  S p = "#REDIRECT [[";
38  
  if (startsWith(raw, p) && endsWith(raw, "]]"))
39  
    ret nu(Redirect, +rawPage,
40  
      toWord := dropPrefix(p, dropSuffix("]]", raw)));
41  
      
42  
  L<S> lines = toLines(raw);
43  
  int n = smartIndexOfStartsWith(lines, "==");
44  
  S firstPart = joinLines(subList(lines, 0, n));
45  
  print("First part:\n" + indent(firstPart));
46  
      
47  
  ret nu(WikiResult, +rawPage, +firstPart);
48  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xinetxnxrdbb

No comments. add comment

Snippet ID: #1007924
Snippet name: Test querying Simple Wikipedia [WORKS]
Eternal ID of this version: #1007924/21
Text MD5: a913bd3175f93c3b8f18e10c27d04a90
Transpilation MD5: 2ef74f3b24b6c58cdb1bdffc95142503
Author: stefan
Category: javax / a.i. / networking
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-18 17:04:26
Source code size: 1223 bytes / 48 lines
Pitched / IR pitched: No / No
Views / Downloads: 599 / 1231
Version history: 20 change(s)
Referenced in: [show references]