Skip to content

Instantly share code, notes, and snippets.

View do-me's full-sized avatar

Dominik Weckmüller do-me

View GitHub Profile
@do-me
do-me / bs_xml_html_parser.py
Last active November 18, 2023 13:18
Beautifoul soup xml/html parser (slower than lxml but more convenient to use)
import re
from bs4 import BeautifulSoup
def remove_consecutive_whitespaces(text):
# Define the regex pattern
pattern = r'[^\S\r\n]*(\r\n|\n|\r)[^\S\r\n]*|([^\S\r\n]){2,}'
# Use re.sub to replace matches with a single whitespace
result = re.sub(pattern, lambda match: ' ' if match.group(2) else match.group(1), text)
@do-me
do-me / xml_html_parser.py
Created November 18, 2023 08:09
Batch XML/HTML Parsing with lxml for better performance than beautifulsoup ~50it/s single-threaded
from lxml import etree
import pyarrow as pa
import pyarrow.parquet as pq
import pandas as pd
from tqdm import tqdm
import os
tqdm.pandas()
import lxml
import cchardet
import re
@do-me
do-me / jupyter_embeddings_batch_chunks.py
Created November 19, 2023 10:46
Jupyter batch processing embeddings for chunks
# Input: folder chunks/chunk_*
# Output: folder chunks/chunk_*_embeddings
# Deletes trash/cache for each iteration to free disk space
import subprocess
def run_shell_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
@do-me
do-me / pandas_pysal.py
Created November 22, 2023 14:29
Pandas Jenks Natural Breaks classifier with pysal
from pysal.viz.mapclassify import NaturalBreaks as nb
import pandas as pd
# import your df here
num_classes = 5
classifier = nb(list(df["score"]), k=num_classes)
classifications = list(classifier.bins)
classifications = [0] + classifications
@do-me
do-me / pandas_hash.py
Created November 24, 2023 16:18
Pandas function to generate unique hash for each row
import hashlib
# Function to generate hash for each row
def generate_hash(row):
hash_object = hashlib.sha256()
# Convert each element in the row to a string and update the hash object
for value in row.values:
hash_object.update(str(value).encode('utf-8'))
# Return the hexadecimal representation of the hash
return hash_object.hexdigest()[:30]
@do-me
do-me / average_embeddings.py
Last active November 25, 2023 09:54
Average embeddings in pandas with numpy.py
import pandas as pd
import numpy as np
# Useful when you indxed document chunks and now need to create average embeddings per document
df = pd.read_parquet("chunk_embeddings.parquet") # ~3GB file
# Convert the embeddings column to a NumPy array
df['embeddings'] = df['embeddings'].apply(np.array)
# Group by filename and calculate the mean of the embeddings
@do-me
do-me / Geoportale.py
Last active November 26, 2023 16:11
Italian Geoportale WMS and WFS mining
import requests
from bs4 import BeautifulSoup
import json
def extract_dataset_info(html):
soup = BeautifulSoup(html, 'html.parser')
dataset_info_list = []
# Find all occurrences of 'h3' and 'p' tags
@do-me
do-me / extract.py
Created December 11, 2023 08:28
MTEB table extractor with bs4
html_table_string = """
<table class="table svelte-1jok1de" style="height: 100%; --bw-svt-p-top: 0px; --bw-svt-p-bottom: 3808.18359375px; --bw-svt-head-height: 37px; --bw-svt-foot-height: 0px; --bw-svt-avg-row-height: 36.97265625px;"><thead class="thead svelte-1jok1de"><tr slot="thead" class="svelte-1bvc1p0"><th aria-sort="none" class="svelte-1bvc1p0" style="width: var(--cell-width-0);"><div class="cell-wrap svelte-1bvc1p0"> <span tabindex="-1" role="button" style="" class="svelte-q8uklq">Rank</span> <div class="sort-button undefined svelte-1bvc1p0"><svg width="1em" height="1em" viewBox="0 0 9 7" fill="none" xmlns="http://www.w3.org/2000/svg" class="svelte-1bvc1p0"><path d="M4.49999 0L8.3971 6.75H0.602875L4.49999 0Z"></path></svg></div></div> </th><th aria-sort="none" class="svelte-1bvc1p0" style="width: var(--cell-width-1);"><div class="cell-wrap svelte-1bvc1p0"> <span tabindex="-1" role="button" style="" class="svelte-q8uklq">Model</span> <div class="sort-button undefined svelte-1bvc1p0"><svg width="1em" he
@do-me
do-me / address_separator.py
Last active December 28, 2023 10:43
Separate street & housnumber
def separate_street_and_number(address):
'''
Split by " " and check for the index of the first element that STARTSWITH a number.
Everything before that index is the street name, so " ".join()
Everything after is the house number.
# K1 | 1-4
# E1 | 15
# Bahnhofstr. | 27
# Marienbrunnen | 10 a
@do-me
do-me / word_counter.py
Created December 19, 2023 15:28
Get the most frequent words in a pandas text column
from collections import Counter
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from tqdm import tqdm
import pandas as pd
import string
# Download NLTK stopwords
import nltk
nltk.download('stopwords')