Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
algolit
algolit
Commits
f06c5e2c
Commit
f06c5e2c
authored
Nov 08, 2017
by
manetta
Browse files
Merge branch 'master' of gitlab.constantvzw.org:algolit/algolit
parents
52176b74
2345ba10
Changes
1
Hide whitespace changes
Inline
Side-by-side
algoliterary_encounter/oulipo/oulipo.py
0 → 100644
View file @
f06c5e2c
#!/usr/bin/env/ python
# This script makes you choose 1 out of 2 Oulipo constraints:
# Constraint 1: http://oulipo.net/fr/contraintes/litterature-definitionnelle
# Constraint 2: rewrites the beginning of a novel by replacing the principal names/places/gender
# The idea for this script comes from the book 'Think Python'.
# Copyright (C) 2016 Constant, Algolit, An Mertens
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details: <http://www.gnu.org/licenses/>.
from
__future__
import
division
import
nltk
from
nltk.corpus
import
wordnet
as
wn
from
pattern.en
import
tag
import
nltk.data
from
random
import
shuffle
,
choice
,
randrange
import
colors
from
colors
import
red
,
green
,
yellow
,
blue
,
magenta
,
cyan
,
bold
,
underline
import
time
import
os
,
sys
## FUNCTIONS
# print on screen character per character
def
typewrite
(
sentence
):
words
=
sentence
.
split
(
" "
)
for
word
in
words
:
for
char
in
word
:
if
char
!=
"<"
and
char
!=
">"
:
sys
.
stdout
.
write
(
'%s'
%
char
)
sys
.
stdout
.
flush
()
time
.
sleep
(
0.1
)
sys
.
stdout
.
write
(
" "
)
sys
.
stdout
.
flush
()
# loop script
while
True
:
print
"
\n\t\t
Dear visitor, you can choose between"
,
red
(
" two Oulipo applications.
\n
"
)
#source = open("frankenstein_for_machines.txt", 'r')
print
"
\t\t
Option a is "
,
green
(
"Litterature Definitionnelle"
),
" with sentences from Mary Shelley's Frankenstein.
\n
"
#source = open("frankenstein_for_machines.txt", 'r')
print
"
\t\t
Option b is "
,
green
(
"A Novel Starring You.
\n
"
)
#source = open("frankenstein_for_machines.txt", 'r')
print
"
\t\t
Type "
,
green
(
'a'
),
" if you want to play with Litterature Definitionnelle.
\n
"
#source = open("frankenstein_for_machines.txt", 'r')
print
"
\t\t
Type "
,
green
(
'b'
),
" if you want to be a star in the opening scene of Kurt Vonneguts' 2BRO2B.
\n
"
#source = open("frankenstein_for_machines.txt", 'r')
choice
=
raw_input
(
"
\t\t
Your choice is: "
)
#source = open("frankenstein_for_machines.txt", 'r')
print
"
\n
"
os
.
system
(
'cls'
if
os
.
name
==
'nt'
else
'clear'
)
print
"
\n
"
### MEET ---------------------------------------------------------------------------------------------
### --------------------------------------------------------------------------------------------------
### MEET/INTRO ---------------------------------------------------------------------------------------------
# retrain model
if
choice
==
'a'
:
### Litterature definitionnelle
# textfiles
source
=
open
(
"frankenstein_for_machines.txt"
,
'r'
)
#source = open("1984_fragment.txt", 'r')
destination
=
open
(
"litterature_definitionelle.txt"
,
"wt"
)
## SCRIPT
# select 4 sentences from source
## split source text into list of sentences
finding_sentences
=
nltk
.
data
.
load
(
'tokenizers/punkt/english.pickle'
)
sentences_list
=
[]
with
source
as
text
:
for
line
in
text
:
# this returns a list with 1 element containing the entire text, sentences separated by \n
sentences
=
'
\n
'
.
join
(
finding_sentences
.
tokenize
(
line
.
strip
()))
# transform string into list of sentences
sentences_list
=
sentences
.
split
(
"
\n
"
)
selected_sentences
=
[
sentences_list
[
randrange
(
len
(
sentences_list
))]
for
s
in
range
(
4
)]
# tokenize source and get Part-of-Speech tags for each word
definitions
=
[]
for
sentence
in
selected_sentences
:
# create tuple of tuples with pairs of word + POS-tag
collection
=
tag
(
sentence
,
tokenize
=
True
,
encoding
=
'utf-8'
)
# transform tuple into list to be able to manipulate it
collection
=
list
(
collection
)
# for each pair:
for
element
in
collection
:
# look for nouns & replace them with their definition
if
element
[
1
]
==
"NN"
:
if
wn
.
synsets
(
element
[
0
]):
synset
=
wn
.
synsets
(
element
[
0
])
definitions
.
append
(
"<"
)
definitions
.
append
(
synset
[
0
].
definition
())
definitions
.
append
(
">"
)
else
:
break
else
:
# non-nouns are left as words
definitions
.
append
(
element
[
0
])
# write the transformed sentence
#print " ".join(definitions)
for
d
in
definitions
:
typewrite
(
d
)
raw_input
(
"
\n
Press Enter to continue..."
)
# time.sleep(10)
# -------------------------------------------
elif
choice
==
'b'
:
### A Novel Starring You
# introduction, getting the variables
print
"
\n\t\t
Dear visitor, we will rewrite the opening scene of "
,
green
(
"Kurt Vonnegut's 2BRO2B"
),
" using your name and favourite city.
\n
"
##source = open("frankenstein_for_machines.txt", 'r')
first_name
=
raw_input
(
"
\t\t
Please type your first name: "
)
##source = open("frankenstein_for_machines.txt", 'r')
last_name
=
raw_input
(
"
\n\t\t
Please type your last name: "
)
##source = open("frankenstein_for_machines.txt", 'r')
country
=
raw_input
(
"
\n\t\t
Choose a country: "
)
##source = open("frankenstein_for_machines.txt", 'r')
city
=
raw_input
(
"
\n\t\t
Choose a city in that country: "
)
##source = open("frankenstein_for_machines.txt", 'r')
print
"
\n\t\t
Do you want to be"
,
green
(
'female'
),
"or"
,
green
(
'male?'
)
gender
=
raw_input
(
"
\t\t
Please type f or m: "
)
#time.sleep(5)
print
"
\n
"
# specify input text
source
=
open
(
"vonnegut.txt"
,
"r"
)
sentences
=
[]
# write & replace
archive
(
"
\n\n
Novel Starring You
\n
"
)
archive
(
"-------------
\n\n
"
)
with
source
as
text
:
for
line
in
text
:
line
=
line
.
replace
(
"the United States"
,
country
)
line
=
line
.
replace
(
"Chicago"
,
city
)
line
=
line
.
replace
(
"Edward K."
,
first_name
)
line
=
line
.
replace
(
"Wehling"
,
last_name
)
if
gender
==
'f'
:
line
=
line
.
replace
(
" man "
,
" woman "
)
line
=
line
.
replace
(
" man,"
,
" woman,"
)
line
=
line
.
replace
(
" his "
,
" her "
)
line
=
line
.
replace
(
" him "
,
" her "
)
line
=
line
.
replace
(
" His "
,
" Her "
)
line
=
line
.
replace
(
" wife "
,
" husband "
)
line
=
line
.
replace
(
" he "
,
" she "
)
line
=
line
.
replace
(
" He "
,
" She "
)
typewrite
(
line
)
archive
(
line
)
# break before relaunching the script
raw_input
(
"
\n
Press Enter to continue..."
)
time
.
sleep
(
10
)
### ELSE --------------------------------------------------------------------------------------
### -------------------------------------------------------------------------------------------
# try again
else
:
print
"
\t\t
You must have typed something else."
#time.sleep(30)
raw_input
(
"
\n
Press Enter to continue..."
)
os
.
system
(
'cls'
if
os
.
name
==
'nt'
else
'clear'
)
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment