User:Luisa Moura/prototyping/python/cgi html

From XPUB & Lens-Based wiki


The code bellow allows a comparative research between two texts through the choice of one or two keywords. The code will grab in each text a few lines containing the words (five before and five after) and will set those paragraphs, from both sources, next to each other. The objective is to create a shortcut into the authors approach around certain words or expressions.

#!/usr/bin/env python
f = open("machiavelli.txt")
g = open("more.txt")
lines = f.readlines()
lines_2 = g.readlines()
numlines = len(lines)
numlines_2 = len(lines_2)
import cgi, cgitb
hits = set()
hits_2 = set()
# check accessing CGI Form Data
form = cgi.FieldStorage()
if form.has_key("q1"):
    v1 = form.getvalue("q1") 
    v1 = ""
if form.has_key("q2"):
    v2 = form.getvalue("q2") 
    v2 = ""
print "Content-type: text/html;charset=utf-8"
print """
<!DOCTYPE html>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="../styles.css">
<body onload="document.getElementById('q').focus()">
<form action="/cgi-bin/" class="top">
    <input type="submit" name="s" value="Search" />
    <input type="text" id="q1" name="q1" /><br />
    <input type="text" id="q2" name="q2" /><br />
for n in range(0, numlines-5):
    block = ""
    for line in lines[n:n+5]:
        block += line
    if v1 in block and v2 in block:
        for n in range(n, n+5):
lastline = 0
for x in range(0, numlines_2-5):
    block_2 = ""
    for line_2 in lines_2[x:x+5]:
        block_2 += line_2
    if v1 in block_2 and v2 in block_2:
        for x in range(x, x+5):
lastline_2 = 0
print """
<div class="container">
<div class="title_A">
the prince - machiavelli
<div class="coluna_A">
for n in range(0, numlines):
    if n in hits:
        line = lines[n].strip()
        if n > lastline + 1:
            print "========================="
            print "<br>"
        print line.replace(v1, v1.upper() ).replace(v2, v2.upper() )
        lastline = n
        print "<br>"
print """
<div class="container">
<div class="title_B">
utopia - thomas more
<div class="coluna_B">
for x in range(0, numlines_2):
    if x in hits_2:
        line_2 = lines_2[x].strip()
        if x > lastline_2 + 1:
            print "========================="
            print "<br>"
        print line_2.replace(v1, v1.upper() ).replace(v2, v2.upper() )
        lastline_2 = x
        print "<br>"
print """
<div class="limit_float">
# special thanks to Lucia
Search engine 01.jpg