In the Flexsearch Library, How to search all matching text? - javascript

I have a large text with 10 "hell" words in the document, and I want flexSearch to retrieve all the matches in the full text for me. I currently use the index. search method, but it can only retrieve the first hell, not include the rest. I don't know what's going on.
my core code:
import Flexsearch from "flexsearch";
//英文搜索
const searchENOptions = {
encode: "icase",
tokenize: "reverse",
resolution: 9,
doc: {
id: "key",
field: ["content"],
},
};
this.indexEN = new Flexsearch(searchENOptions);
const { pages } = this.$site;
this.indexEN.add(pages);
const query = this.query.trim().toLowerCase(); // query keywords
let result = null;
if (!query) {
return;
}
const regex = /[\x00-\x7F]/g;
// currently, the result only has one item.
result = this.indexEN.search(query).map((page) => {
return {
...page,
text: this.getSuggestionText(page),
};
});
}
function getSuggestionText(page) {
const content = page.content; // fullText
const queryIndex = content
.toLowerCase()
.indexOf(this.query.toLowerCase());
const queryFirstWord = this.query.split(" ")[0];
let startIndex =
queryIndex === -1
? content.toLowerCase().indexOf(queryFirstWord.toLowerCase())
: queryIndex;
let prefix = "";
if (startIndex > 15) {
startIndex -= 15;
prefix = "... ";
}
const text = page.content.substr(startIndex, SEARCH_RESULT_LENGTH);
return highlightText(text, this.query) + prefix;
}

Related

Loop over a list of ids and foreach id get data from api

Loop over a list of ids and foreach id get data from api but, i can make only 40 requests per 10 seconds, i'm getting data from tbdb but i'm getting kicked by the api here is my code
Popular.findAll({ limit: 25 }).then((filmes) => {
for (let index = 0; index < filmes.length; index++) {
const element = filmes[index];
const id_filme = element.id_filme;
i get each movie id so i can make requests to the api with this function..
function fetchdata(id_filme) {
setTimeout(function () {
axios
.all([
axios.get(
`https://api.themoviedb.org/3/movie/${id_filme}?api_key=${process.env.API_KEY}&language=pt-BR`
),
axios.get(
`https://api.themoviedb.org/3/movie/${id_filme}/videos?api_key=${process.env.API_KEY}&language=pt-BR`
),
])
.then(
axios.spread(function (detalhes, linksyt) {
var filme = detalhes.data;
var youtube = linksyt.data;
// console.log(filme, youtube);
var filmesLista = new Array();
const title = filme.title;
const filmeid = filme.id;
if (Object.keys(filme.genres).length === 0) {
var genre = filme.genres;
} else {
var genre = filme.genres[0].name;
}
const overview = filme.overview;
const poster_path =
"https://image.tmdb.org/t/p/w500" + filme.poster_path;
const release_date = filme.release_date;
const vote_average = parseFloat(filme.vote_average);
const backdrop_path =
"https://image.tmdb.org/t/p/original" + filme.backdrop_path;
const imdb_id = filme.imdb_id;
const runtime = filme.runtime;
if (Object.keys(youtube.results).length === 0) {
var trailer = "";
} else {
var trailer =
"http://www.youtube.com/watch?v=" + youtube.results[0].key;
}
filmesLista.push([
filmeid,
title,
genre,
overview,
poster_path,
release_date,
vote_average,
backdrop_path,
imdb_id,
runtime,
trailer,
]);
console.log(filmesLista);
filmesLista.forEach((i) => {
const filmeid = i[0];
const title = i[1];
const genre = i[2];
const overview = i[3];
const poster_path = i[4];
const release_date = i[5];
const vote_average = i[6];
const backdrop_path = i[7];
const imdb_id = i[8];
const runtime = i[9];
const trailer = i[10];
Detalhes.create({
id_filme: `${filmeid}`,
title: `${title}`,
genre: `${genre}`,
release_date: `${release_date}`,
vote_average: `${vote_average}`,
imdb_id: `${imdb_id}`,
runtime: `${runtime}`,
overview: `${overview}`,
poster_path: `${poster_path}`,
backdrop_path: `${backdrop_path}`,
trailer: `${trailer}`,
});
});
})
);
o++;
if (o < 25) {
fetchdata(id_filme);
}
}, 10000);
console.log("INSERIDO NO BANCO");
}
fetchdata(id_filme);
the code works but, works so fast and i get kicked every time i run with more than 40 ids.. i'm strugling on just make 40 request every 10 secconds. anyone can help me please?

URLs should ends with the observation id in javascript

In total I have two files first one is tester and second is the solution for tester file
Here is my Tester File
const data = require('./data');
const result0 = data.results[0];
const { getTaxonPhotos } = require('./observations');
const isINaturalistPath = (pathname) =>
/(square.jpe?g|original.jpe?g|small.jpe?g|medium.jpe?g|large.jpe?g|_s.jpe?g)$/.test(pathname);
const hasIdQueryParam = (search) => /^\?\d+$/.test(search);
const isUrl = (url) => {
const { pathname, search } = new URL(url);
if (!isINaturalistPath(pathname)) {
throw new Error(`URL path doesn't look like an iNaturalist photo: ${pathname}`);
}
if (!hasIdQueryParam(search)) {
throw new Error(`URL origin doesn't have expected id on query string: ${search}`);
}
return true;
};
describe('Problem 06 - getTaxonPhotos() function', function () {
let sample, samples, sampleData;
beforeEach(() => {
sample = Object.assign({}, result0);
samples = [sample];
sampleData = { results: samples };
});
test('should return an Array of Objects with the right URLs', function () {
let result = getTaxonPhotos(sampleData);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(1);
const photos = result[0];
expect(isUrl(photos.square)).toBe(true);
expect(isUrl(photos.original)).toBe(true);
expect(isUrl(photos.small)).toBe(true);
expect(isUrl(photos.medium)).toBe(true);
expect(isUrl(photos.large)).toBe(true);
});
test('should return an empty Array if missing taxon', function () {
delete sample.taxon;
let result = getTaxonPhotos(sampleData);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(0);
});
test('real-data should produce the expected result', function () {
let result = getTaxonPhotos(data);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(9);
result.forEach((photos) => {
expect(isUrl(photos.square)).toBe(true);
expect(isUrl(photos.original)).toBe(true);
expect(isUrl(photos.small)).toBe(true);
expect(isUrl(photos.medium)).toBe(true);
expect(isUrl(photos.large)).toBe(true);
});
});
test('URLs should end with the observation ID on the query string', () => {
let taxonPhotos0 = getTaxonPhotos(data)[0];
let observationId = result0.id;
expect(taxonPhotos0.square.endsWith(`?${observationId}`)).toBe(true);
expect(taxonPhotos0.original.endsWith(`?${observationId}`)).toBe(true);
expect(taxonPhotos0.small.endsWith(`?${observationId}`)).toBe(true);
expect(taxonPhotos0.medium.endsWith(`?${observationId}`)).toBe(true);
expect(taxonPhotos0.large.endsWith(`?${observationId}`)).toBe(true);
});
});
Here is my solution for the tester file or you can say function to solve the tester code
function getTaxonPhotos(data) {
// TODO
let patt = /(http(s)?:\/\/)/
let arr = Array();
let pho_obj = {
original: "",
square: "",
small: "",
medium: "",
large: ""
};
data.results.forEach(function(element) {
if (element.hasOwnProperty('taxon') && element.taxon.default_photo) {
// Separate values and find url and id in them
let _values = Object.values(element.taxon.default_photo);
let _url = _values.find(sector => sector.match(patt));
let _id = _values.find( element => typeof element === 'number').toString();
let observationId = "1384377507";
// split the url before file extension
_url = _url.substring(0, (_url.indexOf(_id) + _id.length + 1));
// using a for loop with swith to make correct url
for (let i in pho_obj) {
switch (`${i}`) {
case "original":
pho_obj.original = _url + "original.jpg"+`?${observationId}`;
break;
case "square":
pho_obj.square = _url + "square.jpg"+`?${observationId}`;
break;
case "small":
pho_obj.small = _url + "small.jpg"+`?${observationId}`;
break;
case "medium":
pho_obj.medium = _url + "medium.jpg"+`?${observationId}`;
break;
case "large":
pho_obj.large = _url + "large.jpg"+`?${observationId}`;
break;
}
}
arr.push(pho_obj);
}
});
return arr;
}
I successfully completed 3 conditions but 1 last goes wrong can you help me solve this (I test using jest in javascript)
√ should return an Array of Objects with the right URLs (3 ms)
√ should return an empty Array if missing taxon
√ real-data should produce the expected result (5 ms)
× URLs should end with the observation ID on the query string (3 ms)
let taxonPhotos0 = getTaxonPhotos(data)[0];
I don't think you have anything define for data
did you try
let taxonPhotos0 = getTaxonPhotos(sampleData)[0];

how to export all contact by API in hubspot to google spredsheet with app script

I try to export all contacts from hubspot to spreadsheet,
but I do not know how I can do it
You would be wise to use the people api since the Contact API will begin to be shut down after June 15. I've provided a function that I use.
You welcome to it.
Use what you wish. Modify as needed.
function displayCurrentContacts() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Contacts');
sh.clearContents();
const vs = [['Name', 'Emails']];
const resp = People.People.Connections.list('people/me', { personFields: "emailAddresses,names,organizations" });
//Logger.log(resp);
const data = JSON.parse(resp);
let m = 0;
let n = 0;
data.connections.forEach((ob1, i) => {
if (ob1.emailAddresses && ob1.emailAddresses.length > 0) {
let emails = [... new Set(ob1.emailAddresses.map(ob2 => ob2.value))];//used set to insure I get unique list
//let emails = ob1.emailAddresses.map(ob2 => ob2.value);
let name;
m += emails.length;
if (ob1.names && ob1.organizations) {
name = ob1.names[0].displayName + '\n' + ob1.organizations[0].name;
++n;
} else if (ob1.names) {
name = ob1.names[0].displayName;
++n;
} else if (ob1.organizations) {
name = ob1.organizations[0].name;
++n;
}
vs.push([name, emails.sort().join('\n')])
}
});
vs.push([n, m])
sh.getRange(1, 1, vs.length, vs[0].length).setValues(vs)
sh.getRange(2, 1, sh.getLastRow() - 2, sh.getLastColumn()).sort({ column: 1, sortAscending: true });
}

What is the best way to target an array or a string with user input, and log its respective index?

What is the best way to target an array or a string, so that when a user types a letter it finds a match and logs the letter and its index in the array (or the string)?For example (set-up):
GUESS THIS MOVIE:
how to train your dragon
___ __ _____ ____ ______
Type a letter to guess, you have 10 TRIES:
User Typed: o
Result: _o_ _o _____ _o__ ____o_
HERES MY CODE:
var fs = require('fs');
var inquirer = require('inquirer');
var displayProgress = require('./checkGuess');
// var checkGuess = require('./checkGuess');
var PlayFunc = function() {
var blanksArr = [];
var currentWord = [];
this.getData = function() {
var stackOv = "";
fs.readFile("words.txt", "utf8", function(error, data){
if (error) throw error;
dataType = data.toLowerCase();
//data in array
var wordArr = dataType.split(',');
//select random from word from data
var compWord = wordArr[Math.floor(Math.random() * wordArr.length)];//random
//split chosen word
currentWord = compWord.split('');
console.log("========================\n\n\n");
//Looping through the word
for (var i = 0; i <= currentWord.length - 1; i++) {
// pushing blanks
var gArr = blanksArr.push("_");
//HYPHENS, COLONS, SPACES SHOULD BE PASSED
stackOv = currentWord.join("").replace(/[^- :'.]/g, "_");
wordString = currentWord.join("");
}
console.log("GUESS THIS MOVIE: ");
fs.writeFile("blanks.txt", stackOv, (err) => {
if (err) throw err;
console.log(wordString);
fs.readFile('blanks.txt', "utf8",(err, word) => {
if (err) throw err;
// console.log("GUESS THIS MOVIE: " + compWord);
blanksTxt = word.split(''); //console.log(string.join('').replace(/[^-: '.]/g, "_"));
displayProgress = new displayProgress();
displayProgress.checkGuess();
});
});
});
}
}
module.exports = PlayFunc;
ON THE NEXT FILE CALLED checkGuess.js I Plan to do the checking (which goes back to my original question (OP).
var fs = require('fs');
var inquirer = require('inquirer');
var PlayFunc = require('./PlayFunc');
var displayProgress = function (){
// console.log("WORKING CONNECTED CHECKGUESS MODULE");
// PlayFunc = new PlayFunc();
// PlayFunc.getData();
var a = blanksTxt.join(''); console.log(a); //string a
var manipulateThisArray = blanksTxt;//reading from blanks.txt
// console.log(manipulateThisArray);
this.checkGuess = function(){
inquirer.prompt([
{
type: "input",
name: "letter",
message: "Type a letter to guess, you have 10 TRIES:"
}
]).then(function(userInput) {
var correctArray = [];
// console.log(userInput.letter);
letterTyped = userInput.letter;
//logic
//test if we can parse through the array
for (var i = 0; i <= manipulateThisArray.length - 1; i++) {
x = manipulateThisArray[i]; console.log(x);
// if userinput letter-value matches chosen words letter value
// replace this chosen worsa letter with userinput value
// if(letterTyped == x.charAt(i)) {
console.log("THERES A MATCH " + x.charAt(i));
// }else {
// console.log("NO MATCH");
// }
}
});
}
}
// checkGuess();
module.exports = displayProgress;
//declaration of the variables we need
//the original string:
const string = "how to train your dragon";
//a string of characters we want to show
const show = "o";
//A resulting string we can work with
var result = "";
//Now we go over every character of our string and
for(const character of string){
//check if the character is inside the characters we want to show
if(show.includes(character)){
//if so we show it by adding it to the result
result += character;
}else{
//If not we add an _ instead
result += "_";
}
}
//At the end we can show the result
alert(result);
For this i would use the 'foo'.replace('bar', 'bar') and make sure that the String contains the input 'foo'.includes('input') also you could use a RegExp.
To get the index you could do a loop through the length of the String and use the 'foo'.indexOf('input', number) that will return -1 if no coincidence

javascript regex replace expressions found with themselves within tags

I have a form where a user can enter in some code, and then I want to use Javascript to display it back to them in the webpage. I've already used regex in javascript to replace all the < > tags with html keywords &lt and &gt, but I want to highlight in blue all instances of a tag being opened and then ending with a space or a close tag. I can find the expressions I want but I then want to surround each of them with < span> tags.
The actual code is long but Here's some example code that covers what I want to do:
//example of what a user might put in
var text = "<div id='main'>Here is some <b>bold</b> text.</div>";
//Replace all tag symbols with html keywords
text = text.replace(/\r?</g,'&lt');
//now the expression to get what i want to highlight blue
var regExp = /\&lt[a-zA-Z]+(\s|&gt)/g;
And now I want to find the expressions, and replace them all with themselves wrapped inside span tags, like:
text = text.replace(regExp,"<span class='bluefont'>EACH EXPRESSION FOUND</span>");
I don't know how to do this or if it's even possible just using replace, but it would be really handy if it is.
I know there are external libraries for syntax highlighting but I don't want to use any external libraries for this. I'm using [a-zA-Z] instead of checking for legal tag names in html because I want this to work for xml/xhtml too.
There is a simple solution - replace all < and > with entites, next use regexp (\<[a-zA-Z]*\>). But to be exact I would use something else for first replacement, like below.
markedString = htmlentities('My cat is <span>fluffy</span>')
markedString = markedString.replace(new RegExp('(\<[a-zA-Z]*\>)', 'g'), '<span class="marked">$1</span>');
function htmlentities (string, quote_style, charset, double_encode) {
var hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style),
symbol = '';
string = string == null ? '' : string + '';
if (!hash_map) {
return false;
}
if (quote_style && quote_style === 'ENT_QUOTES') {
hash_map["'"] = ''';
}
if (!!double_encode || double_encode == null) {
for (symbol in hash_map) {
if (hash_map.hasOwnProperty(symbol)) {
string = string.split(symbol).join(hash_map[symbol]);
}
}
} else {
string = string.replace(/([\s\S]*?)(&(?:#\d+|#x[\da-f]+|[a-zA-Z][\da-z]*);|$)/g, function (ignore, text, entity) {
for (symbol in hash_map) {
if (hash_map.hasOwnProperty(symbol)) {
text = text.split(symbol).join(hash_map[symbol]);
}
}
return text + entity;
});
}
return string;
}
function get_html_translation_table (table, quote_style) {
var entities = {},
hash_map = {},
decimal;
var constMappingTable = {},
constMappingQuoteStyle = {};
var useTable = {},
useQuoteStyle = {};
// Translate arguments
constMappingTable[0] = 'HTML_SPECIALCHARS';
constMappingTable[1] = 'HTML_ENTITIES';
constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
constMappingQuoteStyle[2] = 'ENT_COMPAT';
constMappingQuoteStyle[3] = 'ENT_QUOTES';
useTable = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
throw new Error("Table: " + useTable + ' not supported');
// return false;
}
entities['38'] = '&';
if (useTable === 'HTML_ENTITIES') {
entities['160'] = ' ';
entities['161'] = '¡';
entities['162'] = '¢';
entities['163'] = '£';
entities['164'] = '¤';
entities['165'] = '¥';
entities['166'] = '¦';
entities['167'] = '§';
entities['168'] = '¨';
entities['169'] = '©';
entities['170'] = 'ª';
entities['171'] = '«';
entities['172'] = '¬';
entities['173'] = '­';
entities['174'] = '®';
entities['175'] = '¯';
entities['176'] = '°';
entities['177'] = '±';
entities['178'] = '²';
entities['179'] = '³';
entities['180'] = '´';
entities['181'] = 'µ';
entities['182'] = '¶';
entities['183'] = '·';
entities['184'] = '¸';
entities['185'] = '¹';
entities['186'] = 'º';
entities['187'] = '»';
entities['188'] = '¼';
entities['189'] = '½';
entities['190'] = '¾';
entities['191'] = '¿';
entities['192'] = 'À';
entities['193'] = 'Á';
entities['194'] = 'Â';
entities['195'] = 'Ã';
entities['196'] = 'Ä';
entities['197'] = 'Å';
entities['198'] = 'Æ';
entities['199'] = 'Ç';
entities['200'] = 'È';
entities['201'] = 'É';
entities['202'] = 'Ê';
entities['203'] = 'Ë';
entities['204'] = 'Ì';
entities['205'] = 'Í';
entities['206'] = 'Î';
entities['207'] = 'Ï';
entities['208'] = 'Ð';
entities['209'] = 'Ñ';
entities['210'] = 'Ò';
entities['211'] = 'Ó';
entities['212'] = 'Ô';
entities['213'] = 'Õ';
entities['214'] = 'Ö';
entities['215'] = '×';
entities['216'] = 'Ø';
entities['217'] = 'Ù';
entities['218'] = 'Ú';
entities['219'] = 'Û';
entities['220'] = 'Ü';
entities['221'] = 'Ý';
entities['222'] = 'Þ';
entities['223'] = 'ß';
entities['224'] = 'à';
entities['225'] = 'á';
entities['226'] = 'â';
entities['227'] = 'ã';
entities['228'] = 'ä';
entities['229'] = 'å';
entities['230'] = 'æ';
entities['231'] = 'ç';
entities['232'] = 'è';
entities['233'] = 'é';
entities['234'] = 'ê';
entities['235'] = 'ë';
entities['236'] = 'ì';
entities['237'] = 'í';
entities['238'] = 'î';
entities['239'] = 'ï';
entities['240'] = 'ð';
entities['241'] = 'ñ';
entities['242'] = 'ò';
entities['243'] = 'ó';
entities['244'] = 'ô';
entities['245'] = 'õ';
entities['246'] = 'ö';
entities['247'] = '÷';
entities['248'] = 'ø';
entities['249'] = 'ù';
entities['250'] = 'ú';
entities['251'] = 'û';
entities['252'] = 'ü';
entities['253'] = 'ý';
entities['254'] = 'þ';
entities['255'] = 'ÿ';
}
if (useQuoteStyle !== 'ENT_NOQUOTES') {
entities['34'] = '"';
}
if (useQuoteStyle === 'ENT_QUOTES') {
entities['39'] = ''';
}
entities['60'] = '<';
entities['62'] = '>';
// ascii decimals to real symbols
for (decimal in entities) {
if (entities.hasOwnProperty(decimal)) {
hash_map[String.fromCharCode(decimal)] = entities[decimal];
}
}
return hash_map;
}
function htmlentites from http://phpjs.org/functions/htmlentities/
function get_html_translation_table from http://phpjs.org/functions/get_html_translation_table/
I'm no regex expert, but I believe you could do it using backreferences in your regular expression.
var regExp = /(\&lt[a-zA-Z]+(\s|&gt))/g; //Note the additional parentheses
text = text.replace(regExp,"<span class='bluefont'>$1</span>");

Categories