Applescript: number list of words according to their occurrence in the sequence

谁说我不能喝 提交于 2019-12-14 03:36:28

问题


So I have this list of words:

{"It", "was", "the", "best", "of", "times", "it", "was", "the", "worst", "of", "times", "it", "was", "the", "age", "of", "wisdom", "it", "was", "the", "age", "of", "foolishness", "it", "was", "the", "epoch", "of", "belief"}

I'd like each occurrence of a word to be numbered according to the number of times it has occurred in the sequence:

{"It", 1}, {"was", 1}, {"the", 1}, {"best", 1}, {"of", 1}, {"times", 1} {"it", 2}, {"was", 2}, {"the", 2}, {"worst", 1}, {"of", 2}, {"times", 2}, {"it", 3}, {"was", 3}, {"the", 3}, {"age", 1}, {"of", 3}, {"wisdom", 1}, {"it", 4}, {"was", 4}, {"the", 4}, etc.

Any help would be greatly appreciated.


回答1:


AppleScript is not the right tool for this job, so while the following solution works, it is sloooow.

(For better performance, you'd need full hash-table functionality, which AppleScript doesn't provide - AppleScript's record class is severely handicapped by the inability to specify keys dynamically, at runtime, via variables - third-party solutions exist, though (e.g., http://www.latenightsw.com/freeware/list-record-tools/)).

# Helper handler: Given a search word, returns the number of times the word 
# already occurs in the specified list (as the first sub-item of each list item).
on countOccurrences(searchWrd, lst)
  local counter
  set counter to 0
  repeat with wordNumPair in lst
    if item 1 of wordNumPair is searchWrd then
      set counter to counter + 1
    end if
  end repeat
  return counter
end countOccurrences

# Define the input list.
set inList to {"It", "was", "the", "best", "of", "times", "it", "was", "the", "worst", "of", "times", "it", "was", "the", "age", "of", "wisdom", "it", "was", "the", "age", "of", "foolishness", "it", "was", "the", "epoch", "of", "belief"}

# Initialize the output list.
set outList to {}

# Loop over the input list and build the output list incrementally.
repeat with wrd in inList
  # Note that `contents of` returns the string content of the list item
  # (dereferences the object specifier that `repeat with` returns).
  set outList to outList & {{contents of wrd, 1 + (my countOccurrences(contents of wrd, outList))}}
end repeat

# outList now contains the desired result.



回答2:


Another way of doing it...

# Define the input list.
set inList to {"It", "was", "the", "best", "of", "times", "it", "was", "the", "worst", "of", "times", "it", "was", "the", "age", "of", "wisdom", "it", "was", "the", "age", "of", "foolishness", "it", "was", "the", "epoch", "of", "belief"}
set AppleScript's text item delimiters to linefeed
set inListLF to inList as text
set AppleScript's text item delimiters to {""}

set usedList to {}
set resultList to {}
repeat with aWord in inList
    set aWord to contents of aWord
    considering case
        if aWord is not in usedList then
            set end of usedList to aWord
            set end of resultList to aWord & " - " & (do shell script "echo " & quoted form of inListLF & " | grep -o " & quoted form of aWord & " | wc -l")
        end if
    end considering
end repeat

set AppleScript's text item delimiters to linefeed
set resultList to resultList as text
set AppleScript's text item delimiters to {""}
return resultList


来源:https://stackoverflow.com/questions/24582006/applescript-number-list-of-words-according-to-their-occurrence-in-the-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!