-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
154 lines (118 loc) · 3.55 KB
/
Copy pathcli.js
File metadata and controls
154 lines (118 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const Observable = require('rx').Observable
const fs = require('fs')
function clear() {
process.stdout.write('\x1B[2J\x1B[0f')
}
clear()
const file$ = Observable.create( obs => {
const path = __dirname + '/dict.txt'
console.log(`Loading data from ${path}...`)
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
obs.onError(err)
} else {
obs.onNext(data)
obs.onCompleted()
}
})
})
const {random, floor} = Math
const {create, assign, keys} = Object
function createNode(id){
return {id, links: {}}
}
function extractGraph(str){
const chars = str.split('')
.filter(c => c != '\n')
const uniqueChars = new Set(chars)
const graph = { nodes: {}, totalConnections: 0}
uniqueChars.forEach( char => graph.nodes[char] = createNode(char))
return Observable.create( obs => {
Observable
.fromArray(chars)
.pairwise()
.map( ([first, second]) => {
const parentNode = graph.nodes[first]
const link = parentNode.links[second]
if(link >= 1) {
parentNode.links[second] = link + 1
} else {
parentNode.links[second] = 1
}
graph.totalConnections ++
})
.subscribeOnCompleted( () => {
obs.onNext(graph)
obs.onCompleted()
})
})
}
function rand(min, max) {
return random() * (max - min) + min
}
function getRandomItem(list, weight) {
var total_weight = weight.reduce(function (prev, cur, i, arr) {
return prev + cur
}, 0)
var random_num = rand(0, total_weight)
var weight_sum = 0
//console.log(random_num)
for (var i = 0; i < list.length; i++) {
weight_sum += weight[i]
weight_sum = +weight_sum.toFixed(2)
if (random_num <= weight_sum) {
return list[i]
}
}
// end of function
}
function sum(acc, val) { return acc + val }
function next(node, nodes) {
const linkIDs = keys(node.links)
const totalCount = linkIDs.reduce(
(sum, key) => sum + node.links[key],
0 )
const samples = linkIDs.map( id => node.links[id] / totalCount)
// console.log(samples)
return nodes[getRandomItem(linkIDs, samples)]
}
const randomRange = (min, max) => random() * (max - min) + min
function createWords(graph) {
const graphKeys = keys(graph.nodes)
const randomIndex = graphKeys[floor(graphKeys.length * random())]
const randomNode = graph.nodes[randomIndex]
let max = floor(randomRange(2,7))
let node = randomNode
let word = randomNode.id
while(max) {
const nextNode = next(node, graph.nodes)
if(!nextNode) break
node = nextNode
word += nextNode.id
max--
}
return word
}
function renderResult(result) {
console.log(
`
Ladies and Gentlemen, I give You:
----------------------------
${result}
----------------------------
`)
}
const format = name => name.charAt(0).toUpperCase() + name.slice(1)
const maxNames = 10
file$
.flatMap(extractGraph)
.flatMap( graph => Observable.just(graph)
.map(createWords)
.map(format)
.repeat(maxNames)
)
.scan( (all, curr) => all.concat([curr]), [])
.filter( list => list.length == maxNames)
.take(1)
.map( arr => arr.join(',\n'))
.subscribe( renderResult )