Skip to content

Instantly share code, notes, and snippets.

View w00lf's full-sized avatar

Mikhail T w00lf

View GitHub Profile
@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] }
@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
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 / application.rb
Last active August 29, 2015 14:22 — forked from tdl/application.rb
# config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env)
module MyApp
class Application < Rails::Application
# configure Rails to inject this middleware. Must be at least after Rails logger!
for i in {0..100}; do printf "\r"; for(( k=0; k < $i; k++ )); do sum=`expr $i + $k`; num=`expr $sum % 2`; printf "`[ $num == 0 ] && echo '\' || echo '/'`"; done; sleep 0.3; done
@w00lf
w00lf / gist:8840485
Created February 6, 2014 08:43
FreeBSD virtualbox
ftp://ftp.freebsd.org/pub/FreeBSD/snapshots/VM-IMAGES/9.2-STABLE/amd64/20140203/
@w00lf
w00lf / parse_xml.rb
Created January 22, 2014 07:13
Filling tables with objects from xml, with forked insertion and also creation of dump for fast insert.
namespace :fias do
namespace :parse_xml do
class DumpCreator
# creates psql dump from template
attr_reader :target_file, :current_attributes
def initialize(target_file)
@target_file = target_file
target_file = File.open(target_file, 'a')
@stack = []
@w00lf
w00lf / sql_rocket
Created December 12, 2013 19:40 — forked from boblail/sql_rocket
#!/usr/bin/env ruby
# encoding: utf-8
# https://gist.github.com/boblail/5587579
require 'benchmark'
require 'nokogiri'
require 'progressbar'
require 'tempfile'
class SqlRocket < Nokogiri::XML::SAX::Document
@w00lf
w00lf / classic_hard.rb
Created October 28, 2013 08:21
Пример сложного запроса с подзапросом, выбираем номера книжек раздела, после чего выбираем авторов, книжки у которых иданы и принадлежат типу и номер входит в номера книжек раздела
def self.for_classic(compilation = nil)
if compilation.nil?
classic_ids_query = sprintf('SELECT "books".id FROM "books"
INNER JOIN "books_products" ON "books_products".book_id = "books".id
INNER JOIN "products" ON "products".id = "books_products".product_id
WHERE (products.id = %d)
AND ("books"."status" IN (%s, %s))', Product.classic_product.id, "E'published'", "E'republishing'")
else
classic_ids_query = sprintf('SELECT "books".id FROM "books"
INNER JOIN "books_compilations" ON "books".id = "books_compilations".book_id
@w00lf
w00lf / minimum_notation.java
Created September 3, 2013 14:33
****************Наименьшая система счисления**************** http://acmp.ru/index.asp?main=task&id_task=315
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] argv) throws IOException,Exception{
new Main().run();
}
public void run() throws IOException,Exception{