Linuxを使用していますか?これらのシェル関数を使用します
# copyright by Werner Rudolph <werner (at) artistoex (dot) net>
# copying and distributing of the following source code
# is permitted, as long as this note is preserved.
# ftr CHAR1 CHAR2
# translate delimiter char in frequency list
#
ftr()
{
sed -r 's/^( *[0-9]+)'"$1"'/\1'"$2"'/'
}
# valid-collocations -- find valid collocations in inputstream
# reads records COUNT<SPC>COLLOCATION from inputstream
# writes records with existing collocations to stdout.
valid-collocations ()
{
#sort -k 2 -m - "$coll" |uniq -f 1 -D|accumulate
local delimiter="_"
ftr ' ' $delimiter |
join -t $delimiter -o 1.1 0 -1 2 -2 1 - /tmp/wordsets-helper-collocations |
ftr $delimiter ' '
}
# ngrams MAX [MIN]
#
# Generates all n-grams (for each MIN <= n <= MAX, where MIN defaults to 2)
# from inputstream
#
# reads word list, as generated by
#
# $ words < text
#
# from stdin. For each WORD in wordlist, it writes MAX-1 records
#
# COUNT<TAB>WORD<SPC>SUCC_1<SPC>
# COUNT<TAB>WORD<SPC>SUCC_1<SPC>SUCC_2<SPC>
# :
# COUNT<TAB>WORD<SPC>SUCC_1<SPC>SUCC_2<SPC>...<SPC>SUCC_MAX-2
# COUNT<TAB>WORD<SPC>SUCC_1<SPC>SUCC_2<SPC>...<SPC>SUCC_MAX-1
#
# to stdout, where word SUCC follows word WORD, and SUCC_n follows
# SUCC_n-1 in input stream COUNT times.
ngrams ()
{
local max=$1
local min=${2:-2};
awk 'FNR > 1 {print old " " $0} {old=$1}' | if (( $max - 1 > 1 )); then
if (( $min <= 2 )); then
tee >( ngrams $(( $max - 1 )) $(( $min - 1 )) );
else
ngrams $(( $max - 1 )) $(( $min - 1 ));
fi;
else
cat;
fi
}
words() {
grep -Eo '\<([a-zA-Z]'"'"'?){'${1:-3}',}\>'|grep -v "[A-Z]"
}
parse-collocations() {
local freq=${1:-0}
local length=${2:-4}
words | ngrams $length | sort | uniq -c |
awk '$1 > '"$freq"' { print $0; }' |
valid-collocations
}
parse-collocation
実際に使用する関数はどこにありますか。2 つのオプション パラメータを受け入れます。最初のパラメータは、結果からスキップされる用語の最大繰り返し頻度を設定します (デフォルトは 0、つまりすべての用語を考慮します)。2 番目のパラメーターは、検索する単語の最大長を設定します。関数は stdin からテキストを読み取り、用語を 1 行ずつ stdout に出力します。辞書ファイルが必要です(ここから/tmp/wordsets-helper-collocations
ダウンロードしてください):
使用例:
$ parse-collocation < some-text
あなたが望むものはかなりでしょう。ただし、用語を辞書と一致させたくない場合は、これを使用できます
$ words < some-text | ngrams 3 4 | sort | uniq -c |sort -nr
ngrams
の最初のパラメーターは最小期間の長さを設定し、2 番目 (オプション) のパラメーターは最大期間の長さを設定します。