3つの連続した単語を検索してください。

3つの連続した単語を検索してください。

私の書籍リスト(txtファイル)には、次のような重複した書籍があります。 -

The Ideal Team Player
The Ideal Team Player: How to Recognize and Cultivate The Three Essential Virtues
Ideal Team Player: Recognize and Cultivate The Three Essential Virtues

Joy on Demand: The Art of Discovering the Happiness Within
Crucial Conversations Tools for Talking When Stakes Are High
Joy on Demand

Search Inside Yourself: The Unexpected Path to Achieving Success, Happiness
Search Inside Yourself
......
......
......

重複した書籍を見つけて確認し、手動で削除する必要があります。検索してみると、線にはパターンが必要だとわかりました。

前任者。

部分行比較に基づいて重複行を削除する

ファイル内で部分的に繰り返される行を探し、各行が何回繰り返されるかを計算しますか?

しかし、私の場合、線のパターンを見つけるのは難しかった。しかし、私は単語の順序でパターンを見つけました。

3つの連続した単語がある行だけを重複して表示したいと思います。(大文字と小文字を区別しない)

見ればわかるけど―――

The Ideal Team Player
The Ideal Team Player: How to Recognize and Cultivate The Three Essential Virtues
Ideal Team Player: Recognize and Cultivate The Three Essential Virtues

Ideal Team Player私が探している連続語。

出力が次のように表示されます。

3 Ideal Team Player
2 Joy on Demand
2 Search Inside Yourself
......
......
......

どうすればいいですか?

ベストアンサー1

次のawkプログラムは、3つの連続した単語で構成される各グループの発生回数を格納し(句読点が削除された後)、数が1より大きい場合は、数と最後の単語のグループを印刷します。

{
        gsub("[[:punct:]]", "")

        for (i = 3; i <= NF; ++i)
                w[$(i-2),$(i-1),$i]++
}
END {
        for (key in w) {
                count = w[key]
                if (count > 1) {
                        gsub(SUBSEP," ",key)
                        print count, key
                }
        }
}

あなたの質問にテキストが与えられた場合、これは次のようになります。

2 Search Inside Yourself
2 Cultivate The Three
2 The Three Essential
2 Joy on Demand
2 Recognize and Cultivate
2 Three Essential Virtues
2 and Cultivate The
2 The Ideal Team
3 Ideal Team Player

ご覧のとおり、これはあまり役に立ちません。

代わりに、同じ数の情報を収集してからファイルを2番目に渡すことで、数が1より大きい単語の三重項を含む各行を印刷できます。

NR == FNR {
        gsub("[[:punct:]]", "")

        for (i = 3; i <= NF; ++i)
                w[$(i-2),$(i-1),$i]++

        next
}

{
        orig = $0
        gsub("[[:punct:]]", "")

        for (i = 3; i <= NF; ++i)
                if (w[$(i-2),$(i-1),$i] > 1) {
                        print orig
                        next
                }
}

ファイルをテストします。

$ cat file
The Ideal Team Player
The Ideal Team Player: How to Recognize and Cultivate The Three Essential Virtues
Ideal Team Player: Recognize and Cultivate The Three Essential Virtues

Joy on Demand: The Art of Discovering the Happiness Within
Crucial Conversations Tools for Talking When Stakes Are High
Joy on Demand

Search Inside Yourself: The Unexpected Path to Achieving Success, Happiness
Search Inside Yourself
$ awk -f script.awk file file
The Ideal Team Player
The Ideal Team Player: How to Recognize and Cultivate The Three Essential Virtues
Ideal Team Player: Recognize and Cultivate The Three Essential Virtues
Joy on Demand: The Art of Discovering the Happiness Within
Joy on Demand
Search Inside Yourself: The Unexpected Path to Achieving Success, Happiness
Search Inside Yourself

警告:awkプログラムはファイルテキストの約3倍を格納するのに十分なメモリを必要とし、アイテムが実際に重複していなくても一般的なフレーズで重複するアイテムを見つけることができます(たとえば、「料理方法」は複数のタイトルのトピックである可能性があります)。書籍)。

おすすめ記事