1'use strict' 2 3function lovecraftService2($http, LOVECRAFT_API_URL, DEBUG_ON) { 4 //utlity function to capitalise first letter in a string 5 6 async function getNextLovecraft(lastWord) { 7 8 //const relation = 'lc=' 9 const qryString = `${LOVECRAFT_API_URL}` 10 let startPos = -1 11 12 if (DEBUG_ON) { 13 console.log(`qryString: ${qryString}`); 14 } 15 let count = 0 16 do { 17 const results = await getAPICall(qryString); 18 //clean up sentence 19 var punctuationless = results.replace(/.,[\/#!$%\^&\*:{}=\-_`~()]/g, "") 20 var resultsArray = punctuationless.replace(/\s{2,}/g, " ").split(" ") 21 22 //look for a word in the sentence that matches the last one in the story 23 if (lastWord !== null && lastWord !== undefined) { 24 console.log("lastword:" + lastWord); 25 startPos = resultsArray.indexOf(lastWord) 26 } else { 27 // start at a random point in the sentence, 28 // but make sure that we are at least 5 words away from the end 29 console.log("no splice - finding random start point"); 30 if (resultsArray.length > 5) { 31 // don't just return the beginning of the sentence 32 const start = Math.floor(Math.random() * (resultsArray.length - 5)) 33 resultsArray = resultsArray.slice(start, resultsArray.length) 34 } 35 36 } 37 count = count + 1 38 } while (startPos == -1 && (lastWord !== null && lastWord !== undefined) && count <20) 39 40 if(startPos!==-1){ 41 resultsArray = resultsArray.splice(startPos + 1, resultsArray.length - startPos) 42 console.log("spliced at:" + startPos + 1); 43 } 44 45 //fix for occaisonal case where this is empty 46 if (resultsArray.length == 0) { 47 results.Array[0] = "the" 48 } 49 resultsArray[0] = resultsArray[0].toLowerCase() 50 if (resultsArray.length > 10) { 51 resultsArray.length = 10 52 } 53 return resultsArray 54 } 55 56 function getAPICall(qryString) { 57 const headers = { 58 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' 59 } 60 return $http.get(qryString, headers).then((response) => { 61 if (DEBUG_ON) { 62 console.log(response); 63 } 64 65 // split sentence into words only return first 10 66 let results = response.data.data[0].sentence.replace('. . .', ' ') 67 results = results.replace('...', '') 68 console.log('Raw HP data:' + results); 69 return results 70 }).catch((err) => { 71 console.error('An error occurred querying the random Lovecraft API'); 72 console.error(err); 73 return err 74 }) 75 } 76 77return { 78 getNextLovecraft, getNextLovecraft 79} 80 81} 82angular 83 .module('autonomicon') 84 .factory('lovecraftService2', lovecraftService2);
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.