文字列から句読点を取り除く最良の方法
-
06-07-2019 - |
質問
次の方法よりも簡単な方法があるはずです
import string
s = "string. With. Punctuation?" # Sample string
out = s.translate(string.maketrans("",""), string.punctuation)
ありますか?
解決
効率性の観点から、あなたは負けるつもりはありません
s.translate(None, string.punctuation)
Pythonの上位バージョンでは、次のコードを使用します。
s.translate(str.maketrans('', '', string.punctuation))
Cでルックアップテーブルを使用して生の文字列操作を実行します。これに勝るものはあまりありませんが、独自のCコードを記述します。
速度が気にならない場合、別のオプションは次のとおりです:
exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)
これは、各文字のs.replaceよりも高速ですが、以下のタイミングからわかるように、正規表現やstring.translateなどの非純粋なPythonアプローチと同様に実行されません。このタイプの問題では、可能な限り低いレベルでそれを行うことで成果が得られます。
タイミングコード:
import re, string, timeit
s = "string. With. Punctuation"
exclude = set(string.punctuation)
table = string.maketrans("","")
regex = re.compile('[%s]' % re.escape(string.punctuation))
def test_set(s):
return ''.join(ch for ch in s if ch not in exclude)
def test_re(s): # From Vinko's solution, with fix.
return regex.sub('', s)
def test_trans(s):
return s.translate(table, string.punctuation)
def test_repl(s): # From S.Lott's solution
for c in string.punctuation:
s=s.replace(c,"")
return s
print "sets :",timeit.Timer('f(s)', 'from __main__ import s,test_set as f').timeit(1000000)
print "regex :",timeit.Timer('f(s)', 'from __main__ import s,test_re as f').timeit(1000000)
print "translate :",timeit.Timer('f(s)', 'from __main__ import s,test_trans as f').timeit(1000000)
print "replace :",timeit.Timer('f(s)', 'from __main__ import s,test_repl as f').timeit(1000000)
これにより、次の結果が得られます。
sets : 19.8566138744
regex : 6.86155414581
translate : 2.12455511093
replace : 28.4436721802
他のヒント
正規表現は、知っていれば十分単純です。
import re
s = "string. With. Punctuation?"
s = re.sub(r'[^\w\s]','',s)
上記のコードでは、すべてのNON [英数字(\ w)およびスペース(\ s)]を空の文字列に置き換えています(re.sub)。
したがって。そして?正規表現を介してs変数を実行した後、変数 's'に句読点は存在しません。
使用の便宜上、Python 2とPython 3の両方で文字列から句読点をストライピングするというメモを要約します。詳細な説明については、他の回答を参照してください。
Python 2
import string
s = "string. With. Punctuation?"
table = string.maketrans("","")
new_s = s.translate(table, string.punctuation) # Output: string without punctuation
Python 3
import string
s = "string. With. Punctuation?"
table = str.maketrans({key: None for key in string.punctuation})
new_s = s.translate(table) # Output: string without punctuation
myString.translate(None, string.punctuation)
通常、次のようなものを使用します:
>>> s = "string. With. Punctuation?" # Sample string
>>> import string
>>> for c in string.punctuation:
... s= s.replace(c,"")
...
>>> s
'string With Punctuation'
string.punctuation
はASCII only です!より正確な(しかし非常に遅い)方法は、unicodedataモジュールを使用することです:
# -*- coding: utf-8 -*-
from unicodedata import category
s = u'String — with - «punctation »...'
s = ''.join(ch for ch in s if category(ch)[0] != 'P')
print 'stripped', s
reファミリーに精通している場合、必ずしも単純ではありませんが、別の方法です。
import re, string
s = "string. With. Punctuation?" # Sample string
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
Python 3 str
またはPython 2 unicode
値の場合、 str.translate()
は辞書のみを受け取ります。そのマッピングでコードポイント(整数)が検索され、 None
にマッピングされたものはすべて削除されます。
句読点を(一部?)削除するには、次を使用します:
import string
remove_punct_map = dict.fromkeys(map(ord, string.punctuation))
s.translate(remove_punct_map)
dict.fromkeys()
クラスメソッドにより、キーのシーケンスに基づいてすべての値を None
に設定して、マッピングを簡単に作成できます。
ASCII句読点だけでなく all 句読点を削除するには、テーブルを少し大きくする必要があります。 J.Fをご覧ください。セバスチャンの答え(Python 3バージョン):
import unicodedata
import sys
remove_punct_map = dict.fromkeys(i for i in range(sys.maxunicode)
if unicodedata.category(chr(i)).startswith('P'))
string.punctuation
は、現実の世界で一般的に使用される句読点の負荷を逃します。 ASCII以外の句読点で機能するソリューションはどうですか?
import regex
s = u"string. With. Some・Really Weird、Non?ASCII。 「(Punctuation)」?"
remove = regex.compile(ur'[\p{C}|\p{M}|\p{P}|\p{S}|\p{Z}]+', regex.UNICODE)
remove.sub(u" ", s).strip()
個人的には、Pythonで文字列から句読点を削除する最良の方法だと思います:
- すべてのUnicode句読点を削除します
- 簡単に変更できます。たとえば、句読点を削除する場合は
\ {S}
を削除できますが、$
などの記号は保持します。 - 保持したいものと削除したいものを具体的に指定できます。たとえば、
\ {Pd}
はダッシュのみを削除します。 - この正規表現は、空白も正規化します。タブ、キャリッジリターン、その他の奇妙な部分を素敵な単一のスペースにマッピングします。
これはUnicode文字プロパティを使用します。 Wikipediaで詳細を読むことができます。
>これは最良の解決策ではないかもしれませんが、これは私がやった方法です。
import string
f = lambda x: ''.join([i for i in x if i not in string.punctuation])
これは私が書いた関数です。あまり効率的ではありませんが、簡単で、必要な句読点を追加または削除できます。
def stripPunc(wordList):
"""Strips punctuation from list of words"""
puncList = [".",";",":","!","?","/","\\",",","#","@","<*>quot;,"&",")","(","\""]
for punc in puncList:
for word in wordList:
wordList=[word.replace(punc,'') for word in wordList]
return wordList
Python 3.5のワンライナーです:
import string
"l*ots! o(f. p@u)n[c}t]u[a'ti\"on#$^?/".translate(str.maketrans({a:None for a in string.punctuation}))
この回答はまだ見ていません。正規表現を使用してください。単語の文字( \ w
)と数字の文字( \ d
)に続く空白文字( \ s
)を除くすべての文字を削除します。
import re
s = "string. With. Punctuation?" # Sample string
out = re.sub(ur'[^\w\d\s]+', '', s)
正規表現を使用しないソリューションです。
import string
input_text = "!where??and!!or$then:)"
punctuation_replacer = string.maketrans(string.punctuation, ' '*len(string.punctuation))
print ' '.join(input_text.translate(punctuation_replacer).split()).strip()
Output>> where and or then
- 句読点をスペースに置き換えます
- 単語間の複数のスペースを単一のスペースに置き換える
- 末尾のスペースがあれば削除します strip()
更新として、Python 3の@Brianの例を書き直し、関数内でregexコンパイルステップを移動するように変更しました。ここでの私の考えは、機能を機能させるために必要なすべてのステップを計ることでした。おそらく分散コンピューティングを使用していて、ワーカー間で正規表現オブジェクトを共有することができず、各ワーカーで re.compile
ステップが必要な場合があります。また、Python 3のmaketransの2つの異なる実装のタイミングを知りたいと思いました
table = str.maketrans({key: None for key in string.punctuation})
vs
table = str.maketrans('', '', string.punctuation)
さらに、setを使用する別の方法を追加しました。ここでは、交差関数を利用して反復回数を減らします。
これは完全なコードです:
import re, string, timeit
s = "string. With. Punctuation"
def test_set(s):
exclude = set(string.punctuation)
return ''.join(ch for ch in s if ch not in exclude)
def test_set2(s):
_punctuation = set(string.punctuation)
for punct in set(s).intersection(_punctuation):
s = s.replace(punct, ' ')
return ' '.join(s.split())
def test_re(s): # From Vinko's solution, with fix.
regex = re.compile('[%s]' % re.escape(string.punctuation))
return regex.sub('', s)
def test_trans(s):
table = str.maketrans({key: None for key in string.punctuation})
return s.translate(table)
def test_trans2(s):
table = str.maketrans('', '', string.punctuation)
return(s.translate(table))
def test_repl(s): # From S.Lott's solution
for c in string.punctuation:
s=s.replace(c,"")
return s
print("sets :",timeit.Timer('f(s)', 'from __main__ import s,test_set as f').timeit(1000000))
print("sets2 :",timeit.Timer('f(s)', 'from __main__ import s,test_set2 as f').timeit(1000000))
print("regex :",timeit.Timer('f(s)', 'from __main__ import s,test_re as f').timeit(1000000))
print("translate :",timeit.Timer('f(s)', 'from __main__ import s,test_trans as f').timeit(1000000))
print("translate2 :",timeit.Timer('f(s)', 'from __main__ import s,test_trans2 as f').timeit(1000000))
print("replace :",timeit.Timer('f(s)', 'from __main__ import s,test_repl as f').timeit(1000000))
これは私の結果です:
sets : 3.1830138750374317
sets2 : 2.189873124472797
regex : 7.142953420989215
translate : 4.243278483860195
translate2 : 2.427158243022859
replace : 4.579746678471565
>>> s = "string. With. Punctuation?"
>>> s = re.sub(r'[^\w\s]','',s)
>>> re.split(r'\s*', s)
['string', 'With', 'Punctuation']
import re
s = "string. With. Punctuation?" # Sample string
out = re.sub(r'[^a-zA-Z0-9\s]', '', s)
非常に厳密ではない場合には、ワンライナーが役立つ場合があります。
''.join([c for c in s if c.isalnum() or c.isspace()])
#FIRST METHOD
#Storing all punctuations in a variable
punctuation='!?,.:;"\')(_-'
newstring='' #Creating empty string
word=raw_input("Enter string: ")
for i in word:
if(i not in punctuation):
newstring+=i
print "The string without punctuation is",newstring
#SECOND METHOD
word=raw_input("Enter string: ")
punctuation='!?,.:;"\')(_-'
newstring=word.translate(None,punctuation)
print "The string without punctuation is",newstring
#Output for both methods
Enter string: hello! welcome -to_python(programming.language)??,
The string without punctuation is: hello welcome topythonprogramminglanguage
with open('one.txt','r')as myFile:
str1=myFile.read()
print(str1)
punctuation = ['(', ')', '?', ':', ';', ',', '.', '!', '/', '"', "'"]
for i in punctuation:
str1 = str1.replace(i," ")
myList=[]
myList.extend(str1.split(" "))
print (str1)
for i in myList:
print(i,end='\n')
print ("____________")
Pythonを使用してテキストファイルからストップワードを削除する
print('====THIS IS HOW TO REMOVE STOP WORS====')
with open('one.txt','r')as myFile:
str1=myFile.read()
stop_words ="not", "is", "it", "By","between","This","By","A","when","And","up","Then","was","by","It","If","can","an","he","This","or","And","a","i","it","am","at","on","in","of","to","is","so","too","my","the","and","but","are","very","here","even","from","them","then","than","this","that","though","be","But","these"
myList=[]
myList.extend(str1.split(" "))
for i in myList:
if i not in stop_words:
print ("____________")
print(i,end='\n')
これは、ドキュメントを大文字に変更する方法です または小文字。
print('@@@@This is lower case@@@@')
with open('students.txt','r')as myFile:
str1=myFile.read()
str1.lower()
print(str1.lower())
print('*****This is upper case****')
with open('students.txt','r')as myFile:
str1=myFile.read()
str1.upper()
print(str1.upper())
次のような関数を使用します:
def scrub(abc):
while abc[-1] is in list(string.punctuation):
abc=abc[:-1]
while abc[0] is in list(string.punctuation):
abc=abc[1:]
return abc