Skip to content

Instantly share code, notes, and snippets.

View macleginn's full-sized avatar

Dmitry Nikolayev macleginn

View GitHub Profile
@macleginn
macleginn / pages_from_pdf.sh
Created March 14, 2020 12:33
A bash/zsh function to easily extract pages from .pdf files using qpdf
# params:
# $1 input-file path,
# $2 page range (e.g., "1-1", "10-39", "5,9-12"),
# $3 output-file path
# ex.: pages_from_pdf input.pdf "1,3,8-9" test.pdf
# qpdf should be installed
function pages_from_pdf() {
qpdf $1 --pages $1 $2 -- $3
}
from itertools import combinations, permutations
from collections import Counter
import gurobipy as gb
from gurobipy import GRB
def get_pairwise_ordering(all_deprels: set, training_set_constraints: Counter):
'''
Solves an integer program and returns a non-loopy ordering
@macleginn
macleginn / first_comma_collocates.py
Created October 2, 2020 09:23
A script for extracting first-comma collocates from the Bible corpus.
import re
import os
from math import log
from collections import Counter
import pandas as pd
def logL(p, k, n):
return k * log(p) + (n - k) * log(1 - p)
import pandas as pd
import matplotlib.pyplot as plt
d = pd.read_excel('spectrograms-relative-20.xlsx', header=None)
# Combine the first two columns in a new index
index_col = [ f'{a}-{b}' for a, b in zip(d.iloc[:,0], d.iloc[:,1]) ]
d.index = index_col
# Delete old index columns
del d[0]
del d[1]
@macleginn
macleginn / get_roberta_word_embeddings.py
Created June 21, 2021 07:17
Code for extracting word embeddings from RoBERTa
def rm_whitespace(s):
if s.startswith('Ġ'):
return s[1:]
else:
return s
def get_tokens_with_ranges(input_string, tokenizer):
'''
RoBERTa prepends 'Ġ' to the beginning of what it
# Собираем вместе все возможные знаки пунктуации
import sys
from unicodedata import category
chrs = (chr(i) for i in range(sys.maxunicode + 1))
punctuation = set(c for c in chrs if category(c).startswith("P"))
# Дефис бывает внутри слов
punctuation.remove('-')
def tokenize(s, lower_case=False):
@macleginn
macleginn / simulation_step.py
Last active February 11, 2022 12:01
A step in the simulation of random feature spread on a network guided by NPM
import numpy as np
# We're given an n by n distance matrix *D* with transfer
# probabilities for a given pair of nodes (for any feature),
# a feature matrix *M*, and a dropout probability p_d.
# We convert the transfer probabilities to no-transfer probabilities
# and take their logs
L = np.log(1 - D)
@macleginn
macleginn / predict_from_CLS.py
Last active June 3, 2022 13:54
Training and evaluation code for a simple model that predicts a token removed from a sentence
import json
from math import ceil
from random import shuffle
import torch
import torch.nn as nn
from transformers import AutoTokenizer, AutoModel
from transformers import AdamW, get_scheduler
import os
import sys
import shutil
def copy_tree(src, dst):
'''
Copy a directory tree from src to dst ignoring dangling
symlinks, retrieving files symlinks point to, and
breaking the cycles, i.e. never copying the same
@macleginn
macleginn / clusterise_domain.py
Created December 20, 2022 08:28
Clusterisation of fine-grained CMP domains based on SBERT sentence similarities
from collections import defaultdict
from itertools import combinations
import pandas as pd
import numpy as np
from sentence_transformers import SentenceTransformer, util
def compute_kernel_bias(vecs, k=None):
"""
Code taken from: https://github.com/bojone/BERT-whitening