From a180f1a6f8a5dc7e96dbe15cf4a73488dc3b75ed Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Sun, 21 Sep 2014 02:58:53 +0100 Subject: Added paging, some fixes for FOLDOC --- bladictionary.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/bladictionary.py b/bladictionary.py index ab4acbc..4ad0fd4 100755 --- a/bladictionary.py +++ b/bladictionary.py @@ -7,7 +7,7 @@ import optparse from lxml import etree import sqlite3 -VERSION = "2.1.10b" +VERSION = "2.2.0" class Definition(object): #ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list @@ -157,8 +157,9 @@ def parse_args(): word = "" word_type = "" word_dict = "" - + page_num = 1 stop_id = 0 + #See if the dictionary or type have been specified if len(args) > 1: cur_id = 1 @@ -171,6 +172,11 @@ def parse_args(): word_dict = arg if stop_id == 0: stop_id = cur_id + elif len(arg) < 3 and arg.isdigit(): + page_num = int(arg) + if stop_id == 0: + stop_id = cur_id + cur_id +=1 #If no type or dictionary has been set, use all the args @@ -189,7 +195,7 @@ def parse_args(): print "You must specify a word to define" sys.exit(1) - return word, word_type, word_dict, options + return word, word_type, word_dict, page_num, options #Get definitions from the OED text file def parse_oed(word): @@ -304,6 +310,7 @@ def parse_foldoc(word, refer = False): found = False count_blank = 0 items = [] + #If it's been referred from another definition, include the new word if refer: definition = word + ". " @@ -311,21 +318,22 @@ def parse_foldoc(word, refer = False): definition = "" for line in file: - line = line.strip() + + #line = line.strip() word_parts = line.split(" ") word_part = "" if not found: #Read the appropriate number of words depending on how many were specified for part in word_parts[0:word_len]: word_part += part + " " - word_part = word_part.strip() - + word_part = word_part.rstrip() #Ignore case if word_part.lower() == word.lower(): found = True #Foldoc definitions are split over multiple lines, so keep reading once we've found it else: + line = line.strip() #Line with the specified word is followed by one blank line #Skip the first blank line, and then stop when any further blank lines are found if len(line) == 0: @@ -343,7 +351,7 @@ def parse_foldoc(word, refer = False): return items for part in word_parts: - definition += part.replace("{", "").replace("}", "") + " " + definition += part.strip().replace("{", "").replace("}", "") + " " if not found : return @@ -356,7 +364,7 @@ def parse_foldoc(word, refer = False): def main(): - word, word_type, word_dict, options = parse_args() + word, word_type, word_dict, page_num, options = parse_args() if options.version: print VERSION @@ -378,7 +386,9 @@ def main(): if word_dict is None or word_dict == "": word_dict = "db" items = get_sql(word) - items += parse_foldoc(word) + foldoc_items = parse_foldoc(word) + if foldoc_items is not None and len(foldoc_items) > 0: + items += foldoc_items if items is None or len(items) == 0: print "No definitions found for "+word @@ -398,7 +408,6 @@ def main(): types = ["n", "v", "adj", "adv", "tech"] type_id = 0 - all_types = word_type is "" #Normally we try and display 1 of each type if no type is specified @@ -418,7 +427,11 @@ def main(): if all_types: found_type = False cur_id = type_id - + if item.id < page_num: + if item.id is not 0: + continue + elif item.id == 0 and page_num > 1: + continue #Definitions should be ordered by type, so loop through all the types until we find a match for cur_type in types[type_id:]: if item.word_type == cur_type: @@ -432,7 +445,6 @@ def main(): if not found_type: word_type = types[type_id] - if word_type == item.word_type: #Keep track of how many we haven't printed if suppress_print: @@ -446,13 +458,16 @@ def main(): definition += ". " elif definition[-1] == "." : definition += " " - + #Print usage examples if they exist if len(item.uses) > 0: definition += "; \""+item.uses[0]['quote']+"\" " + if not all_types and line_length + len(definition) < (max_length * (page_num -1)): + line_length += len(definition) +1 + continue #Once we've reached the maximum length, stop printing any more - if line_length + len(definition) +1 > max_length: + elif line_length + len(definition) +1 > (max_length * page_num): #If we haven't printed anything so far, it's just one really long definition if line_length == 0: #So truncate it -- cgit v1.2.3