Libraryless. Click here for Pure Java version (427L/4K/12K).
1 | !747 |
2 | |
3 | m { |
4 | static S sentence1 = "Yes is the opposite of no. No is the opposite of yes."; |
5 | static S sentence2 = "Green is the opposite of white."; |
6 | |
7 | !include #1000988 // MultiSet |
8 | |
9 | p { |
10 | L<S> tok1 = parse(sentence1); |
11 | L<S> tok2 = parse(sentence2); |
12 | |
13 | Map<S, MultiSet<S>> map = makeMMapPrefix(tok1, tok2); |
14 | if (map == null) |
15 | print("No match."); |
16 | else |
17 | print(structure(map)); |
18 | |
19 | print(complete(sentence1, sentence2)); |
20 | } |
21 | |
22 | static L<S> parse(S s) { |
23 | return tokensToLowerCase(javaTok(s)); |
24 | } |
25 | |
26 | static S complete(S sentence1, S sentence2) { |
27 | L<S> tok1 = parse(sentence1); |
28 | L<S> tok2 = parse(sentence2); |
29 | Map<S, MultiSet<S>> map = makeMMapPrefix(tok1, tok2); |
30 | if (map == null) return null; |
31 | new L<S> tok; |
32 | tok.addAll(tok2.subList(0, tok2.size()-1)); |
33 | for (int i = tok2.size()-1; i < tok1.size(); i++) { |
34 | S t = tok1.get(i); |
35 | MultiSet<S> set = map.get(t); |
36 | tok.add(set == null ? t : set.getMostPopularEntry()); |
37 | } |
38 | return join(tok); |
39 | } |
40 | |
41 | static Map<S, MultiSet<S>> makeMMapPrefix(L<S> tok1, L<S> tok2) { |
42 | if (tok1.size() < tok2.size()) return null; |
43 | |
44 | Map<S, MultiSet<S>> map = new TreeMap<S, MultiSet<S>>(); |
45 | for (int i = 1; i < tok2.size(); i += 2) { |
46 | S t1 = tok1.get(i), t2 = tok2.get(i); |
47 | MultiSet<S> set = map.get(t1); |
48 | if (set == null) |
49 | map.put(t1, new MultiSet<S>(t2)); |
50 | else |
51 | set.add(t2); |
52 | } |
53 | |
54 | // match succeeds |
55 | return map; |
56 | } |
57 | } |
Began life as a copy of #1000986
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000987 |
Snippet name: | Multiset matching for prediction |
Eternal ID of this version: | #1000987/1 |
Text MD5: | ed7856c0508dde97310f3017d6c3a51b |
Transpilation MD5: | dfad3438c063fe55b3d79df6839131d6 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-09-14 02:07:56 |
Source code size: | 1544 bytes / 57 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 594 / 598 |
Referenced in: | [show references] |