Commit 68b318c6 authored by haberman's avatar haberman

we're done

parent 52948d41
## IDVI
This repo contains the python code that I wrote during the 2019's edition of the *VICE VERSA* digital art residency (cross eXchange between [La Chambre Blanche](http://www.chambreblanche.qc.ca) (CA) & [Transcultures](http://transcultures.be) (BE)). There's 2 folders `read` and `write` that corresponds to the 2 RPi: one writes poems to strobes (it contains a rudimentary generator), the other decodes the light pulses signal back into an 8bit char and transmits it to the thermal printer.
### TODO's
- [ ] cache the json peotry shape data
\ No newline at end of file
This repo contains the python code that I wrote during the 2019's edition of the *VICE VERSA* digital art residency (cross eXchange between [La Chambre Blanche](http://www.chambreblanche.qc.ca) (CA) & [Transcultures](http://transcultures.be) (BE)). There's 2 folders `read` and `write` that corresponds to the 2 RPi: one writes poems to strobes (it contains a rudimentary generator), the other decodes the light pulses signal back into an 8bit char and transmits it to the thermal printer.
\ No newline at end of file
No preview for this file type
......@@ -23,9 +23,9 @@ QUERY_CREATE_HISTORY = '''CREATE TABLE IF NOT EXISTS history
QUERY_CREATE_STATUSES = '''CREATE TABLE IF NOT EXISTS statuses
(status_id INTEGER PRIMARY KEY,
status_hashtags TEXT,
status_author TEXT,
status_content TEXT,
status_hashtags TEXT,
status_info TEXT)'''
RE_URL = re.compile(
......
......@@ -7,13 +7,13 @@ ALTER TABLE statuses RENAME TO _statuses_old;
CREATE TABLE IF NOT EXISTS statuses
(status_id INTEGER PRIMARY KEY,
status_hashtags TEXT,
status_author TEXT,
status_content TEXT,
status_hashtags TEXT,
status_info TEXT);
INSERT INTO statuses (status_id,status_hashtags,status_author,status_content,status_info)
SELECT status_id,status_hashtags,status_author,status_content,status_info
INSERT INTO statuses (status_id,status_author,status_content,status_hashtags,status_info)
SELECT status_id,status_author,status_content,status_hashtags,status_info
FROM _statuses_old;
DROP TABLE _statuses_old;
......
......@@ -104,6 +104,11 @@ class Db:
return self.execute(query[:-1], vals)
def update_one(self, where, where_id, value, table='history'):
self.connect()
self.cursor.execute('UPDATE {} SET {}=? WHERE {}=?'.format(table, value[0], where), (value[1], where_id))
self.close()
def execute(self, query, value):
self.connect()
......@@ -124,9 +129,9 @@ class Db:
return True
def delete_one(self, row_id, id, table='history'):
def delete_one(self, where, where_id, table='history'):
self.connect()
self.cursor.execute('DELETE FROM {} WHERE {}=?'.format(table, row_id), (id,))
self.cursor.execute('DELETE FROM {} WHERE {}=?'.format(table, where), (where_id,))
self.close()
def delete_all(self, table='history'):
......
......@@ -21,7 +21,7 @@ from concurrent.futures import ProcessPoolExecutor
# from chip import mcp23s17
from data import db_init, twitter_search
from poemade.generator import poems_rand_load, stanza_sort, combine_gutenberg, combine_twitter
from poemade.parser import poetry_checks, VERSE_MAX
from poemade.parser import VERSE_MAX, poetry_checks, to_cp1252
from poemade import cmu_load
PROG_NAME = 'IDVI-write'
......@@ -30,24 +30,6 @@ PATH_SRC_FILE = '{}/src/nlpoetry_corpus.json'.format(PATH_DATA_FOLDER)
# PATH_RES_DIR = '{}/res'.format(PATH_DATA_FOLDER)
PATH_RES_DIR = os.path.join(os.path.dirname(__file__), '../../yaga/data/json')
PATH_CMU_DICT = '{}/cmudict-0.7b.json'.format(PATH_DATA_FOLDER)
# PATH_DB_FILE = '{}/idvi.db'.format(PATH_DATA_FOLDER)
# c_poems = generator.closest_poems(r_poem, poems, args.verbose)
# if len(c_poems) > 0:
# if args.verbose:
# logging.info('Listing candidates for source:\n{}'.format(poem_txt))
# print(r_poem['rhyme']['scheme'])
# generator.shuffle_internal(r_poem, c_poems, False, args.verbose)
# else:
# if args.verbose:
# a_rhyme_scheme = r_poem['rhyme']['scheme']
# logging.warning('No matching poem found for source scheme {}'.format(a_rhyme_scheme))
# grow_internal(r_poem, poems, False, args.verbose)
def cbk_prepare():
......@@ -59,13 +41,17 @@ def cbk_prepare():
def cbk_encode(src_stzidx, res_data):
c_verses, s_verses = [], []
if len(res_data) == 2:
c_verses, s_verses = res_data[1][3].split('\n'), res_data[0]
c_verses, s_verses = res_data[1][2].split('\n'), res_data[0]
db.add_one('history', [None, 'status', int(time.time()), src_stzidx, res_data[1][0], None])
elif len(res_data) == 4:
c_verses, s_verses = res_data[2], res_data[3]
db.add_one('history', [None, 'stanza', int(time.time()), src_stzidx, res_data[0], res_data[1]])
print('\n______________{}\n{}'.format(c_verses, s_verses))
print('\ncbk_encode______________{}\n{}'.format(c_verses, s_verses))
c_verses, s_verses = to_cp1252(c_verses), to_cp1252(s_verses)
if not c_verses or not s_verses:
print('can\'t encode {}->{}'.format(s_verses, c_verses))
assert fsm
fsm.release()
......@@ -114,6 +100,7 @@ def prepare():
src = sources[rand_idx]
if len(src['verses']) <= 2: # TODO -> @incomplete
sess_data = combine_twitter(src, db.fetch_all('statuses'))
print(sess_data)
else:
poems_rand = poems_rand_load(db, PATH_RES_DIR)
print('-> {} random poems'.format(len(poems_rand)))
......
......@@ -40,6 +40,8 @@ def combine_twitter(source, candidates, idx=0):
c_idx = c_forms[0][0]
candidate = candidates[c_idx]
print('twitter candidate: {}'.format(candidate))
return s_verses, candidate
......
......@@ -33,18 +33,35 @@ poetry_checks = {
'not_number': lambda line: not re.search(r'^\d', line) and not re.search(r'\d.{0,1}$', line),
}
unicode_to_cp1252 = [(u'\u200b', '')]
def lexical_diversity(text):
return len(set(text)) / len(text)
def to_cp1252(lines):
for l in lines:
try:
(w.encode('cp1252') for w in l.split(' '))
return lines
except UnicodeEncodeError:
rep = dict((r[0], r[1]) for r in unicode_to_cp1252)
pattern = re.compile(r'|'.join(rep.keys()), re.UNICODE)
if pattern.match(l):
text = pattern.sub(lambda m: rep[m.group(0)], l)
return text.splitlines()
else:
return None
# TODO @fix -> hang forever
def is_poem(verses, line_size=VERSE_MAX):
poem_score = 1
para_score = 1
inc_score = 1/line_size
tokens = tokenize('\n'.join(verses), True)
# tokens = tokenize('\n'.join(verses), True)
# diversity = lexical_diversity(tokens)
# print(tokens)
# print(diversity)
......
......@@ -89,8 +89,8 @@ def test_poetry_checks(test_str):
def test_poetry_estimation():
print(test_poetry_checks(' * * * * *'))
print(is_poem([' * * * * *', '', " JOHNNY CROW'S GARDEN", '',
' * * * * *', '', " JOHNNY CROW'S PARTY", '',
print(is_poem([' * * * * *', '', ' JOHNNY CROW\'S GARDEN', '',
' * * * * *', '', ' JOHNNY CROW\'S PARTY', '',
' * * * * *']))
......@@ -105,80 +105,38 @@ def test_twitter():
print(twitter_search('%23poem', 'en', 66))
def test_contiguous():
from operator import itemgetter
from itertools import groupby
# ranges = []
# for idx in groupby(enumerate(ints), lambda x: x[0]-x[1]):
# group = map(itemgetter(1), idx[1])
# group = list(map(int, group))
# start, stop = group[0], group[-1]
# if len(ranges) > 0:
# prev_start, prev_stop = ranges[-1].start, ranges[-1].stop
# if start - prev_stop <= snap_under:
# ranges[-1] = range(prev_start, stop)
# continue
# ranges.append(range(start, stop))
# return ranges
def process(x):
print(x)
return x[1][0] - x[0]
def contiguous_tuples(tuples, idx=0, snap_under=1):
assert type(idx) is int and idx >= 0
'''
Transforms a list of continuous integers into a list of contiguous ranges.
'''
tuples = sorted(tuples, key=lambda x: x[idx])
ranges = []
for k, g in groupby(enumerate(tuples), process):
print(k)
group = (map(itemgetter(0), g))
group = list(map(int, group))
print(group)
# for t_idx in groupby(enumerate(tuples), lambda x: x[0] - x[1][idx]):
# print(t_idx)
# group = map(itemgetter(1), t_idx[1])
# # print(group)
# group = map(itemgetter(0), t_idx[1])
# group = list(map(int, group))
# print(group)
# start, stop = group[0], group[-1]
# if len(ranges) > 0:
# prev_start, prev_stop = ranges[-1].start, ranges[-1].stop
# if start - prev_stop <= snap_under:
# ranges[-1] = range(prev_start, stop)
# continue
# ranges.append(range(start, stop))
return ranges
# res = []
# prev_idx = tuples[0][idx]
# for i in range(len(tuples)):
# tup = tuples[i]
# if tup[idx] <= prev_idx + snap_under:
# res.append(tup)
# prev_idx = tup[idx]
# return res
test_data = [(21, 17, 17), (21, 20, 17), (21, 23, 17), (22, 17, 13), (22, 20, 13), (22, 23, 13), (26, 17, 1), (26, 20, 1), (26, 23, 1), (27, 17, 15), (27, 20, 15), (27, 23, 15), (30, 17, 21), (30, 20, 21), (30, 23, 21), (33, 17, 9), (33, 20, 9), (33, 23, 9), (33, 17, 10), (33, 20, 10), (33, 23, 10), (33, 17, 19), (33, 20, 19), (33, 23, 19), (34, 17, 3), (34, 20, 3), (34, 23, 3), (36, 17, 0), (36, 20, 0), (36, 23, 0), (36, 17, 7), (36, 20, 7), (36, 23, 7), (37, 17, 5), (37, 20, 5), (37, 23, 5), (37, 17, 11), (37, 20, 11), (37, 23, 11), (37, 17, 23), (37, 20, 23), (37, 23, 23), (46, 25, 14), (49, 21, 2), (49, 22, 18), (49, 25, 18), (50, 25, 7), (50, 25, 12), (50, 16, 14), (50, 25, 22), (51, 21, 12), (51, 22, 14), (51, 21, 20), (52, 16, 2), (52, 22, 2), (52, 25, 2), (52, 21, 6), (52, 22, 6), (52, 25, 6), (52, 25, 8), (52, 24, 14), (52, 25, 20), (52, 21, 22), (53, 22, 11), (53, 21, 16), (53, 22, 22), (54, 22, 0), (54, 21, 4), (54, 22, 4), (54, 25, 4), (54, 21, 14), (54, 22, 23), (55, 24, 2), (55, 25, 5), (55, 22, 7), (55, 22, 8), (55, 25, 10), (55, 22, 12), (55, 21, 18), (55, 24, 18), (55, 22, 20), (55, 22, 21), (56, 22, 5), (56, 22, 9), (56, 16, 12), (56, 19, 14), (56, 22, 16), (56, 25, 16), (56, 17, 18), (56, 20, 18), (56, 23, 18), (56, 25, 23), (57, 25, 0), (57, 19, 2), (57, 22, 3), (57, 24, 4), (57, 16, 6), (57, 25, 9), (57, 22, 10), (57, 25, 11), (57, 18, 14), (57, 22, 19), (57, 25, 21), (57, 16, 22), (58, 25, 3), (58, 19, 4), (58, 16, 7), (58, 21, 10), (58, 21, 11), (58, 19, 12), (58, 22, 13), (58, 25, 15), (58, 24, 16), (58, 25, 19), (59, 21, 0), (59, 22, 1), (59, 21, 5), (59, 15, 6), (59, 21, 7), (59, 21, 8), (59, 21, 9), (59, 22, 15), (59, 17, 16), (59, 20, 16), (59, 23, 16), (59, 16, 18), (60, 25, 1), (60, 21, 3), (60, 16, 4), (60, 18, 6), (60, 17, 8), (60, 18, 8),
(60, 20, 8), (60, 23, 8), (60, 24, 8), (60, 15, 12), (60, 17, 12), (60, 20, 12), (60, 23, 12), (60, 25, 13), (60, 18, 16), (60, 18, 18), (60, 24, 20), (60, 19, 22), (60, 21, 23), (61, 15, 4), (61, 16, 5), (61, 19, 6), (61, 24, 6), (61, 24, 12), (61, 15, 14), (61, 22, 17), (61, 16, 20), (61, 18, 20), (61, 19, 20), (62, 15, 2), (62, 18, 2), (62, 16, 3), (62, 18, 4), (62, 16, 9), (62, 25, 17), (62, 19, 18), (62, 16, 19), (62, 21, 19), (62, 21, 21), (63, 16, 0), (63, 21, 1), (63, 19, 5), (63, 24, 7), (63, 16, 8), (63, 16, 10), (63, 16, 11), (63, 18, 12), (63, 17, 14), (63, 20, 14), (63, 23, 14), (63, 15, 20), (63, 17, 20), (63, 20, 20), (63, 23, 20), (63, 16, 21), (63, 15, 22), (63, 18, 22), (63, 24, 22), (64, 24, 5), (64, 18, 10), (64, 24, 10), (64, 24, 11), (64, 15, 18), (64, 24, 21), (64, 16, 23), (64, 18, 23), (65, 17, 4), (65, 20, 4), (65, 23, 4), (65, 17, 6), (65, 20, 6), (65, 23, 6), (65, 15, 7), (65, 19, 7), (65, 24, 9), (65, 18, 11), (65, 19, 11), (65, 16, 16), (65, 24, 23), (66, 24, 0), (66, 16, 1), (66, 19, 9), (66, 19, 10), (66, 21, 13), (66, 16, 15), (66, 21, 15), (66, 18, 19), (66, 17, 22), (66, 20, 22), (66, 23, 22), (67, 19, 0), (67, 19, 3), (67, 24, 3), (67, 18, 5), (67, 18, 7), (67, 19, 8), (67, 18, 9), (67, 24, 19), (67, 18, 21), (67, 19, 23), (68, 18, 0), (68, 17, 2), (68, 20, 2), (68, 23, 2), (68, 18, 3), (68, 15, 5), (68, 15, 8), (68, 15, 11), (68, 24, 15), (68, 15, 16), (68, 19, 16), (68, 19, 19), (68, 19, 21), (69, 15, 0), (69, 19, 1), (69, 15, 10), (69, 16, 13), (69, 19, 13), (69, 16, 17), (69, 21, 17), (70, 24, 1), (70, 15, 9), (70, 24, 13), (70, 19, 15), (70, 15, 21), (70, 15, 23), (71, 18, 1), (71, 15, 3), (71, 18, 13), (71, 18, 15), (72, 19, 17), (72, 15, 19), (73, 15, 15), (73, 24, 17), (74, 15, 1), (74, 18, 17), (75, 15, 13), (78, 15, 17)]
r = contiguous_tuples(test_data)
print(r)
def test_db_st_cleanup():
import re
from data import db_init
REP = [(u'\u200b', '')]
db = db_init('../data')
# statuses = db.fetch_all('statuses')
for s in db.fetch_all('statuses'):
if s[2].startswith('#'):
print(s)
db.delete_one('status_id', s[0], 'statuses')
continue
for l in s[2].splitlines():
# l.replace(line)
try:
(w.encode('cp1252') for w in l.split(' '))
except UnicodeEncodeError:
print('anything ???')
rep = dict((r[0], r[1]) for r in REP)
pattern = re.compile("|".join(rep.keys()), re.UNICODE)
text = pattern.sub(lambda m: rep[m.group(0)], l)
# where, where_id, value, table='history'
db.update_one('status_id', s[0], ('status_content', text), 'statuses')
# db.delete_one('status_id', s[0], 'statuses')
break
if __name__ == '__main__':
test_contiguous()
test_db_st_cleanup()
# test_twitter()
# test_mpc()
# test_cmu_annotate()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment