快速shell命令删除文本文件中的停用词

时间:2015-06-01 12:57:37

标签: shell nlp text-processing

我有一个2GB的文本文件。我试图从这个文件中删除经常出现的英语停用词。

我有像这样的stopwords.txt ..

a
an
the
for
and
I

使用shell命令(如tr,sed或awk)执行此操作的快速方法是什么?

1 个答案:

答案 0 :(得分:2)

这是使用命令行和perl的方法:

将下面的文字保存为replacesw.sh

#! /bin/bash
MYREGEX=\\b\(`perl -pe 's/\n/|/g' $1`\)\\b
perl -pe "s/$MYREGEX//g" $2

然后,如果您已将上面的文件保存为stopwords.txt,并且有第二个文件(例如)名为testtext.txt,其中包含:

This is a file with the stopwords from the stopwords.txt for testing.
More than one line in the file, for a better test.

然后命令行中的以下内容将删除stopwords

KBs-MBP13:temp kbenoit$ ./replacesw.sh stopwords.txt testtext.txt 
This is  file with  stopwords from  stopwords.txt  testing.
More than one line in  file,   better test.

您可能需要先chmod u+x replacesw.sh