Skip to content

Commit 972c8e4

Browse files
add letters-words-prediction tries
1 parent 3729fe0 commit 972c8e4

File tree

6 files changed

+113
-3
lines changed

6 files changed

+113
-3
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,37 @@ console.log(trie.hasSequence("missing")) // false
3030

3131
```
3232

33+
## Letters and Words Prediction Tries
34+
35+
```ts
36+
37+
import { LettersWordsPredictionTrie } from "https://deno.land/tries/mod-letters-and-word-prediction.ts"
38+
39+
const lettersWordsPredictionTrie = new LettersWordsPredictionTrie()
40+
41+
lettersWordsPredictionTrie.learn(['ether', 'eth', 'ethereum', 'super'])
42+
43+
const brain = lettersWordsPredictionTrie.getBrain()
44+
45+
console.log(JSON.stringify(brain, undefined, 2))
46+
47+
48+
```
49+
50+
## PATRICIA Tries
51+
**P** Practical
52+
**A** Algorithm
53+
**T** To
54+
**R** Retrieve
55+
**I** Information
56+
**C** Coded
57+
**I** In
58+
**A** Alphanumeric
59+
60+
In the Ethereum Blockchain pretty much everything (state trie, transaction trie, receipt trie, ...) ist stored in PATRICIA Tries. For details I can recommend [this video](https://www.youtube.com/watch?v=OxofT39TJgg).
3361

34-
## Patricia Tries
3562
### Usage Examples
36-
[Patricia Tries](https://de.wikipedia.org/wiki/Patricia-Trie) ...
63+
... Under Construction ...
3764

3865
## Unit Tests
3966
For further usage examples etc. please check the [unit tests](https://github.com/distributed-ledger-technology/tries/blob/main/src/trie.spec.ts).

mod-letters-and-word-prediction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./src/letters-words-prediction-trie/letters-words-prediction-trie.ts"

mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./src/trie.ts"
1+
export * from "./src/trie.ts"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import { assertEquals } from "https://deno.land/std@0.106.0/testing/asserts.ts"
3+
import { LettersWordsPredictionTrie } from "./letters-words-prediction-trie.ts"
4+
5+
Deno.test("should build a data structure optimized for letters and words prediction", async () => {
6+
7+
const lettersWordsPredictionTrie = new LettersWordsPredictionTrie()
8+
9+
lettersWordsPredictionTrie.learn(['ether', 'eth', 'ethereum', 'super'])
10+
11+
const brain = lettersWordsPredictionTrie.getBrain()
12+
13+
assertEquals(JSON.stringify(brain).length, 575) // better assertion follows --> search / query ...
14+
})
15+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export class LettersWordsPredictionTrie {
2+
3+
private brain: any = {}
4+
5+
public learn(inputs: string[]): any {
6+
7+
this.freeMyBrain()
8+
9+
for (const input of inputs) {
10+
this.addInput(input, this.brain)
11+
}
12+
13+
}
14+
15+
public getBrain(): any {
16+
return this.brain
17+
}
18+
19+
private addInput(input: string, brain: any): any {
20+
21+
if (input.length == 0) return {}
22+
23+
if (this.isCharacterInBrain(input[0], brain)) {
24+
this.incrementCountForCharacter(input[0], brain)
25+
} else {
26+
this.createNewNode(input[0], brain)
27+
}
28+
29+
const theRestOfTheInput = input.substring(1)
30+
31+
if (theRestOfTheInput !== '') {
32+
this.handleTheRestOfTheMessage(input[0], brain, theRestOfTheInput)
33+
}
34+
35+
return brain
36+
}
37+
38+
private createNewNode(character: string, brain: any): void {
39+
brain[character] = { value: character, count: 1, followables: {} }
40+
}
41+
42+
private incrementCountForCharacter(character: string, brain: any): void {
43+
brain[character].count += 1
44+
}
45+
46+
private handleTheRestOfTheMessage(character: string, brain: any, theRestOfTheMessage: string): void {
47+
brain[character].followables = this.addInput(theRestOfTheMessage, brain[character].followables)
48+
}
49+
50+
private freeMyBrain(): void {
51+
this.brain = {}
52+
}
53+
54+
private isCharacterInBrain(character: string, brain: any): boolean {
55+
return (brain[character] !== undefined)
56+
}
57+
58+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { LettersWordsPredictionTrie } from "https://deno.land/tries/mod-letters-and-word-prediction.ts"
2+
3+
const lettersWordsPredictionTrie = new LettersWordsPredictionTrie()
4+
5+
lettersWordsPredictionTrie.learn(['ether', 'eth', 'ethereum', 'super'])
6+
7+
const brain = lettersWordsPredictionTrie.getBrain()
8+
9+
console.log(JSON.stringify(brain, undefined, 2))

0 commit comments

Comments
 (0)