43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import math
|
|
import numpy as np
|
|
import matplotlib
|
|
import matplotlib.pyplot as plt
|
|
import os
|
|
|
|
|
|
# Assumes 0 <= x <= 1
|
|
def sigmoid(x, slope=0.1, offset=0, max_x=1, flip=False) -> float:
|
|
assert x <= max_x
|
|
x = x - (max_x / 2) - (offset * max_x / 2)
|
|
s = 1 / (1 + math.exp(-x / slope))
|
|
return max_x - s if flip else s
|
|
|
|
curve_dir = os.path.dirname(__file__)
|
|
|
|
for name, f in [
|
|
('common', lambda x: sigmoid(x, slope=0.05, offset=-0.6, flip=True)),
|
|
('dialect', lambda x: sigmoid(x, slope=0.08, offset=-0.2)),
|
|
('kanji', lambda x: x ** 5),
|
|
('katakana', lambda x: 0 if x > 0.5 else 1),
|
|
('nhk', lambda x: sigmoid(x, slope=0.03, offset=-0.6, flip=True)),
|
|
('wordsum', lambda x: x),
|
|
]:
|
|
plt.rc('font', size=33)
|
|
plt.xlim(-0.05, 1.05)
|
|
plt.ylim(-0.05, 1.05)
|
|
plt.locator_params(nbins=2)
|
|
space = np.linspace(0, 1, 1000)
|
|
p = [f(n) for n in space] #
|
|
plt.plot(space, p, linewidth=5)
|
|
plt.savefig(f"{curve_dir}/{name}.png")
|
|
plt.clf()
|
|
|
|
plt.rc('font', size=33)
|
|
plt.xlim(-0.05, 24.05)
|
|
plt.ylim(-0.05, 1.05)
|
|
plt.locator_params(nbins=3)
|
|
space = np.linspace(0, 24, 1000)
|
|
p = [sigmoid(n, slope=1.4, max_x=24) for n in space] #
|
|
plt.plot(space, p, linewidth=5)
|
|
plt.savefig(f"{curve_dir}/sentence_length.png")
|