Skip to content

Instantly share code, notes, and snippets.

View stekhn's full-sized avatar

Steffen Kühne stekhn

View GitHub Profile
@stekhn
stekhn / weightedStdDev.js
Last active July 12, 2016 20:59
Discrete random variable standard deviation in JavaScript
function weightedStdDev(values, weights) {
var avg = weightedMean(values, weights);
var result = values.map(function (value, i) {
var weight = weights[i];
var diff = value - avg;
var sqrDiff = weight * Math.pow(diff, 2);
@stekhn
stekhn / weightedMedian.js
Created July 13, 2016 07:16
Weighted median in JavaScript
function weightedMedian(values, weights) {
var midpoint = 0.5 * sum(weights);
var cumulativeWeight = 0;
var belowMidpointIndex = 0;
var sortedValues = [];
var sortedWeights = [];
@stekhn
stekhn / prepareElastic.js
Last active April 13, 2023 00:52
Creates and prepares an Elasticsearch index, using the Node.js client. Closes the index before putting settings and mappings. The response and error handlers are optional, remove them if necessary.
var elastic = require('elasticsearch');
var client = new elastic.Client({ host: 'localhost:9200' });
var index = 'myindex';
var type = 'document';
(function init() {
Promise.resolve()
.then(deleteIndex, handleError)
@stekhn
stekhn / log-to-slack.sh
Last active September 3, 2020 08:43
Stream a log file (Apache, Ngix etc.) to a Slack channel with webhooks. From the Postman blog: http://blog.getpostman.com/2015/12/23/stream-any-log-file-to-slack-using-curl/
#!/bin/bash
tail -n0 -F "$1" | while read LINE; do
(echo "$LINE" | grep -e "$3") && curl -X POST --silent --data-urlencode \
"payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "$2";
done
@stekhn
stekhn / ocr.sh
Created September 13, 2016 11:08
Bash script to convert image PDFs to text using OCR. The script uses pdftk, imagemagick, ghostscript and tesseract-ocr.
#!/bin/sh
#needs: pdftk, imagemagick, ghostscript and tesseract-ocr
#pdf should be 300dpi or higher resolution
#set -o errexit
function success () {
echo "\033[33;32m ✓ \033[33;00m"
}
@stekhn
stekhn / extract.js
Created January 3, 2017 10:49
Extract text from PDF files (with images) using Node.js
// Extract text from PDF files (with images)
// Installation guide: https://github.com/nisaacson/pdf-extract
var extract = (function() {
'use strict';
var fs = require('fs');
var path = require('path');
var pdfExtract = require('pdf-extract');
@stekhn
stekhn / merkel-terminkalender.json
Created January 30, 2017 10:31
Sitemap configuration for scraping Angela Merkel's calender using the webscrape.io Chrome extension
{
"selectors": [
{
"parentSelectors": [
"_root"
],
"type": "SelectorLink",
"multiple": true,
"id": "month",
"selector": "div.calendar-switcher.months ul li a",
@stekhn
stekhn / form.html
Created May 27, 2017 15:24
Change URL location and parameters with a simple a HTML form. The query strings gets constructed on form submit. No JavaScript required.
<form action="search">
<fieldset>
<legend>Search string</legend>
<input type="search" name="query" value="" placeholder="Enter text">
</fieldset>
<fieldset>
<legend>Search for</legend>
<input type="radio" name="type" value="name" checked="checked">
@stekhn
stekhn / gist:c50df43c32c063ddbdb3cf3be2c199fd
Created October 2, 2017 13:44
Generate alpha(numeric) IDs from aa to zz in Excel
=CHAR((ROW(A1)-1)/26+97)&CHAR(MOD(ROW(A1)-1;26)+97)
@stekhn
stekhn / shareTheLove.js
Last active December 8, 2017 14:46
Infinite recursion with the sole purpose of sharing love
(function shareTheLove(♥) {
if (♥ < Infinity) {
shareTheLove(++♥)
}
})(0);