Skip to content

Instantly share code, notes, and snippets.

View w00lf's full-sized avatar

Mikhail T w00lf

View GitHub Profile
class BatchCreatorService < Struct.new(:model, :to_insert_attrs)
include Service
def call
raw_connection = get_raw_connection
keys = to_insert_attrs.first.keys
@types_hash = Hash[Event.column_types.map{|x,y| [x,y.type.to_s] }]
raw_connection.exec("COPY #{model.table_name} (#{keys.join(',')}) FROM STDIN WITH CSV DELIMITER ';'")
to_insert_attrs.each do |attrs|
@w00lf
w00lf / Dockerfile
Created January 11, 2017 12:27
Mruby nginx redirects with redis
FROM centos:latest
MAINTAINER Mikl Taraskin <example@gmail.com>
ADD nginx-build-rpm /tmp/nginx-build-rpm
WORKDIR /tmp/nginx-build-rpm
RUN rpm -Uvh nginx-build.rpm
RUN yum install -y redis
RUN mkdir -p /etc/nginx/include.d/rewrites/
ADD example.rb /etc/nginx/include.d/rewrites/example.rb
ADD nginx.conf /etc/nginx/nginx.conf
@w00lf
w00lf / cosine_similarity.rb
Created June 13, 2017 10:43
Computes cosine similarity of two text, matched by words.
# Inspired by: https://stackoverflow.com/questions/1746501/can-someone-give-an-example-of-cosine-similarity-in-a-very-simple-graphical-wa
# And: https://github.com/agarie/measurable/blob/8ff8efbab1a0892bdddf6e865dd8864956168a91/lib/measurable/cosine.rb
# https://github.com/agarie/measurable/blob/8ff8efbab1a0892bdddf6e865dd8864956168a91/lib/measurable/euclidean.rb
# Accept two text adn calcualtes cosine similarity by words of these texts, returns Float, between 0.0(not similat at all) and 1.0(identical)
def cosine_similarity(one, two)
indexes = [ one, two ].map { |text| text.scan(/[a-zA-Z]{1,}/) }.flatten.uniq
counters = [ one, two ].map do |text|
counter = text.scan(/[a-zA-Z]{1,}/).reduce(Hash.new(0)) { |x,y| x.tap {|n| n[y] += 1 } }
indexes.map { |key| counter[key] }