def lead_text(top_elem, num_words=10):
 pat = re.compile(u'\\s+', flags=re.UNICODE)
 words = []
 def get_text(x, attr=u'text'):
  ans = getattr(x, attr)
  if ans:
   words.extend(filter(None, pat.split(ans)))
 stack = [(top_elem, u'text')]
 while (stack and (len(words) < num_words)):
  (elem, attr) = stack.pop()
  get_text(elem, attr)
  if (attr == u'text'):
   if (elem is not top_elem):
    stack.append((elem, u'tail'))
   stack.extend(reversed(list(((c, u'text') for c in elem.iterchildren(u'*')))))
 return u' '.join(words[:num_words])

def lead_text ( top_elem , num_words = 10 ) : pat = re . compile ( u'\\s+' , flags = re . UNICODE ) words = [ ] def get_text ( x , attr = u'text' ) : ans = getattr ( x , attr ) if ans : words . extend ( filter ( None , pat . split ( ans ) ) ) stack = [ ( top_elem , u'text' ) ] while ( stack and ( len ( words ) < num_words ) ) : ( elem , attr ) = stack . pop ( ) get_text ( elem , attr ) if ( attr == u'text' ) : if ( elem is not top_elem ) : stack . append ( ( elem , u'tail' ) ) stack . extend ( reversed ( list ( ( ( c , u'text' ) for c in elem . iterchildren ( u'*' ) ) ) ) ) return u' ' . join ( words [ : num_words ] )
return the leading text contained in top_elem up to a maximum of num_words words .