Skip to content

Commit f3ad311

Browse files
committed
Version bump to v0.1.3: A string generator; Fixed several bugs
1 parent 2c59f25 commit f3ad311

File tree

14 files changed

+135
-7
lines changed

14 files changed

+135
-7
lines changed

cyaron/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from .io import IO
22
from .graph import Graph, Edge
3+
from .str import String
34
from .utils import *
5+
from .consts import *
46
from random import randint, randrange, uniform, choice, random

cyaron/consts.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import math
2+
import string
3+
4+
PI = math.pi
5+
E = math.e
6+
7+
ALPHABET_SMALL = string.ascii_lowercase
8+
ALPHABET_CAPITAL = string.ascii_uppercase
9+
ALPHABET = string.ascii_uppercase
10+
NUMBERS = string.digits
11+
SENTENCE_SEPARATORS = ',,,,,,,;;:' # 70% ',' 20% ';' 10% ':'
12+
SENTENCE_TERMINATORS = '....!' # 80% '.' 20% '!'
13+

cyaron/graph.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .utils import *
12
import random
23

34

@@ -27,7 +28,7 @@ def to_str(self, **kwargs):
2728
edge_buf = []
2829
for edge in self.iterate_edges():
2930
edge_buf.append(
30-
Edge(tmp[edge.start], tmp[edge.end], edge.weight))
31+
Edge(new_node_id[edge.start], new_node_id[edge.end], edge.weight))
3132
random.shuffle(edge_buf)
3233
for edge in edge_buf:
3334
if not self.directed and random.randint(0, 1) == 0:
@@ -68,7 +69,7 @@ def flower(point_count, **kwargs):
6869
def tree(point_count, chain=0, flower=0, **kwargs):
6970
directed = kwargs.get("directed", False)
7071
weight_limit = kwargs.get("weight_limit", (1, 1))
71-
if not isinstance(weight_limit, tuple):
72+
if not list_like(weight_limit):
7273
weight_limit = (1, weight_limit)
7374
weight_gen = kwargs.get(
7475
"weight_gen", lambda: random.randint(
@@ -102,7 +103,7 @@ def tree(point_count, chain=0, flower=0, **kwargs):
102103
def binary_tree(point_count, left=0, right=0, **kwargs):
103104
directed = kwargs.get("directed", False)
104105
weight_limit = kwargs.get("weight_limit", (1, 1))
105-
if not isinstance(weight_limit, tuple):
106+
if not list_like(weight_limit):
106107
weight_limit = (1, weight_limit)
107108
weight_gen = kwargs.get(
108109
"weight_gen", lambda: random.randint(
@@ -117,7 +118,7 @@ def binary_tree(point_count, left=0, right=0, **kwargs):
117118
can_right = {1}
118119
graph = Graph(point_count, directed)
119120
for i in range(2, point_count + 1):
120-
edge_pos = random.uniform(0, 1)
121+
edge_pos = random.random()
121122
node = 0
122123
# Left
123124
if edge_pos < left or left + right < edge_pos <= (1.0 - left - right) / 2:
@@ -137,7 +138,7 @@ def binary_tree(point_count, left=0, right=0, **kwargs):
137138
def graph(point_count, edge_count, **kwargs):
138139
directed = kwargs.get("directed", False)
139140
weight_limit = kwargs.get("weight_limit", (1, 1))
140-
if not isinstance(weight_limit, tuple):
141+
if not list_like(weight_limit):
141142
weight_limit = (1, weight_limit)
142143
weight_gen = kwargs.get(
143144
"weight_gen", lambda: random.randint(
@@ -155,7 +156,7 @@ def hack_spfa(point_count, **kwargs):
155156
directed = kwargs.get("directed", False)
156157
extraedg = kwargs.get("extra_edge", 2)
157158
weight_limit = kwargs.get("weight_limit", (1, 1))
158-
if not isinstance(weight_limit, tuple):
159+
if not list_like(weight_limit):
159160
weight_limit = (1, weight_limit)
160161
weight_gen = kwargs.get(
161162
"weight_gen", lambda: random.randint(

cyaron/io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .utils import *
12
import subprocess
23

34

cyaron/str.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from .consts import ALPHABET_SMALL, SENTENCE_SEPARATORS, SENTENCE_TERMINATORS
2+
from .utils import *
3+
import random
4+
5+
6+
class String:
7+
@staticmethod
8+
def random(length_range, **kwargs):
9+
length = length_range
10+
if list_like(length_range):
11+
length = random.randint(length_range[0], length_range[1])
12+
charset = kwargs.get("charset", ALPHABET_SMALL)
13+
14+
if list_like(charset):
15+
return random.choice(charset)
16+
else:
17+
return "".join(random.choice(charset) for i in range(length))
18+
19+
@staticmethod
20+
def random_sentence(word_count_range, **kwargs):
21+
word_count = word_count_range
22+
if list_like(word_count_range):
23+
word_count = random.randint(word_count_range[0], word_count_range[1])
24+
25+
word_length_range = kwargs.get("word_length_range", (3, 8))
26+
first_letter_uppercase = kwargs.get("first_letter_uppercase", True)
27+
charset = kwargs.get("charset", ALPHABET_SMALL)
28+
29+
word_separators = kwargs.get("word_separators", " ")
30+
if word_separators is None or len(word_separators) == 0:
31+
word_separators = [""]
32+
33+
sentence_terminators = kwargs.get("sentence_terminators", SENTENCE_TERMINATORS)
34+
if sentence_terminators is None or len(sentence_terminators) == 0:
35+
sentence_terminators = [""]
36+
37+
words = []
38+
for i in range(word_count):
39+
words.append(String.random(word_length_range, charset=charset))
40+
if first_letter_uppercase:
41+
words[0] = words[0].capitalize()
42+
43+
# We cannot just `sentence_separators.join()` here
44+
# since we want to randomly select one on each join
45+
sentence = reduce(lambda x, y: x + random.choice(word_separators) + y, words)
46+
sentence += random.choice(sentence_terminators)
47+
48+
return sentence
49+
50+
@staticmethod
51+
def random_paragraph(sentence_count_range, **kwargs):
52+
sentence_count = sentence_count_range
53+
if list_like(sentence_count_range):
54+
sentence_count = random.randint(sentence_count_range[0], sentence_count_range[1])
55+
56+
first_letter_uppercase = kwargs.get("first_letter_uppercase", True)
57+
kwargs["first_letter_uppercase"] = False
58+
59+
termination_percentage = kwargs.get("termination_percentage", 0.3)
60+
if not 0 <= termination_percentage <= 1:
61+
raise Exception("Invalid termination_percentage")
62+
63+
sentence_joiners = kwargs.get("sentence_joiners", " ")
64+
if sentence_joiners is None or len(sentence_joiners) == 0:
65+
sentence_joiners = [""]
66+
67+
sentence_separators = kwargs.get("sentence_separators", SENTENCE_SEPARATORS)
68+
if sentence_separators is None or len(sentence_separators) == 0:
69+
sentence_separators = [""]
70+
71+
sentence_terminators = kwargs.get("sentence_terminators", SENTENCE_TERMINATORS)
72+
if sentence_terminators is None or len(sentence_terminators) == 0:
73+
sentence_terminators = [""]
74+
kwargs["sentence_terminators"] = None
75+
76+
sentences = []
77+
capitalize_next_sentence = True
78+
for i in range(sentence_count):
79+
string = String.random_sentence(**kwargs)
80+
sep_or_term = random.random()
81+
82+
if capitalize_next_sentence and first_letter_uppercase:
83+
string = string.capitalize()
84+
85+
if sep_or_term < termination_percentage or i == sentence_count - 1:
86+
string += random.choice(sentence_terminators)
87+
capitalize_next_sentence = True
88+
else:
89+
string += random.choice(sentence_separators)
90+
capitalize_next_sentence = False
91+
92+
sentences.append(string)
93+
94+
paragraph = reduce(lambda x, y: x + random.choice(sentence_joiners) + y, sentences)
95+
return paragraph
96+

cyaron/tests/general_test.py

Whitespace-only changes.

cyaron/tests/graph_test.py

Whitespace-only changes.

cyaron/tests/io_test.py

Whitespace-only changes.

cyaron/tests/str_test.py

Whitespace-only changes.

cyaron/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
def ati(array):
22
return [int(i) for i in array]
33

4+
def list_like(data):
5+
return isinstance(data, tuple) or isinstance(data, list)

0 commit comments

Comments
 (0)