Dynamical approach to Zipf's law