Example #1
0
 def __init__(self, results_queue):
   self.results_queue = results_queue
   self.terminate_request_flag = False
   self.haiku_finder = HaikuFinder()
   self.untex_thread_class = UntexWrapper()
   self.last_parsed_id = get_last_parsed_id()
   threading.Thread.__init__(self)
Example #2
0
class HaikuFindingThread(threading.Thread):
  def __init__(self, results_queue):
    self.results_queue = results_queue
    self.terminate_request_flag = False
    self.haiku_finder = HaikuFinder()
    self.untex_thread_class = UntexWrapper()
    self.last_parsed_id = get_last_parsed_id()
    threading.Thread.__init__(self)
    
  def terminate_request(self):
    self.terminate_request_flag = True
    
  def terminate(self):
    if not no_dictionary_update:
      logging.info("Saving dictionary")
      self.haiku_finder.save_dict()
    logging.info("Saving last_parsed_id")
    set_last_parsed_id(self.last_parsed_id)

  def run(self):
    for (article_id, raw_tex) in rssparse(arxiv_feed_url, self.last_parsed_id):
      if self.terminate_request_flag:
        critical("Worker thread caught terminate_request_flag, terminating")
        self.terminate()
        return
      
      self.last_parsed_id = article_id

      logging.info("Attempting raw tex from article_id: "+ str(article_id))
      
      if not raw_tex:
        error("Got empty raw_tex from article_id: " + str(article_id))
        continue
      
      haiku_list = []
      try:
        raw_text = self.untex_thread_class.run_untex(raw_tex)
        haiku_list = self.haiku_finder.find_haiku_in_text(raw_text)
      except RuntimeError as e:
        print_with_warning(str(e))
      if len(haiku_list)==0:
        logging.info("Found no Haiku in article_id: " + str(article_id))
      else:
        for haiku in haiku_list:
          self.results_queue.put(haiku + " (" + str(article_id) + ") #arXivHaiku")
          logging.info("Found haiku in article_id :" + str(article_id) + " : " + haiku)
    print_with_ok("Got to the end of the list :(")
    logging.info("Worker thread finished")
    self.terminate()
Example #3
0
      detexify = True  
    else:
      usage()
      sys.exit(2)
  if not input_file:
    usage()
    sys.exit(2)
      
  try:
    raw_text = open(input_file, "r").read()
  except IOError as e:
    print "Can't read input file:" + str(e) + "\n"
    sys.exit(2)
  
  if detexify:
    untex_thread_class = UntexWrapper()
    raw_text = untex_thread_class.run_untex(raw_text)

  haiku_finder = HaikuFinder()
  haiku_list = haiku_finder.find_haiku_in_text(raw_text) 
  if no_dictionary_update:
    haiku_finder.save_dict()

  logging.info("Found the following haiku: " + str(haiku_list))
  if len(haiku_list)==0:
    print "Found no Haiku, sorry :("
  else:
    print "Found the following Haiku:"
    for haiku in haiku_list:
      print haiku