Loading ...
Sorry, an error occurred while loading the content.

[Clip] Statistics on Multi-word Phrase.

Expand Messages
  • Charles M. Raine
    Hi Clippers: This is my version of this request that was made a few days ago. This version displays the Phrase and how many times it occurs in the specified
    Message 1 of 1 , Sep 4 7:14 PM
    • 0 Attachment
      Hi Clippers:

      This is my version of this request that was made a few days ago. This
      version displays the Phrase and how many times it occurs in the specified
      document and the number of words in the phrase. It also displays a list of
      all the individual words and how often each word occurs. It only searches
      for whole words and phrases delimited by blanks, but it creates a temporary
      file and eliminates most punctuation to improve the search process. The
      temporary file is then Discarded. Since some of the code in this clip is
      rather complicated I have included more than the average number of
      comments. I hope some of you will find this informative and useful.

      ---------< start clip >---------
      H="Phrase/Word Counts"
      ; Charles M. Raine <rainec@...>
      ; 09/04/1999, 07:45:12 PM
      ; Clear and define variables.
      ^!ClearVariables
      ^!Set %phcnt%=0
      ; Input Wizard for Phrase and File selection. One long line.
      ^!Set %phrase%=^?{Input a Search Phrase ( NO punctuation )};
      %file%=^?{(T=O)Browse to get Input File=^$GetDocumentPath$*.*}
      ; Opens selected file.
      ^!Open ^%file%
      ; Create Temporary File and copy file into it.
      ^!ToolBar Copy All
      ^!Set %temp%=^$GetDocumentPath$phrasewrk.txt
      ^!Open ^%temp%
      ^!Paste
      ; Eliminate punctuation to improve word Search.
      ^!Replace "." >> " " ATWS
      ^!Replace """ >> " " ATWS
      ^!Replace "!" >> " " ATWS
      ^!Replace "?" >> " " ATWS
      ^!Replace "(" >> " " ATWS
      ^!Replace ")" >> " " ATWS
      ^!Replace "," >> " " ATWS
      ^!Replace ":" >> " " ATWS
      ^!Replace ";" >> " " ATWS
      ^!Replace "/" >> " " ATWS
      ^!Replace " " >> " " ATWS
      ; Eliminate extra blanks from phrase.
      ^!Set %phrase%=^$StrTrim("^%phrase%")$
      ^!Set %phrase%=^$StrReplace(" ";" ";"^%phrase%";false;false)$
      ^!If ^$StrCount(" ";"^%phrase%";false;false)$<>0 Skip_-1
      ; Negates hintinfo for faster processing.
      ^!SetHintInfo Working....
      ^!SetScreenUpdate OFF
      ; Position cursor to start of file.
      ^!Jump 1
      ; The following loop counts the number of times
      ; the Phrase occurs in the file.
      :loop
      ^!Find "^%phrase%" CIS
      ^!IfError donephrase
      ^!Inc %phcnt%
      ^!GoTo loop

      :donephrase
      ; Counts words in phrase by searching for blanks in phrase +1
      ^!Set %cnt%=^$Calc(1+^$StrCount(" ";"^%phrase%";false;false)$)$
      ; Adds blank to end of Saved phrase to facilitate processing words
      ^!Set %savphr%=^%phrase%" "
      ; Initialize variables to build an Array of the words.
      ^!Set %savcnt%=1
      ^!Set %inc%=000
      ^!SetListDelimiter ^p
      ^!Set %pos%=0

      ; Stores First word from the phrase.
      ^!Set %pos1%=^$StrPos(" ";"^%savphr%";false)$
      ^!Set
      %word%=^$StrCopy("^%phrase%";^$Calc(^%pos%+1)$;^$Calc(^%pos1%-1-^%pos%)$)$
      ; Each entry in the array contains 'word|000'
      ; which is 'the word', 'word counter' initialized to 000 separated by |.
      ^!SetArray %words%=^%word%|^%inc%

      ; Loop to store subsequent words from the phrase.
      :loop2
      ^!If ^%cnt%<>^%savcnt% Next ELSE search
      ^!Set %pos%=^%pos1%
      ^!Set %savphr%=^$StrInsert("-";"^%savphr%";^%pos1%)$
      ^!Set %savphr%=^$StrDelete("^%savphr%";^$Calc(^%pos1%+1)$;1)$
      ^!Set %pos1%=^$StrPos(" ";"^%savphr%";false)$
      ^!Set
      %word%=^$StrCopy("^%phrase%";^$Calc(^%pos%+1)$;^$Calc(^%pos1%-1-^%pos%)$)$
      ^!Inc %savcnt%
      ^!Set %words^%savcnt%%=^%word%|^%inc%
      ^!Set %words0%=^%savcnt%
      ^!GoTo loop2

      ; The following routine searchs for the number
      ; of occurrences of each word in the file.
      :search
      ; Initialize array process to start.
      ^!Set %i%=1
      :search2
      ; Test to see if all words processed.
      ^!If ^%cnt%<^%i% finish
      ; Extract word and counter from the array.
      ^!Set %aword%=^%words^%i%%
      ^!Set %loc%=^$StrPos("|";"^%aword%";false)$
      ^!Dec %loc%
      ^!Set %word%=^$StrCopy("^%aword";1;^%loc%)$
      ^!Inc %loc% 2
      ^!Set %inc%=^$StrCopy("^%aword";^%loc%;3)$
      ^!Jump 1

      ; Loop to count the occurrences of word.
      :loop3
      ^!Find "^%word" CIS
      ^!IfError Skip_2
      ^!Inc %inc%
      ^!GoTo loop3

      ; This stores the counter back into the array.
      ^!Set %aword%=^$StrReplace("000";"^%inc%";"^%aword%";false;false)$
      ^!Set %words^%i%%=^%aword%
      ; Get ready to process the Next word.
      ^!Inc %i%
      ^!GoTo search2

      :finish
      ^!SetScreenUpdate ON
      ^!ToolBar Clear All
      ^!Save
      ^!Close "^%temp%"
      ; Note: the | is replaced by a Tab Character, for display purposes.
      ; The following displays the Final Information Window and is one Long Line.
      ^!Info [L]Phrase:^p" ^%phrase% "^p^pPhrase Count:^%TAB%^%phcnt%^pWords in
      phrase:^%TAB%^%cnt%^p^pWord List
      Totals^p^$StrReplace("|";"^%TAB%";"^%words%";false;false)$
      ---------< end clip >-----------
      |----< Copy between wrappers >----|
      | right click over the clipbook |
      | and choose "Add from Clipboard" |
      |---------------------------------|

      Charlie.

      Charles M. Raine Home Page: http://www.escape.ca/~rainec
      Phone (204) 895-0940 ICQ # 300058
      Winnipeg, Manitoba, Canada
    Your message has been successfully submitted and would be delivered to recipients shortly.