質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

438閲覧

文字列の一致について

PTK

総合スコア29

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/09/22 08:58

編集2021/09/22 12:15

pythonで4単語以上の単語が連続して重複している箇所を抽出すると言うことをしたいのですが、正規表現を使って抽出することはできるのでしょうか。調べてみてもあらかじめ比べる文字が決まっている場合のみの例しか出てきません。自分で思いつく方法は文章をすべて単語に分けて総当たりする方法しか思いつきません。何かいい方法を知っている方がいたら教えてください。お願いします。
例えば例文1と2を比べる場合、

例文1
When hay is followed by a plural noun, the article is omitted. If we want to describe the quantity of the plural noun, we use a number after “hay” (as we saw in one of the previous examples). This application includes two hundred keywords with transcription, explanation, synonyms, and samples selected from the SAT vocabulary.

例文2
When hay is followed by a plural noun, the article is omitted. Under Study tools, you will find a pop-up grammar guide complete with examples. I am a party inside my head.
Today is sunny. I am a party inside my head.

この場合例文1と2で共通した、4単語以上が連続している部分を抜き出したいです。When hay is followed by a plural noun, the article is omitted.だけを抜き出したいです。
回答いただいた方法で試してみましたが、例文2にて重複する部分も抜き出されてしまいます。( I am a party inside my head.)

頂いた方法

python

1text=""" 2When hay is followed by a plural noun, the article is omitted. If we want to describe the quantity of the plural noun, we use a number after “hay” (as we saw in one of the previous examples). 3When hay is followed by a plural noun, the article is omitted. 4""" 5 6import re 7rex = re.compile(r"(\b\w+(?:\W+\w+){3,}\b).*\1",re.DOTALL) 8print(re.findall(rex, text))

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ppaul

2021/09/22 09:03

4単語以上の単語が連続して重複している箇所 の意味がわかりません。 「4単語以上の単語」は「4文字以上の単語」ですか。 よくわからないので例を示してください。
PTK

2021/09/22 11:30

説明が下手で申し訳ありません。 4単語以上の文です。
退会済みユーザー

退会済みユーザー

2021/09/22 13:09

たとえば例文2の先頭が "When hay is followed by a PROPER noun, the article is omitted."だった場合、 "When hay is followed by a"(6単語マッチ) と "noun, the article is omitted."(5単語マッチ) を両方とも抜き出すということでしょうか?
PTK

2021/09/22 13:48

はい!そうです!
guest

回答2

0

ベストアンサー

otnさんの答えにほんの少しだけ追加しました。

python

1>>> text1 = '''When hay is followed by a plural noun, the article is omitted. If we want to describe the quantity of the plural noun, we use a number after "hay" (as we saw in one of the previous examples). This application includes two hundred keywords with transcription, explanation, synonyms, and samples selected from the SAT vocabulary.''' 2>>> 3>>> text2 = '''When hay is followed by a plural noun, the article is omitted. Under Study tools, you will find a pop-up grammar guide complete with examples. I am a party inside my head. 4... Today is sunny. I am a party inside my head.''' 5>>> 6>>> import re 7>>> rex = re.compile(r"(\b\w+(?:\W+\w+){3,}\b).*\1",re.DOTALL) 8>>> 9>>> print([s for s in re.findall(rex, text1 + text2) if s in text1 and s in text2]) 10['When hay is followed by a plural noun, the article is omitted']

投稿2021/09/22 12:45

ppaul

総合スコア24666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

Python

1text=""" 2When hay is followed by a plural noun, the article is omitted. If we want to describe the quantity of the plural noun, we use a number after “hay” (as we saw in one of the previous examples). 3When hay is followed by a plural noun, the article is omitted. 4""" 5 6import re 7rex = re.compile(r"(\b\w+(?:\W+\w+){3,}\b).*\1",re.DOTALL) 8print(re.findall(rex, text))

でしょうか。

投稿2021/09/22 09:29

otn

総合スコア84808

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問