Jump to content

Artisan's Script


The Voiceless One

Recommended Posts

I just go done reading Warbreaker, and decided to make something to translate text into colors.  I can't figure out where to put it, so I will just leave it here.

(Spoilered for length)

Spoiler

#!/usr/bin/python
#import cgi
#import cgitb; cgitb.enable()  # for troubleshooting

import sys, os

import urllib

#preprocess post data
POST={}
args=sys.stdin.read().split('&')
for arg in args: 
    t=arg.split('=')
    if len(t)>1: k, v=arg.split('='); POST[k]=v

#get dictionary to translate to sylables
from nltk.corpus import cmudict

#import ability to use a regex
import re

#define reference to dictionary
d = cmudict.dict()

#define non-alphabetic characters supported
nonalpha=["_", "?", "!", ".", ",", "$", ":", ";", "\"", "'", " ", "(", ")", "/", "\\", "-", "*", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

#create syllable list from dictionary
syls=[]
for k in d.values():
    for rl in k:
        for s in rl:
            if s not in syls:
                syls.append(s)

#define function to translate to sylables
def process(word):
    if word in nonalpha:
        return word
    else:
        return [list(y for y in x if y[-1].isdigit()) for x in d[word.lower()]]

#define function to sanatize input
def cleaner(inp):
    inp=urllib.unquote(inp)
    inp=re.sub(r"[+]", " ", inp)
    good=re.sub(r"[^\w\?\!\.\,\$\:\;\"\'\(\)\/\\\-\* ]", "", inp)
    bad=re.sub(r"[\w\?\!\.\,\$\:\;\"\'\(\)\/\\\-\* ]", "", inp)
    rl=[good, bad]
    return rl

#define function to lookup color assigned to a character
def get_color(char):
    #increment is 0xFFFFFF/(length_of_syllable_list + 27)
    inc=int(0xffffff)/(len(syls) + len(nonalpha))
    if char in nonalpha:    
        for i in range(0, len(nonalpha)):
            if str(char)==str(nonalpha):
                color=inc*i
    elif char in syls:
        for i in range(0, len(syls)):
            if str(char)==str(syls):
                color=inc*i
                color=color + (27*inc)
    else:
        color=16777215-inc#unknowns are white minus one increment
    #convert decimal color to hex string
    retcolor=format(color, "06x")
    return retcolor

#define function to translate words to colors
def translate(word):
    c=process(word)
    ret_list=[]
    for rl in c:
        for l in rl:
            ret_list.append(get_color(l))
    return ret_list

#define function to split the input into words and translate them
def separate(clean_inp):
    inp=str(clean_inp[0])
    letter_storage=[]
    construction_string=""
    end_result=[]
    for c in inp:
        if c.isalpha():
            letter_storage.append(c)
        else:
            if len(letter_storage)>0:
                for l in range(0, len(letter_storage)):
                    construction_string=construction_string + letter_storage[l]
                for i in translate(construction_string):
                    end_result.append(i)
            end_result.append(get_color(c))
            construction_string=""
            letter_storage=[]
    return end_result

#start generating the HTML page
print "Content-type: text/html\n\n"
print """
<html>
<head>
<link rel="icon" type="image/png" href="../uploads/globe.png">
<title>Artisan's Script</title>
</head>
<body bgcolor="#ffffff">
<p>go <a href="../home.htm">home</a></p>
</br>"""

print """
<form action='hallandren.cgi' method='post'>
Input the text to translate:  &nbsp;
<input type="text" name="sentence"/>
<input type='submit' />
</form>
</br>
<p>"""

#print cleaner(POST.get("sentence", " "))
for color in separate(cleaner(POST.get("sentence", " "))):
    print "<span style=\"background-color: #" + str(color) + "\">_</span>"

print """
</p>
</br>
<p><a href="/feedback.htm">give feedback</a></p>
<p><a href="../problem.htm">report a problem</a></p>

</body>
</html>"""

For those of you who want to run it, you will need python 2.7 and nltk installed.  It works bes on a web server, but will run fine on a computer.

Link to comment
Share on other sites

  • 3 weeks later...

You will need python 2.7 and nltk installed.  For Windows and Mac OS X, the downloads are at https://www.python.org/downloads/release/python-2714/, and for Linux it is usually already installed.  Detailed instructions for installing nltk can be found at http://www.nltk.org/install.html.  For Linux, 

sudo pip install -U nltk

For Windows, download nltk at  https://pypi.python.org/packages/4c/0a/b462e417b47cdfaed41552f987854b6fbc968205429f52ec96d6fb10ed76/nltk-3.2.5.win32.exe#md5=2861e7fc2f11d2b6b9533f9a90f141f8, and then run the file.

 

Once you have python and nltk installed, copy and paste the code into a blank text file (use something like notepad, Microsoft Word will not work) save the file (make sure that it has the .py file extension), and then close it.  Find the file and right click on it, then go to properties and make the file executable.  Next, open a command prompt and type 

start path_to_file\name_of_file

on Windows or 

path_to_file/name_of_file

on Linux or Mac, and then press enter.  The command prompt should go to a new line as if the computer is taking a long time to complete the command.  On this line, type 

sentence="your sentence here"&ref="home.htm"

and replace "your sentence here" with what you want translated, then press enter and control+d.  Depending on your computer, the command may take a long time (mine is from 2005), and should then produce some HTML code.  Paste this into a new empty file (once again, do not use Microsoft Word) and then save and exit.  Right click on the file and select "open with" and then pick your web browser.  If this does not work, or I did not explain very well, please let me know.

Link to comment
Share on other sites

On 11/27/2017 at 1:44 AM, Ookla the Codebringer said:

@Ookla the Tremendous you use Windows? 

Well, sorta. We use Windows on our main computer, but my personal computer currently runs Ubuntu, and...

On 11/27/2017 at 3:27 PM, The Voiceless One said:

You will need python 2.7 and nltk installed.  For Windows and Mac OS X, the downloads are at https://www.python.org/downloads/release/python-2714/, and for Linux it is usually already installed.  Detailed instructions for installing nltk can be found at http://www.nltk.org/install.html.  For Linux, 


sudo pip install -U nltk

For Windows, download nltk at  https://pypi.python.org/packages/4c/0a/b462e417b47cdfaed41552f987854b6fbc968205429f52ec96d6fb10ed76/nltk-3.2.5.win32.exe#md5=2861e7fc2f11d2b6b9533f9a90f141f8, and then run the file.

 

Once you have python and nltk installed, copy and paste the code into a blank text file (use something like notepad, Microsoft Word will not work) save the file (make sure that it has the .py file extension), and then close it.  Find the file and right click on it, then go to properties and make the file executable.  Next, open a command prompt and type 


start path_to_file\name_of_file

on Windows or 


path_to_file/name_of_file

on Linux or Mac, and then press enter.  The command prompt should go to a new line as if the computer is taking a long time to complete the command.  On this line, type 


sentence="your sentence here"&ref="home.htm"

and replace "your sentence here" with what you want translated, then press enter and control+d.  Depending on your computer, the command may take a long time (mine is from 2005), and should then produce some HTML code.  Paste this into a new empty file (once again, do not use Microsoft Word) and then save and exit.  Right click on the file and select "open with" and then pick your web browser.  If this does not work, or I did not explain very well, please let me know.

...oh. Wow. :mellow: 

Link to comment
Share on other sites

I just looked over it again myself.  It is amazing how much simpler it seemed when I was typing it.  Do you want the instructions for Windows or Linux?  I think they will make more sense if I am not trying to explain two different methods at the same time.  Would it help if I posted screenshots of what I am doing?

Link to comment
Share on other sites

  • Chaos locked this topic
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...