Vil*_*age 18 ruby bash perl cjk python-2.7
我有两个文件,wordlist.txt和text.txt.
第一个文件wordlist.txt包含中文,日文和韩文的大量单词列表,例如:
?
??
?
Run Code Online (Sandbox Code Playgroud)
第二个文件text.txt包含长段落,例如:
???????
??OK????
Run Code Online (Sandbox Code Playgroud)
我想创建一个新的单词列表(wordsfount.txt),但它应该只包含wordlist.txt至少在其中找到一行的那些行text.txt.上面的输出文件应该显示如下:
?
??
Run Code Online (Sandbox Code Playgroud)
在此列表中找不到"我",因为它从未找到过text.txt.
我想找到一种非常快速的方法来创建此列表,该列表仅包含第二个文件中的第一个文件中的行.
我在BASH中知道一种简单的方法来检查每一行worlist.txt并查看它是否在text.txt使用中grep:
a=1
while read line
do
c=`grep -c $line text.txt`
if [ "$c" -ge 1 ]
then
echo $line >> wordsfound.txt
echo "Found" $a
fi
echo "Not found" $a
a=`expr $a + 1`
done < wordlist.txt
Run Code Online (Sandbox Code Playgroud)
不幸的是,这wordlist.txt是一个很长的清单,这个过程需要很长时间.必须有一个更快的解决方案.这是一个考虑因素:
由于文件包含CJK字母,因此可以将它们视为一个包含大约8,000个字母的巨型字母.所以几乎每个单词都共享字符.例如:
?
??
Run Code Online (Sandbox Code Playgroud)
由于这个事实,如果从未找到"我" text.txt,那么"我们"也不会出现.更快的脚本可能首先检查"我",并且在发现它不存在时,将避免检查包含在wordlist.txt其中的每个后续单词wordlist.txt.如果找到大约8,000个唯一字符wordlist.txt,则脚本不需要检查这么多行.
创建列表的最快方法是什么,该列表仅包含第一个文件中也在第二个文件中找到的那些单词?
Ovi*_*vid 12
我从Gutenberg项目中获取了战争与和平的文本,并编写了以下脚本.如果打印所有单词也在/usr/share/dict/words其中war_and_peace.txt.你可以改变它:
perl findwords.pl --wordlist=/path/to/wordlist --text=/path/to/text > wordsfound.txt
Run Code Online (Sandbox Code Playgroud)
在我的电脑上,运行只需一秒钟.
use strict;
use warnings;
use utf8::all;
use Getopt::Long;
my $wordlist = '/usr/share/dict/words';
my $text = 'war_and_peace.txt';
GetOptions(
"worlist=s" => \$wordlist,
"text=s" => \$text,
);
open my $text_fh, '<', $text
or die "Cannot open '$text' for reading: $!";
my %is_in_text;
while ( my $line = <$text_fh> ) {
chomp($line);
# you will want to customize this line
my @words = grep { $_ } split /[[:punct:][:space:]]/ => $line;
next unless @words;
# This beasty uses the 'x' builtin in list context to assign
# the value of 1 to all keys (the words)
@is_in_text{@words} = (1) x @words;
}
open my $wordlist_fh, '<', $wordlist
or die "Cannot open '$wordlist' for reading: $!";
while ( my $word = <$wordlist_fh> ) {
chomp($word);
if ( $is_in_text{$word} ) {
print "$word\n";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的时间:
• [ovid] $ wc -w war_and_peace.txt
565450 war_and_peace.txt
• [ovid] $ time perl findwords.pl > wordsfound.txt
real 0m1.081s
user 0m1.076s
sys 0m0.000s
• [ovid] $ wc -w wordsfound.txt
15277 wordsfound.txt
Run Code Online (Sandbox Code Playgroud)
只需使用通讯
http://unstableme.blogspot.com/2009/08/linux-comm-command-brief-tutorial.html
comm -1 wordlist.txt text.txt
这可能对你有用:
tr '[:punct:]' ' ' < text.txt | tr -s ' ' '\n' |sort -u | grep -f - wordlist.txt
Run Code Online (Sandbox Code Playgroud)
基本上,从文件中创建一个新的单词列表text.txt并对其进行grep wordlist.txt.
注意您可能希望使用用于构建原始软件的软件wordlist.txt.在这种情况下,您只需要:
yoursoftware < text.txt > newwordlist.txt
grep -f newwordlist.txt wordlist.txt
Run Code Online (Sandbox Code Playgroud)
new file newlist.txt
for each word in wordlist.txt:
check if word is in text.txt (I would use grep, if you're willing to use bash)
if yes:
append it to newlist.txt (probably echo word >> newlist.txt)
if no:
next word
Run Code Online (Sandbox Code Playgroud)