Skip to content

Instantly share code, notes, and snippets.

View stekhn's full-sized avatar

Steffen Kühne stekhn

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / weightedMean.js
Last active September 25, 2024 03:23
Weighted arithmetic mean (average) in JavaScript
function weightedMean(arrValues, arrWeights) {
var result = arrValues.map(function (value, i) {
var weight = arrWeights[i];
var sum = value * weight;
return [sum, weight];
}).reduce(function (p, c) {
@stekhn
stekhn / stdDev.js
Last active July 8, 2016 17:58
Standard deviation in JavaScript
function stdDev(arr) {
var avg = mean(arr);
var squareDiffs = arr.map(function (value) {
var diff = value - avg;
var sqrDiff = diff * diff;
return sqrDiff;
});
@stekhn
stekhn / gist:16584703877ca138e5a8
Created February 17, 2016 08:44
Generate unique IDs in Excel
=IF(COUNTIFS($B$1:B3;B3)>1;INDEX($A$1:A2;IFERROR(MATCH(B3;$B$1:B2;0);1));MAX($A$1:A2)+1)
@stekhn
stekhn / mapValue.js
Created November 17, 2015 09:21
Map value to range
function mapValue(value, fromMin, toMin, fromMax, toMax) {
return (value - fromMin) / (toMin - fromMin) * (toMax - fromMax) + fromMax;
}