code-golftext-parsinglanguage-agnosticrosetta-stone

Code Golf: Quickly Build List of Keywords from Text, Including # of Instances


I've already worked out this solution for myself with PHP, but I'm curious how it could be done differently - better even. The two languages I'm primarily interested in are PHP and Javascript, but I'd be interested in seeing how quickly this could be done in any other major language today as well (mostly C#, Java, etc).

  1. Return only words with an occurrence greater than X
  2. Return only words with a length greater than Y
  3. Ignore common terms like "and, is, the, etc"
  4. Feel free to strip punctuation prior to processing (ie. "John's" becomes "John")
  5. Return results in a collection/array

Extra Credit

  1. Keep Quoted Statements together, (ie. "They were 'too good to be true' apparently")
    Where 'too good to be true' would be the actual statement

Extra-Extra Credit

  1. Can your script determine words that should be kept together based upon their frequency of being found together? This being done without knowing the words beforehand. Example:
    *"The fruit fly is a great thing when it comes to medical research. Much study has been done on the fruit fly in the past, and has lead to many breakthroughs. In the future, the fruit fly will continue to be studied, but our methods may change."*
    Clearly the word here is "fruit fly," which is easy for us to find. Can your search'n'scrape script determine this too?

Source text: http://sampsonresume.com/labs/c.txt

Answer Format

  1. It would be great to see the results of your code, output, in addition to how long the operation lasted.

Solution

  • GNU scripting

    sed -e 's/ /\n/g' | grep -v '^ *$' | sort | uniq -c | sort -nr
    

    Results:

      7 be
      6 to
    [...]
      1 2.
      1 -
    

    With occurence greater than X:

    sed -e 's/ /\n/g' | grep -v '^ *$' | sort | uniq -c | awk '$1>X'
    

    Return only words with a length greater than Y (put Y+1 dots in second grep):

    sed -e 's/ /\n/g' | grep -v '^ *$' | grep .... | sort | uniq -c
    

    Ignore common terms like "and, is, the, etc" (assuming that the common terms are in file 'ignored')

    sed -e 's/ /\n/g' | grep -v '^ *$' | grep -vf ignored | sort | uniq -c
    

    Feel free to strip punctuation prior to processing (ie. "John's" becomes "John"):

    sed -e 's/[,.:"\']//g;s/ /\n/g' | grep -v '^ *$' | sort | uniq -c
    

    Return results in a collection/array: it is already like an array for shell: first column is count, second is word.