Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View kkew3's full-sized avatar

Kaiwen kkew3

View GitHub Profile
@kkew3
kkew3 / eval_coherence_gensim_sklearn_lda.py
Last active December 23, 2023 03:42
The approach to evaluate scikit-learn topic model in terms of coherence with gensim using existing vocabulary.
from collections import Counter
from typing import Dict, Union, List
import numpy as np
from scipy import sparse
import pandas as pd
import spacy
from sklearn.datasets import fetch_20newsgroups
from sklearn.decomposition import LatentDirichletAllocation
from gensim.models.coherencemodel import CoherenceModel
@kkew3
kkew3 / .msmtprc
Last active November 20, 2023 07:45
msmtprc for sina email account
# replace 'your_user_name' with your user name
account your_user_name@sina.com
host smtp.sina.com
port 465
tls on
tls_starttls off
auth on
user your_user_name
from your_user_name@sina.com
# paste your authentication code (客户端授权码) here,
@kkew3
kkew3 / gtranslate.py
Last active August 11, 2023 08:58
Python script that invokes Google translation, depending on `requests` library.
import json
import sys
import requests
tl, query = sys.argv[1:]
sl = 'auto'
ua = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) '
'Gecko/20100101 Firefox/109.0')
query = query.strip()
@kkew3
kkew3 / custom.applescript
Last active April 7, 2023 14:08
Setting Alfred Terminal to iTerm2 without wrangling around current iTerm2 window.
# Paste this script to "Alfred Preference > Advanced > Terminal",
# selecting "Application" to "Custom".
#
# In addition, create an iTerm2 profile with string "Alfred" in its Name,
# for example, "Working with Alfred".
#
# The script below will open the first iTerm2 session with such profile,
# or create a new window with that profile if not found. The command
# will be executed there.
@kkew3
kkew3 / cron_calendar.py
Created March 11, 2023 08:40
Convert Apple cron-style time notation to an array of StartCalendarInterval (launchd plist key) dicts.
def parse_crontab_field(acc_values: range, acc_names: dict, string: str):
"""
Parse a field of crontab from ``string``.
>>> values = range(1, 8)
>>> names = dict(zip(['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'],
... values))
>>> parse_crontab_field(values, names, '*')
[None]
>>> parse_crontab_field(values, names, '*/2')
@kkew3
kkew3 / sum_filesize.sh
Last active February 17, 2023 17:27 — forked from fsteffenhagen/sum_filesize.sh
sum human readable file sizes from `du` with `numfmt` and `perl`
# Require: [`fd`](https://github.com/sharkdp/fd.git)
PATTERN='XXX' # Assume this would be a directory
fd -0 -td "$PATTERN" \
| xargs -0 du -sh \
| numfmt --from=auto --to=none \
| perl -ane 'BEGIN { my $sum = 0; } $sum += $F[0]; print; END { print $sum, " totoal\n"; }' \
| numfmt --from=none --to=si
@kkew3
kkew3 / hull_plot.py
Last active February 5, 2023 21:27 — forked from nicoguaro/hull_plot.py
Plot the convex hull around a set of points as a shaded polygon.
# -*- coding: utf-8 -*-
"""
Plot the convex hull around a set of points as a
shaded polygon.
@author: Nicolas Guarin Zapata and Kaiwen
@date: February 6, 2023
"""
import numpy as np
from scipy.spatial import ConvexHull
@kkew3
kkew3 / import_imgs.pl
Last active February 7, 2023 12:28
Use (a little more intelligent) bisection algorithm to resize images under $from_dir to appropriate small size, and move them to $to_dir. The whole process is performed interactively. This script is designed for macOS and requires imagemagick to run.
use warnings;
use strict;
use File::Basename qw(fileparse);
use File::Copy qw(cp mv);
######################################################################
# Configuration #
######################################################################
@kkew3
kkew3 / nums2ranges.py
Created January 28, 2023 05:06
Compress a list of integers into a list of ranges. For example, `1 2 3 4 5 7 8 9` becomes `1:6 7:10`. The code can be used both as a library and as an executable.
#!/usr/bin/env python3
def nums2ranges(nums):
"""
>>> list(nums2ranges([]))
[]
>>> list(nums2ranges([0, 1, 2, 3, 4]))
[slice(0, 5, None)]
>>> list(nums2ranges([0, 1, 4, 5, 6]))
@kkew3
kkew3 / bisectimgresizer.py
Created December 29, 2022 10:57
Compress image to just below the given size upper bound with `imagemagick` using bisection algorithm.
#!/usr/bin/env python3
import os
import re
import sys
import shlex
import shutil
import argparse
import subprocess
import logging