Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1.  
  2. def find_word(dict_file, the_word):
  3.    
  4.     ''' Searches through a text file for occurences of a given word
  5.    
  6.        The function goes through the file line by line, putting all words of the line into a list.
  7.        It then checks if said word is in the list and adds (line_number, line_string) to the output.
  8.        
  9.        Input: (file_path, word_you're_searching_for)
  10.    
  11.    '''
  12.      
  13.     found_it = False                
  14.  
  15.     # does one iteration too much but whatever .. :3
  16.     while found_it == False and dict_file.tell() < 28956000:  # 2nd condition = end of file
  17.        
  18.         current_line = dict_file.readline()
  19.         altered_current_line = current_line
  20.        
  21.         for i in ["'", "`", "(", ")", "[", "]", "{", "}",
  22.                                    ":", ",", ".", "!", "?", '"', ";"]:
  23.            
  24.             if i in altered_current_line:
  25.                
  26.                 altered_current_line = altered_current_line.replace(i, " ")
  27.        
  28.         current_word_list = altered_current_line.split()
  29.        
  30.         if the_word in current_word_list:
  31.            
  32.             found_it = True
  33.        
  34.     dict_file.seek(dict_file.tell() - len(current_line) - 1)
  35.     return None
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. def webster_definition(unclear_word):
  44.    
  45.     dict_file = open("webster.txt", "r")
  46.     dict_file.seek(550)
  47.    
  48.     find_word(dict_file, unclear_word.upper())
  49.     find_word(dict_file, "Defn")
  50.     dict_file.seek(dict_file.tell() + 6)
  51.    
  52.     # Fraidy: Paragraph stuff start ---------------------
  53.    
  54.     start = dict_file.tell()
  55.    
  56.     length = 5
  57.     stopper = 0
  58.    
  59.     while length > 1 and stopper < 20:
  60.        
  61.         length = len(dict_file.readline())
  62.         stopper += 1
  63.    
  64.     end = dict_file.tell()
  65.     dict_file.seek(start)
  66.     result = dict_file.read(end - start - stopper)
  67.     dict_file.close()
  68.    
  69.     return result
  70.  
  71. print webster_definition("axstone")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement