kupolak / textstat

Ruby gem to calculate statistics from text to determine readability, complexity and grade level of a particular corpus.
MIT License
31 stars 9 forks source link

Performance suggestions #30

Open abrom opened 4 years ago

abrom commented 4 years ago

I've noticed some pretty nasty performance issues related to the use of the Text::Hyphen package. When passed in really long words it can take 5+ minutes to 'visualise'. Given there is no memoization of the Text::Hyphen dictionary (and it's caches) then calling each of the various methods that use your syllable_count method can end up taking hours to calculate what is otherwise a pretty simple piece of text (albeit with a few errant 'long' words).

Suggested solution would be to memoize the dictionary. Given that each of the methods in the TextStat class are class methods it does complicate things a little. An option would be to move each of the the class methods into instance methods then create wrappers on the class to create an instance (to keep backward compatibility).

ie

def self.sentence_count(text)
  new.sentence_count text
end

def sentence_count(text)
  ...
end

Of course could also use some meta programming to simplify the various class method calls too.

This would allow the dictionary to be memoized and minimise its cache being flushed, reducing runtime from hours to minutes.

Happy to put together a PR

kupolak commented 4 years ago

Good points. I know there may be some problems with performance due to the fact that the original python Texstat library utilizes LRU caching.

abrom commented 4 years ago

Here are the changes i've been using for the past few weeks with great success:

https://github.com/kupolak/textstat/compare/master...Studiosity:cache-dictionary

Note I've been sure to leave the existing interface untouched whilst opening up using the tools with great speed-ups:

text_service = TextStat.new 'ru'
reading_ease = text_service.flesch_reading_ease text
smog_index = text_service.smog_index text
...

Another (slight) improvement would be to pass the text block in through the initialiser to save passing it to each instance method..

Again, happy to put this in a PR if you're interested.

kupolak commented 4 years ago

PRs welcome.