Generate Taxonomy Terms from Node Content Automatically
I haven't achieved such content based node tagging programatically on my own. But used a module named Open Calais which uses the Thomson Reuters web service to generate and classify taxonomy term from node body content. Though it was available with free key use the number of API calls were limited. This with More Like This might serve some purpose in case you are running on Drupal 6.

Another module that might be of interest is Autocategorise which appears to do the same by subjecting node content to regular expression rather than a web service API like calais. If you wish to code the logic yourself examining the module's code might give some ideas on implementation.