Skip to content

Commit

Permalink
tweaks for python 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbird committed Feb 11, 2013
1 parent 160070f commit 7b87346
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 70 deletions.
4 changes: 2 additions & 2 deletions nltk/app/chartparser_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2242,9 +2242,9 @@ def app():

print('grammar= (')
for rule in grammar.productions():
print(' ', repr(rule)+',')
print((' ', repr(rule)+','))
print(')')
print('tokens = %r' % tokens)
print(('tokens = %r' % tokens))
print('Calling "ChartParserApp(grammar, tokens)"...')
ChartParserApp(grammar, tokens).mainloop()

Expand Down
84 changes: 16 additions & 68 deletions nltk/app/wordnet_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

import os
from sys import argv
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from collections import defaultdict
import webbrowser
import datetime
Expand All @@ -60,13 +59,17 @@
import time
import getopt
import base64
import cPickle
import pickle
import copy

from nltk import compat
from nltk.corpus import wordnet as wn
from nltk.corpus.reader.wordnet import Synset, Lemma

if compat.PY3:
from http.server import HTTPServer, BaseHTTPRequestHandler
else:
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

# now included in local file
# from util import html_header, html_trailer, \
Expand Down Expand Up @@ -99,10 +102,6 @@ def do_GET(self):
print('Server shutting down!')
os._exit(0)

elif sp == 'favicon.ico':
type = 'image/x-icon'
page = favicon_data()

elif sp == '': # First request.
type = 'text/html'
if not server_mode and firstClient:
Expand Down Expand Up @@ -153,7 +152,7 @@ def do_GET(self):

# Send result.
self.send_head(type)
self.wfile.write(page)
self.wfile.write(page.encode('utf8'))


def send_head(self, type=None):
Expand All @@ -172,58 +171,6 @@ def log_message(self, format, *args):
format%args))


# This data was encoded with the following procedure
def encode_icon():
f = open("favicon.ico", "rb")
s = f.read()
f.close()

def split(s):
if len(s) <= 72:
return [s]
else:
return [s[0:72]] + split(s[72:])

print(split(base64.urlsafe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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'AAAAAAAAAAAAAAAGFh4YLAYAAAAAAAAAAAAHGB4gIBYWBgAAAAAAAAAAFhgzQR45MixGMQAA',
'AAAAABQYTF0WbzplWQAAAABFVEgKFExdFG59eywWCwBAdHRJCgpLXRhxe3IvIiIDT2p0VAdh',
'fn5xbzplciAwFFNqanQ3BwoKChYYGB4gICxYanRqalRPWVRZRhMYHiAYTmlqdnZ2dnh5eX1G',
'FhgeFEVjaT1SVithKzg7WhMYGAsATmM9UjgwXDt2eFsIFgcAAAAAFDRDLUo-bnhZAAAAAAAA',
'AAgwRS1cO3Z2WgAAAAAAAAADUTZHbVJ0d0kAAAAAAAAAADFPY2pqZEgAAAAAAAAA__8AAP__',
'AAD__wAAsaEAAE5eAABOXgAA__4AAPv_AAD__wAA__8AAM3-AADw_wAA__8AAML-AAD__wAA',
'xf4=']


def favicon_data():
"""
Return the data for the favicon image.
"""
return base64.urlsafe_b64decode(''.join(FAVICON_BASE64_DATA))


def get_unique_counter_from_url(sp):
"""
Extract the unique counter from the URL if it has one. Otherwise return
Expand Down Expand Up @@ -587,7 +534,7 @@ def _collect_one_synset(word, synset, synset_relations):
ref = copy.deepcopy(Reference(word, synset_relations))
ref.toggle_synset(synset)
synset_label = typ + ";"
if synset.name in synset_relations.keys():
if synset.name in synset_relations:
synset_label = _bold(synset_label)
s = '<li>%s (%s) ' % (make_lookup_link(ref, synset_label), descr)
def format_lemma(w):
Expand Down Expand Up @@ -629,7 +576,7 @@ def _synset_relations(word, synset, synset_relations):
:rtype: str
'''

if not synset.name in synset_relations.keys():
if not synset.name in synset_relations:
return ""
ref = Reference(word, synset_relations)

Expand Down Expand Up @@ -694,7 +641,7 @@ def encode(self):
# This uses a tuple rather than an object since the python
# pickle representation is much smaller and there is no need
# to represent the complete object.
string = cPickle.dumps((self.word, self.synset_relations), -1)
string = pickle.dumps((self.word, self.synset_relations), -1)
return base64.urlsafe_b64encode(string)

def toggle_synset_relation(self, synset, relation):
Expand All @@ -716,7 +663,7 @@ def toggle_synset(self, synset):
"""
Toggle displaying of the relation types for the given synset
"""
if synset.name in self.synset_relations.keys():
if synset.name in self.synset_relations:
del self.synset_relations[synset.name]
else:
self.synset_relations[synset.name] = set()
Expand All @@ -729,7 +676,7 @@ def decode_reference(string):
Decode a reference encoded with Reference.encode
"""
string = base64.urlsafe_b64decode(string)
word, synset_relations = cPickle.loads(string)
word, synset_relations = pickle.loads(string)
return Reference(word, synset_relations)

def make_lookup_link(ref, label):
Expand Down Expand Up @@ -785,10 +732,11 @@ def page_from_reference(href):

# This looks up multiple words at once. This is probably not
# necessary and may lead to problems.
for pos in [wn.NOUN, wn.VERB, wn.ADJ, wn.ADV]:
form = wn.morphy(w, pos)
if form and form not in pos_forms[pos]:
pos_forms[pos].append(form)
for w in words:
for pos in [wn.NOUN, wn.VERB, wn.ADJ, wn.ADV]:
form = wn.morphy(w, pos)
if form and form not in pos_forms[pos]:
pos_forms[pos].append(form)
body = ''
for pos,pos_str,name in _pos_tuples():
if pos in pos_forms:
Expand Down

0 comments on commit 7b87346

Please sign in to comment.