code_junkie

What is a simple way to generate keywords from a text

Nov 13th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings 'all';
  5.  
  6. # Read the text:
  7. open my $ifh, '<', 'text.txt'
  8. or die "Cannot open file: $!";
  9. local $/;
  10. my $text = <$ifh>;
  11.  
  12. # Find all the words, and count how many times they appear:
  13. my %words = ( );
  14. map { $words{$_}++ }
  15. grep { length > 1 && $_ =~ m/^[@a-z-']+$/i }
  16. map { s/[",.]//g; $_ }
  17. split /s/, $text;
  18.  
  19. print "Words, sorted by frequency:n";
  20. my (@data_line);
  21. format FMT =
  22. @<<<<<<<<<<<<<<<<<<<<<<... @########
  23. @data_line
  24. .
  25. local $~ = 'FMT';
  26.  
  27. # Sort them by frequency:
  28. map { @data_line = ($_, $words{$_}); write(); }
  29. sort { $words{$b} <=> $words{$a} }
  30. grep { $words{$_} > 2 }
  31. keys(%words);
  32.  
  33. john@ubuntu-pc1:~/Desktop$ perl frequency.pl
  34. Words, sorted by frequency:
  35. for 32
  36. Jan 27
  37. am 26
  38. of 21
  39. your 21
  40. to 18
  41. in 17
  42. the 17
  43. Get 13
  44. you 13
  45. OTRS 11
  46. today 11
  47. PSM 10
  48. Card 10
  49. me 9
  50. on 9
  51. and 9
  52. Offline 9
  53. with 9
  54. Invited 9
  55. Black 8
  56. get 8
  57. Web 7
  58. Starred 7
  59. All 7
  60. View 7
  61. Obama 7
  62.  
  63. perl -ne'$h{$1}++while m/b(w{3,})b/g}{printf"%-20s %5dn",$_,$h{$_}for sort{$h{$b}<=>$h{$a}}grep{$h{$_}>2}keys%h'
  64.  
  65. perl -ne'$h{$1}++while m/b(w{3,})b/g}{printf"%-20s %5dn",$_,$h{$_}for sort{$h{$b}<=>$h{$a}}grep{$h{$_}>2}keys%h'
  66.  
  67. perl -ne'$h{$1}++while m/b(w{3,})b/g}{printf"%-20s %5dn",$_,$h{$_}for sort{$h{$b}<=>$h{$a}or$a cmp$b}grep{$h{$_}>2}keys%h'
Add Comment
Please, Sign In to add comment