1

背景

创建了一个脚本来计算纯文本文件中单词的频率。该脚本执行以下步骤:

  1. 计算语料库中单词的频率。
  2. 保留在字典中找到的语料库中的每个单词。
  3. 创建一个以逗号分隔的频率文件。

脚本位于: http: //pastebin.com/VAZdeKXs

#!/bin/bash

# Create a tally of all the words in the corpus.
#
echo Creating tally of word frequencies...
sed -e 's/ /\n/g' -e 's/[^a-zA-Z\n]//g' corpus.txt | \
  tr [:upper:] [:lower:] | \
  sort | \
  uniq -c | \
  sort -rn > frequency.txt

echo Creating corpus lexicon...
rm -f corpus-lexicon.txt

for i in $(awk '{if( $2 ) print $2}' frequency.txt); do
  grep -m 1 ^$i\$ dictionary.txt >> corpus-lexicon.txt;
done

echo Creating lexicon...
rm -f lexicon.txt

for i in $(cat corpus-lexicon.txt); do
  egrep -m 1 "^[0-9 ]* $i\$" frequency.txt | \
    awk '{print $2, $1}' | \
    tr ' ' ',' >> lexicon.txt;
done

问题

以下几行不断地在字典中循环以匹配单词:

for i in $(awk '{if( $2 ) print $2}' frequency.txt); do
  grep -m 1 ^$i\$ dictionary.txt >> corpus-lexicon.txt;
done

它可以工作,但速度很慢,因为它正在扫描找到的单词以删除任何不在字典中的单词。代码通过扫描字典中的每个单词来执行此任务。(该-m 1参数在找到匹配项时停止扫描。)

问题

您将如何优化脚本,以便字典不会从头到尾扫描每个单词?大多数单词不会出现在字典中。

谢谢!

4

3 回答 3

2

您可以使用grep -f通过frequency.txt 一次性搜索所有单词:

awk '{print $2}' frequency.txt | grep -Fxf dictionary.txt > corpus-lexicon.txt
  • -F搜索固定字符串。
  • -x仅匹配整行。
  • -f从 dictionary.txt 中读取搜索模式

事实上,您甚至可以将其与第二个循环结合起来,并消除中间的 corpus-lexicon.txt 文件。两个 for 循环可以用一个 grep 替换:

grep -Fwf dictionary.txt frequency.txt | awk '{print $2 "," $1}'

请注意,我更改-x-w.

于 2011-01-07T15:59:09.007 回答
1

这通常是您为了速度而用 Perl 编写的那些脚本之一。但是,如果您像我一样讨厌只写编程语言,那么您可以在 awk 中完成这一切:

awk '
    BEGIN {
        while ((getline < "dictionary.txt") > 0)
            dict[$1] = 1
    }
    ($2 && $2 in dict) { print $2 }
' < frequency.txt > corpus-lexicon.txt

rm -f corpus-lexicon.txt在这个版本中不需要。

于 2011-01-07T16:10:44.117 回答
0

使用真正的编程语言。所有的应用程序启动和文件扫描都在杀死你。例如,这是我刚刚在 Python 中创建的一个示例(最小化代码行数):

import sys, re
words = re.findall(r'(\w+)',open(sys.argv[1]).read())
counts = {}
for word in words:
  counts[word] = counts.setdefault(word,0) + 1
open(sys.argv[2],'w').write("\n".join([w+','+str(c) for (w,c) in counts.iteritems()]))

对我坐在那里的一个大文本文件(1.4MB,根据 wc 为 80,000 个字)进行测试,这在 5 岁的 powermac 上在一秒钟内完成(18k 个唯一字)。

于 2011-01-07T16:09:17.767 回答