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

114
LINES

< > BotCompany Repo | #1005476 // Parse Sentence Multi [works]

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

Libraryless. Click here for Pure Java version (5809L/37K/126K).

!752

concepts.

concept WithTime {
  new Ref sentence;
  new Ref time;
}

concept Possession {
  new Ref x; // e.g. "your"
  new Ref y; // e.g. "car"
}

// e.g. someone's girl
concept Possession2 {
  new Ref x; // e.g. "someone"
  new Ref y; // e.g. "girl"
}

concept Sentence {}

Sentence > SPO {
  new Ref s; // subject
  new Ref p; // predicate
  new Ref o; // object
  
  static S _fieldOrder = "s p o";
  
  *() {}
  *(Concept s, Concept p, Concept o) {
    this.s.set(s);
    this.p.set(p);
    this.o.set(o);
  }
}

p {
  testParse("I've been here today");
  testParse("I've been your friend today");
  testParse("I've been his bimbo today");
  testParse("I've been this dude's girl today");
}

svoid testParse(S s) {
  print("** \* s */ **");
  print();
  new L<Concept> l;
  parse(s, listCollector(l));
  for i over l:
    print((i+1) + ". " + structureConcept(l.get(i)));
  
  for (Concept c : iterateOptions(l)) {
    if (properSentence(c)) {
      print("First proper sentence: " + structureConcept(c));
      break;
    }
  }
  print();
}

static Concept parseToOptions(S s) {
  new L<Concept> l;
  parse(s, listCollector(l));
  ret options(l);
}

static L<S> poss = splitAtSpace("my your his her their our");

static void parse(S s, Collector<Concept> out) {
  new Matches m;
  
  if (flexMatchIC("* today", s, m))
    ret if out.add(cnew(WithTime,
      sentence := parseToOptions($1),
      time := concept("today")));

  if (flexMatchStartIC(poss, s, m))
    ret if out.add(cnew(Possession,
      x := cstr($1),
      y := parseToOptions($2)));

  L<S> tok = javaTok(s);
  int i = indexOfEndsWith(tok, "'s");
  if (i >= 0) {
    S left = join(subList(tok, 0, i)) + dropSuffix("'s", tok.get(i));
    S right = join(subList(tok, i+2));
    ret if out.add(cnew(Possession2,
      x := parseToOptions(left), y := parseToOptions(right)));
  }
  
  if (flexMatchIC("I've been *", s, m))
    ret if out.add(
      new SPO(cstr("I"),
      cstr("have been"),
      parseToOptions($1)));

  out.add(cstr(s)); // default
}

static Concept firstOfFirst(Concept c) {
  ret conceptGraphMap_aggressive(c, func(Concept c) {
    c << Options ? first(c/Options) : c
  });
}

sbool properSentence(Concept c) {
  ret c << Sentence
    || c << WithTime && properSentence(c/WithTime.sentence!);
}

static Iterable<Concept> iterateOptions(L<Concept> l) {
  ret mapI(l, func(Concept c) { firstOfFirst(c) });
}

Author comment

Began life as a copy of #1005474

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

-
Snippet ID: #1005476
Snippet name: Parse Sentence Multi [works]
Eternal ID of this version: #1005476/1
Text MD5: 9cbaee1a74d8b36d7ef925e325633dd1
Transpilation MD5: 5307e4bb7cb2cb0b55a307cf849d6ffe
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-21 16:32:16
Source code size: 2517 bytes / 114 lines
Pitched / IR pitched: No / No
Views / Downloads: 628 / 675
Referenced in: