PageSourceSearch

https://autonomicon-c352f.web.app/js/components/textView.js

js autonomicon-c352f.web.app collected 2026-10-03 12:17:32 UTC 10,627 bytes, 276 lines download raw bytes

1/* 
2  
3    angularJS button component for pull down 
4
5*/
6
7"use strict";
8
9var textView = {
10    bindings: {},
11    controller: ['wordService', 'lovecraftService2',function (wordService, lovecraftService2) {
12        var ctrl = this;
13        ctrl.status = 'running'
14
15        ctrl.$onInit = function () {
16            ctrl.wordBuffer = []
17            ctrl.wordList = [{}]
18
19            ctrl.getLovecraftSentence().then((results) => {
20                ctrl.wordBuffer = results
21                ctrl.status = 'done'
22                ctrl.story = "..." + ctrl.wordBuffer.shift()
23                ctrl.getNextWord(ctrl.wordBuffer.shift(), 0)
24            })
25
26
27        };
28
29        // utility function to return an integer random number in the range requested
30        ctrl.random = (range) => {
31            return Math.floor(Math.random() * range)
32        }
33
34        //utlity function to capitalise first letter in a string
35        const capitalise = (str) => {
36            if (typeof str === 'string') {
37                return str.replace(/^\w/, c => c.toUpperCase());
38            } else {
39                return '';
40            }
41        };
42
43        //restart - effectively just rerun init
44        ctrl.clear = () => {
45            ctrl.wordBuffer = []
46            ctrl.wordList = [{}]
47            ctrl.getLovecraftSentence().then((results) => {
48                ctrl.wordBuffer = results
49                ctrl.story = "..." + ctrl.wordBuffer.shift()
50                ctrl.getNextWord(ctrl.wordBuffer.shift(), 0)
51            })
52 
53        };
54
55        //call the lovecraft service to return a sentence from lovecraft API
56        ctrl.getLovecraftSentence = (lastWord) => {
57            ctrl.wordList = [{}]
58            return lovecraftService2.getNextLovecraft(lastWord).then((results) => {
59                console.log('Got sentence:');
60                console.log('lastWord:')+  lastWord;
61                console.log(results);
62
63                console.log(results);
64                return results
65            })
66        };
67
68
69        // called when user presses the go button.
70        // if we have a sentence in the buffer then display next in sequence
71        // if not then call the generator
72        ctrl.showNextWord = () => {
73            if (ctrl.wordBuffer.length > 0) {
74                console.log(ctrl.wordBuffer);
75                const nextWord = ctrl.wordBuffer.shift()
76                //ctrl.story = ctrl.story + " " + nextWord
77                // call this to update the selection of most likely words to follow
78                ctrl.getNextWord(nextWord, 0)
79            } else {
80                ctrl.generator()
81            }
82
83        }
84
85        // this routine is called to generate a selection of words most
86        // likely to follow the word supplied along with scores
87        ctrl.generator = () => {
88            //get last word added to the story
89            const lastWord = ctrl.story.split(" ").pop()
90            console.log("lastWord:" + lastWord);
91            const choice = Math.floor(Math.random() * 10)
92            // try and splice on three letter words only as these are much better connectors
93            if (choice > 4 && lastWord.length<5) {
94                //go get a new sentence
95                ctrl.status = 'running'
96                ctrl.blurb = "Great Cthulhu is dreaming ..."
97                ctrl.getLovecraftSentence(lastWord).then((results) => {
98                    ctrl.wordBuffer = results
99                    const nextWord = ctrl.wordBuffer.shift()
100                    //ctrl.story = ctrl.story + " " + nextWord
101                    ctrl.status = 'done'
102                    ctrl.blurb = ""
103                    ctrl.getNextWord(nextWord, 0)
104                })
105            } else {
106                ctrl.getNextRandomWord()
107            }
108        }
109
110        // get a word from datamuse based on the current list of words in the ctrl.nextwords array
111        ctrl.getNextRandomWord = () => {
112            const punctuation = new RegExp(/"['.?=;,:0123456789]"/)
113            //empty the buffer once we get a random word
114            if (ctrl.random(10) > 8) {
115                ctrl.wordBuffer = []
116            }
117            ctrl.totalWords = []
118
119            //get the word with the highest score from the current selection
120
121            //pick the highest word or a random one
122            let chosenWord = ''
123
124            //get last word added to the story
125            const lastWord = ctrl.story.split(" ").pop()
126            console.log("lastWord:" + lastWord);
127
128            // do this until we have a chosen word whcih isn't the last one
129            // to avoid repeating the same word in the story
130            do {
131                if (ctrl.random(10) > 7) {
132
133                    // pick the highest scoring word
134                    ctrl.nextWords.sort((a, b) => {
135                        return a.score - b.score;
136                    });
137
138                    console.log('sorted selection');
139                    console.log(ctrl.nextWords);
140
141                    let hiScoreWord
142                    do {
143                        hiScoreWord = ctrl.nextWords.pop().word
144                        console.log("selecting:" + hiScoreWord)
145                        //avoid using the items below as they hang the datamuse API
146                    } while (punctuation.test(hiScoreWord))
147                    chosenWord = hiScoreWord
148                } else {
149                    console.log('random selection');
150                    // just pick one at random
151                    chosenWord = ctrl.nextWords[ctrl.random(ctrl.nextWords.length)].word
152                }
153
154            } while (chosenWord == lastWord)
155
156            // add the chosen word to the story 
157            //capitalise if necessary
158            if ((lastWord.includes('.') || lastWord.includes('?') || lastWord.includes('!')) && !lastWord.includes('...')) {
159                ctrl.story = ctrl.story + ' ' + capitalise(chosenWord)
160            }
161            else {
162                if (chosenWord == 'i') {
163                    chosenWord = "I"
164                }
165                ctrl.story = ctrl.story + ' ' + chosenWord
166            }
167
168            ctrl.nextWords = []
169            wordService
170                .getNextWord(chosenWord)
171                .then((response) => {
172                    if (response.status == 200) {
173                        ctrl.totalWords = response.data;
174                        console.log('length:' + ctrl.totalWords.length);
175                        const totalWords = ctrl.totalWords.length
176                        //randomise the result set
177                        if (totalWords > 0) {
178
179                            if (totalWords > 20) {
180                                let itemCount = 0;
181                                while (itemCount < 20) {
182                                    const idx = Math.floor(Math.random() * totalWords)
183                                    // don't add duplicates
184                                    if (ctrl.nextWords.includes(ctrl.totalWords[idx]) == false) {
185                                        ctrl.nextWords.push(ctrl.totalWords[idx])
186                                        console.log(ctrl.totalWords[idx]);
187                                        itemCount = itemCount + 1
188                                    }
189                                }
190                            }
191                            else {
192                                ctrl.nextWords = ctrl.totalWords
193                            }
194                            ctrl.nextWords.sort((a, b) => {
195                                return b.score - a.score;
196                            });
197                        }
198                        return response.data
199                    };
200                })
201                .catch(err => {
202                    console.error(err);
203                });
204        }
205
206        // get the next word that the user has selected from the ctrl.wordlist array
207        ctrl.getNextWord = (word, index) => {
208            console.log("ctrl.story:"+ ctrl.story);
209            const lastWord = ctrl.story.split(" ").pop()
210            //ctrl.story = ctrl.story + " " + word;
211
212            if ((lastWord.includes('.') || lastWord.includes('?') || lastWord.includes('!')) && !lastWord.includes('...')) {
213                ctrl.story = ctrl.story + ' ' + capitalise(word)
214            }
215            else {
216                if (word == 'i') {
217                    word = "I"
218                }
219                ctrl.story = ctrl.story + ' ' + word
220            }
221
222            ctrl.nextWords = []
223            ctrl.totalWords = []
224
225            if (ctrl.random(10) > 7) {
226                ctrl.wordBuffer = []
227            }
228
229            wordService
230                .getNextWord(word, index)
231                .then((response) => {
232                    if (response.status == 200) {
233                        ctrl.totalWords = response.data;
234                        console.log('length:' + ctrl.totalWords.length);
235                        //randomise the result set
236                        const totalWords = ctrl.totalWords.length
237                        if (totalWords > 0) {
238                            const totalWords = ctrl.totalWords.length
239                            if (totalWords > 20) {
240                                let itemCount = 0;
241                                while (itemCount < 20) {
242                                    const idx = Math.floor(Math.random() * totalWords)
243                                    // don't add duplicates
244                                    if (ctrl.nextWords.includes(ctrl.totalWords[idx]) == false) {
245                                        ctrl.nextWords.push(ctrl.totalWords[idx])
246                                        console.log(ctrl.totalWords[idx]);
247                                        itemCount = itemCount + 1
248                                    }
249                                }
250                            }
251                            else {
252                                ctrl.nextWords = ctrl.totalWords
253                            }
254                            //sort them in score order
255                            ctrl.nextWords.sort((a, b) => {
256                                return b.score - a.score;
257                            });
258                        }
259                        //loop through all fo the total words and select 10
260                        return response.data
261                    };
262                })
263                .catch(err => {
264                    console.error(err);
265                });
266
267
268        }
269
270    }],
271    templateUrl: "partials/textView.html"
272};
273
274angular
275    .module("autonomicon")
276    .component("textView", textView);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.