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

90
LINES

< > BotCompany Repo | #1005038 // Parse Some Text [dev.]

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

Libraryless. Click here for Pure Java version (4020L/26K/89K).

!752

concepts.

// a named concept
concept NConcept {
  S name;
  
  *() {}
  *(S *name) { change(); }
}

concept HasA {
  new Ref<NConcept> a;
  new Ref<NConcept> b;
  
  *() {}
  *(NConcept a, NConcept b) {
    this.a.set(a);
    this.b.set(b);
  }
}

concept IsA {
  new Ref<NConcept> a;
  new Ref<NConcept> b;
  
  *() {}
  *(NConcept a, NConcept b) {
    this.a.set(a);
    this.b.set(b);
  }
}

concept IsCollectionOf {
  new Ref<NConcept> a;
  new Ref<NConcept> b;
  
  *() {}
  *(NConcept a, NConcept b) {
    this.a.set(a);
    this.b.set(b);
  }
}

concept SizeOfCollection extends IsCollectionOf {
  int n;
  
  *() {}
  *(NConcept a, int n, NConcept b) {
    super(a, b);
    this.n = n;
    change();
  }
}

sS text = [[
  What is a FUNCTION?
  A function has a NAME.
  A name is a STRING.
  A STRING is a COLLECTION of CHARACTERS.
  "The ALPHABET" is a COLLECTION of CHARACTERS.
  "The ALPHABET" contains 26 CHARACTERS.
  "The DIGITS" is a COLLECTION of CHARACTERS.
  "The DIGITS" contains 26 CHARACTERS.
  "SPACE" is a CHARACTER.
]];

p {
  loadAndAutoSaveConcepts();
  new Matches m;
  for (S s : toLinesFullTrim(text)) {
    if "What is a *" getConcept(m, 0);
    if "A * has a *" hasA(getConcept(m, 0), getConcept(m, 1));
    if "A * is a collection of *" isCollectionOf(getConcept(m, 0), getConcept(m, 1));
    if (match("A * contains * *", s, m) && isInteger(m.unq(1)))
      sizeOfCollection(getConcept(m, 0), m.psi(1), getConcept(m, 2));
    if "* is a *" isA(getConcept(m, 0), getConcept(m, 1));
  }
}

static NConcept getConcept(Matches m, int i) {
  S conceptName = singular(toLowerCase(m.unq(0)));
  ret uniqueConcept(NConcept.class, "name", conceptName);
}

svoid hasA(NConcept a, NConcept b) { uniqueConcept(HasA.class, "a", a, "b", b); }
svoid isCollectionOf(NConcept a, NConcept b) { uniqueConcept(IsCollectionOf.class, "a", a, "b", b); }
svoid sizeOfCollection(NConcept a, int n, NConcept b) { uniqueConcept(SizeOfCollection.class, "a", a, "n", n, "b", b); }
svoid isA(NConcept a, NConcept b) { uniqueConcept(IsA.class, "a", a, "b", b); }

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1005038
Snippet name: Parse Some Text [dev.]
Eternal ID of this version: #1005038/1
Text MD5: dea7a5e8b77cc90aa8ce35cab3a38fc6
Transpilation MD5: 7fac6a87548dd82b9ebf2510ce24294b
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-29 23:04:17
Source code size: 2159 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 578 / 650
Referenced in: