Skip to content

Instantly share code, notes, and snippets.

@yuanmai
yuanmai / max_ent.py
Created July 21, 2021 05:48
Maximum Entropy
import numpy as np
def encode(featureset, label, mapping):
encoding = []
for (fname, fval) in featureset.items():
if(fname,fval,label) in mapping:
encoding.append((mapping[(fname,fval,label)],1))
return encoding
def calculate_empirical_fcount(train_toks, mapping):
fcount = np.zeros(len(mapping))
@yuanmai
yuanmai / settings.xml
Last active January 7, 2019 06:47
maven 阿里云镜像
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
@yuanmai
yuanmai / CpsRegex.fsx
Last active August 31, 2017 08:45
用 CPS 实现字符串匹配。基于讲座 https://youtu.be/cnhb4M8-J5M
type Re =
| Lit of char
| Seq of Re * Re
| Or of Re * Re
| Star of Re
let rec match2 (re: Re) (str: char list) k =
match re with
| Lit(c) -> match str with
| [] -> false
@yuanmai
yuanmai / 4-7-8.py
Created November 24, 2016 15:04
HOW TO FALL ASLEEP and RELAX IN 60 SECONDS - THE 4-7-8 BREATHING TECHNIQUE EXPLAINED
#!/usr/bin/python
import time
# for more info https://www.youtube.com/watch?v=oiV6sbuQUNc
def round():
for i in "Inhale 2 3 4 Hold 2 3 4 5 6 7 Exhale 2 3 4 5 6 7 8".split(" "):
print i
time.sleep(1)
@yuanmai
yuanmai / JRegressTest.java
Last active September 26, 2016 08:41
回归测试框架雏形:JRegress.recording() 记录运行结果, JRegress.matching() 比较实际和上次记录的结果
package jregress;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
def has_factor(factor, word):
return lambda n: word if n % factor == 0 else ''
def has_substring(i, word):
return lambda n: word if str(i) in str(n) else ''
def concat(rule1, rule2):
return lambda n: rule1(n) + rule2(n)
def one_of(rule1, rule2):
@yuanmai
yuanmai / option.py
Last active November 12, 2015 08:15
Option price monte carlo. You can cross validate the pricing on http://www.optioncombo.com/#
from math import exp
from random import gauss, shuffle
interest=.01
vol=.15
T = 1.0/365
def moved(x, _):
return x * exp((interest-.5*vol**2)*T + vol*T**.5*gauss(0,1))
St0=400.0
(defn neighbours [cell]
(map (partial mapv + cell)
[[-1 -1] [-1 0] [-1 1]
[ 0 -1] [ 0 1]
[ 1 -1] [ 1 0] [ 1 1]]))
(def live? #{[true 3] [true 2] [false 3]})
(defn step [live-cells]
(set
@yuanmai
yuanmai / shell.sh
Created June 26, 2014 07:20
ruby simple server
ruby -run -e httpd . -p 3000

Feature Toggle

$(document).one('feature-a', '#some-id', function() {
  $(document).on('some-event', '#some-ids', handler);
});

//Turn on feature a
$('#a, #b').trigger('feature-a');