Skip to content

Instantly share code, notes, and snippets.

@oliverfoggin
oliverfoggin / viewWithTextIn.m
Last active March 9, 2016 06:53
How to centre text using drawInRect...
//Create the rect you would like your text to be inside of...
CGRect maxTextRect = CGRectMake(0, 0, 200, 60);
//Create the attributed string
NSAttributedString *theString = //... do all the setup.
//Find the rect that the string will draw into **inside the maxTextRect**
CGRect actualRect = [theString boundingRectWithSize:maxTextRect.size options:NSStringDrawingUsesLineFragmentOrigin context:nil];
//Offset the actual rect inside the maxTextRect
@niw
niw / UITextFieldLimitLengthDelegate.m
Last active July 1, 2019 16:53
The right solution to limit length of a text field.
/*
Why not simply use textField:shouldChangeCharactersInRange:replacementString:?
===
If you google how to limit a length of text field, you'll see many implementations simply using textField:shouldChangeCharactersInRange:replacementString: like
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return [[textField.text stringByReplacingCharactersInRange:range withString:string] length] <= MAX_LENGTH;
@monikkinom
monikkinom / rnn-lstm.py
Last active September 3, 2019 04:44
Tensorflow RNN-LSTM implementation to count number of set bits in a binary string
#Source code with the blog post at http://monik.in/a-noobs-guide-to-implementing-rnn-lstm-using-tensorflow/
import numpy as np
import random
from random import shuffle
import tensorflow as tf
# from tensorflow.models.rnn import rnn_cell
# from tensorflow.models.rnn import rnn
NUM_EXAMPLES = 10000
# pylint: disable=C0111,too-many-arguments,too-many-instance-attributes,too-many-locals,redefined-outer-name,fixme
# pylint: disable=superfluous-parens, no-member, invalid-name
import sys
sys.path.insert(0, "../../python")
import mxnet as mx
import numpy as np
import cv2, random
from io import BytesIO
from captcha.image import ImageCaptcha
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.optim import lr_scheduler
import torch.utils.data as data
from torch.nn.utils.rnn import pack_padded_sequence as pack, pad_packed_sequence as unpack
import torchaudio
import torchaudio.transforms as tat
@basaundi
basaundi / multi_bleu.py
Last active September 20, 2020 07:28
python rewrite of Moses' multi-bleu.perl; usable as a library
#!/usr/bin/env python
# Ander Martinez Sanchez
from __future__ import division, print_function
from math import exp, log
from collections import Counter
def ngram_count(words, n):
if n <= len(words):
@lampts
lampts / gensim2projector_tf.py
Last active December 7, 2020 22:37
how to convert/port gensim word2vec to tensorflow projector board.
# required tensorflow 0.12
# required gensim 0.13.3+ for new api model.wv.index2word or just use model.index2word
from gensim.models import Word2Vec
import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector
# loading your gensim
model = Word2Vec.load("YOUR-MODEL")
@j314erre
j314erre / text_cnn.py
Created July 13, 2016 00:00
load pre-trained word2vec into cnn-text-classification-tf
import tensorflow as tf
import numpy as np
class TextCNN(object):
"""
A CNN for text classification.
Uses an embedding layer, followed by a convolutional, max-pooling and softmax layer.
"""
def __init__(
@p2
p2 / csv-to-sqlite.py
Created December 4, 2013 23:35
Quickly create a SQLite table from a CSV/TSV file
#!/usr/bin/python
#
# Read a CSV/TSV with a header row (!) and put it into a new sqlite table
import sys
import csv
import sqlite3
class Importer (object):