【问题标题】:Applescript Duplicate Word CountApplescript重复字数
【发布时间】:2014-05-09 20:28:47
【问题描述】:

我如何创建一个applescript来计算pdf中的重复单词,然后在层次结构中显示结果,其中重复最多的单词位于顶部(及其计数),第二多的第二个,依此类推?我想在学校使用它,以便在将 ppt 转换为 pdf 后,我可以运行此脚本来查看演示文稿中最重要的内容。

理想情况下,它会过滤掉诸如:the、so、it 等词。

【问题讨论】:

  • 到目前为止你有什么
  • collabedit.com/peh9k 我从在线书籍中获得代码,但 AppleScript Editor 在第 17 行出现问题 this 是本书
  • this applescript 正在编译,但它没有创建新的文本编辑文档。此外,它的注释首先显示频率最高的单词,第二个单词,等等,我仍然需要过滤某些“常见”单词。
  • updated code: 所以我找到了this page,该人试图将这段代码作为他的代码传递出去,建议删除return word_frequency_list 行,它可以生成新的文本编辑文档。现在我只需要根据单词的数量对单词进行排名,然后过滤掉特定的单词。
  • I have just finish an example that I have found in the internet 并不意味着他试图将代码作为他的.?。无论如何,你似乎到达那里......

标签: applescript word-count duplicates


【解决方案1】:

你要找的最后一部分很简单。

只需设置一个列表并检查该单词是否在其中。

    set ignoreList to {"to", "is"}
    set reportFile to "/Users/USERNAME/Desktop/Word Frequencies.txt"
set theTextFile to "Users/USERNAME/Desktop/foo.txt")


set word_list to every word of (do shell script "cat " & quoted form of theTextFile)

    set word_frequency_list to {}

    repeat with the_word_ref in word_list
        set the_current_word to contents of the_word_ref
        if the_current_word is not in ignoreList then

            set word_info to missing value

            repeat with record_ref in word_frequency_list
                if the_word of record_ref = the_current_word then
                    set word_info to contents of record_ref
                    exit repeat
                end if
            end repeat

            if word_info = missing value then
                set word_info to {the_word:the_current_word, the_count:1}
                set end of word_frequency_list to word_info
            else
                set the_count of word_info to (the_count of word_info) + 1
            end if

        end if
    end repeat
    --return word_frequency_list

    set the_report_list to {}
    repeat with word_info in word_frequency_list
        set end of the_report_list to quote & the_word of word_info & ¬
            quote & "  - appears " & the_count of word_info & " times."
    end repeat

    set AppleScript's text item delimiters to return
    set the_report to the_report_list as text
    do shell script "echo  " & quoted form of the_report & " >  " & quoted form of reportFile
    set AppleScript's text item delimiters to ""
    delay 1
    do shell script " open   " & quoted form of reportFile

我还更改了一些代码以使用 shell 脚本读取/写入文件。只是因为我更喜欢使用它而不是 Textedit。

【讨论】:

    【解决方案2】:

    虽然它在 applescript 中是可行的,如 markhunte 所示,但它非常慢。如果您正在处理较大的文本或大量文件,applescript 非常慢。在我的测试中,我放弃了它。因此,这里有一个简短的 shell 脚本,如果需要,您可以从 applescript 调用它,速度非常快。

    #!/bin/sh
    
    [ "$1" = "" ] || [ "$2" = "" ] && echo "$0 [wordsfile] [textfile]" && exit 1 
    
    INFILE="$2"
    WORDS="${2}.words"
    EXWORDS="$1"
    
    echo "File $INFILE has `cat $INFILE | wc -w ` words."
    echo "Excluding the `cat $EXWORDS | wc -w` words."
    
    echo "Extracting words from file and removing common words..."
    grep -o -E '\w{3,}' $INFILE | grep -x -i -v -f $EXWORDS > $WORDS
    
    echo "Top 10 most frequest words in $INFILE are..."
    cat "$WORDS" | tr [:upper:] [:lower:] | sort | uniq -c | sort -rn | head -10
    
    # Clean up
    rm $WORDS
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多