Skip to content

Instantly share code, notes, and snippets.

@asemic-horizon
Created November 29, 2021 19:04
Show Gist options
  • Save asemic-horizon/c7cc449e757f3dba6e22addee61c73f9 to your computer and use it in GitHub Desktop.
Save asemic-horizon/c7cc449e757f3dba6e22addee61c73f9 to your computer and use it in GitHub Desktop.
better than GPT-3?
import sys, itertools, functools, more_itertools
import numpy as np, os, networkx as nx
import community as community_louvain
from glob import glob
from time import time
def louvain(G, resolution):
partition = community_louvain.best_partition(
G, resolution=resolution, random_state=0
)
distribution = [
[q for q in partition.keys() if partition[q] == p] for p in partition.values()
]
return (nx.subgraph(G, mod) for mod in distribution)
def make_graph(source):
base = nx.Graph()
with open(source) as f:
lines = f.readlines()
eta = 2 * (len(lines) / 50) ** 2
print(len(lines), eta / 60, eta / 3600)
for line in lines:
base = nx.compose(base, nx.complete_graph(line.split(" ")))
return base
def center(graph):
centralities = nx.pagerank(graph)
return sorted(centralities.items(), key=lambda x: x[1], reverse=True)[
len(centralities) // 2
][0]
def traversals(graph):
return (
nx.topological_sort(nx.dfs_tree(mod,center(mod))) for mod in louvain(graph, resolution=1.0)
)
def punctuation(length, period_prob=1 / 8, paragraph_prob=1 / 30):
periods = np.random.poisson(period_prob, length)
paragraphs = np.random.poisson(paragraph_prob, length)
return (
period_count * "." + paragraph_count * "\n"
for (period_count, paragraph_count) in zip(periods, paragraphs)
)
if __name__ == "__main__":
sources = sys.argv[1]
graph = make_graph(sources)
text = " ".join(
set(more_itertools.collapse(zip(traversals(graph), punctuation(len(graph)))))
)
with open(f"output {time()}.txt", "w") as fp:
fp.write(text)
with open(f"output.txt", "w") as fp:
fp.write(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment