2013-12-11 18 views
20

Sto cercando di eliminare la punteggiatura in modo efficiente da una stringa unicode. Con una stringa normale, utilizzando mystring.translate(None, string.punctuation) è chiaramente il fastest approach. Tuttavia, questo codice si interrompe su una stringa unicode in Python 2.7. Come spiegano i commenti a questo answer, il metodo di traduzione può ancora essere implementato, ma deve essere implementato con un dizionario. Tuttavia, quando utilizzo questo implementation, trovo che le prestazioni di translate si riducano drasticamente. Qui è il mio codice di distribuzione (copiato in primo luogo da questa answer):Il modo più veloce per eliminare la punteggiatura da una stringa unicode in Python

import re, string, timeit 
import unicodedata 
import sys 


#String from this article www.wired.com/design/2013/12/find-the-best-of-reddit-with-this-interactive-map/ 

s = "For me, Reddit brings to mind Obi Wan’s enduring description of the Mos Eisley cantina: a wretched hive of scum and villainy. But, you know, one you still kinda want to hang out in occasionally. The thing is, though, Reddit isn’t some obscure dive bar in a remote corner of the universe—it’s a huge watering hole at the very center of it. The site had some 400 million unique visitors in 2012. They can’t all be Greedos. So maybe my problem is just that I’ve never been able to find the places where the decent people hang out." 
su = u"For me, Reddit brings to mind Obi Wan’s enduring description of the Mos Eisley cantina: a wretched hive of scum and villainy. But, you know, one you still kinda want to hang out in occasionally. The thing is, though, Reddit isn’t some obscure dive bar in a remote corner of the universe—it’s a huge watering hole at the very center of it. The site had some 400 million unique visitors in 2012. They can’t all be Greedos. So maybe my problem is just that I’ve never been able to find the places where the decent people hang out." 


exclude = set(string.punctuation) 
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(None, string.punctuation) 

tbl = dict.fromkeys(i for i in xrange(sys.maxunicode) 
         if unicodedata.category(unichr(i)).startswith('P')) 

def test_trans_unicode(su): 
    return su.translate(tbl) 

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) 

print "sets (unicode)  :",timeit.Timer('f(su)', 'from __main__ import su,test_set as f').timeit(1000000) 
print "regex (unicode)  :",timeit.Timer('f(su)', 'from __main__ import su,test_re as f').timeit(1000000) 
print "translate (unicode) :",timeit.Timer('f(su)', 'from __main__ import su,test_trans_unicode as f').timeit(1000000) 
print "replace (unicode) :",timeit.Timer('f(su)', 'from __main__ import su,test_repl as f').timeit(1000000) 

Come i miei risultati mostrano, l'attuazione unicode di tradurre esegue orribilmente:

sets  : 38.323941946 
regex  : 6.7729549408 
translate : 1.27428412437 
replace : 5.54967689514 

sets (unicode)  : 43.6268708706 
regex (unicode)  : 7.32343912125 
translate (unicode) : 54.0041439533 
replace (unicode) : 17.4450061321 

La mia domanda è se c'è un modo più veloce per implementare translate per unicode (o qualsiasi altro metodo) che possa sovraperformare la regex.

+0

dare un'occhiata veloce al codice sorgente C del ' translate' incorporato tra 'stringobject.c' e' unicodeobject.c' sono effettivamente implementati in modo molto diverso. – qwwqwwq

+0

Probabilmente potresti velocizzarlo, sì. Ci sono un sacco di chiamate di funzione che sono principalmente per chiarezza; questi potrebbero essere inline. Il problema principale per l'implementazione è che i char unicode sono più intensi da analizzare e che il numero di possibili sostituzioni è molto più grande (il tuo 'tbl' contiene 585 caratteri) che richiede la strategia della mappa usata in' unicodeobject'. Il metodo regex è troppo lento? – beerbajay

+0

Non stavo nemmeno pensando all'implementazione C, volevo solo dire che esiste il codice Python esistente che può sovraperformare il metodo regex. Il metodo regex andrà bene, è quello che ho implementato per ora, ma è cinque volte più lento e ho un sacco di testo, quindi ho pensato di chiedere. – Michael

risposta

6

Lo script di test corrente è difettoso, perché non si confronta come con simili.

Per un confronto più equo, tutte le funzioni devono essere eseguite con lo stesso set di caratteri di punteggiatura (ad esempio, tutti gli ascii o tutti gli unicode).

Al termine, i metodi regex e replace fanno molto più con il set completo di caratteri di punteggiatura Unicode.

Per unicode completo, sembra che il metodo "set" sia il migliore. Tuttavia, se si desidera rimuovere solo i caratteri di punteggiatura ascii dalle stringhe Unicode, potrebbe essere preferibile codificare, tradurre e decodificare (in base alla lunghezza della stringa di input).

Il metodo "replace" può anche essere sostanzialmente migliorato eseguendo un test di contenimento prima di tentare la sostituzione (a seconda del preciso make-up della stringa).

Ecco alcuni risultati di esempio di un re-hash dello script di test:

$ python2 test.py 
running ascii punctuation test... 
using byte strings... 

set: 0.862006902695 
re: 0.17484498024 
trans: 0.0207080841064 
enc_trans: 0.0206489562988 
repl: 0.157525062561 
in_repl: 0.213351011276 

$ python2 test.py a 
running ascii punctuation test... 
using unicode strings... 

set: 0.927773952484 
re: 0.18892288208 
trans: 1.58275294304 
enc_trans: 0.0794939994812 
repl: 0.413739919662 
in_repl: 0.249747991562 

python2 test.py u 
running unicode punctuation test... 
using unicode strings... 

set: 0.978360176086 
re: 7.97941994667 
trans: 1.72471117973 
enc_trans: 0.0784001350403 
repl: 7.05612301826 
in_repl: 3.66821289062 

Ed ecco la ri-hash script:

# -*- coding: utf-8 -*- 

import re, string, timeit 
import unicodedata 
import sys 


#String from this article www.wired.com/design/2013/12/find-the-best-of-reddit-with-this-interactive-map/ 

s = """For me, Reddit brings to mind Obi Wan’s enduring description of the Mos 
Eisley cantina: a wretched hive of scum and villainy. But, you know, one you 
still kinda want to hang out in occasionally. The thing is, though, Reddit 
isn’t some obscure dive bar in a remote corner of the universe—it’s a huge 
watering hole at the very center of it. The site had some 400 million unique 
visitors in 2012. They can’t all be Greedos. So maybe my problem is just that 
I’ve never been able to find the places where the decent people hang out.""" 

su = u"""For me, Reddit brings to mind Obi Wan’s enduring description of the 
Mos Eisley cantina: a wretched hive of scum and villainy. But, you know, one 
you still kinda want to hang out in occasionally. The thing is, though, 
Reddit isn’t some obscure dive bar in a remote corner of the universe—it’s a 
huge watering hole at the very center of it. The site had some 400 million 
unique visitors in 2012. They can’t all be Greedos. So maybe my problem is 
just that I’ve never been able to find the places where the decent people 
hang out.""" 

def test_trans(s): 
    return s.translate(tbl) 

def test_enc_trans(s): 
    s = s.encode('utf-8').translate(None, string.punctuation) 
    return s.decode('utf-8') 

def test_set(s): # with list comprehension fix 
    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_repl(s): # From S.Lott's solution 
    for c in punc: 
     s = s.replace(c, "") 
    return s 

def test_in_repl(s): # From S.Lott's solution, with fix 
    for c in punc: 
     if c in s: 
      s = s.replace(c, "") 
    return s 

txt = 'su' 
ptn = u'[%s]' 

if 'u' in sys.argv[1:]: 
    print 'running unicode punctuation test...' 
    print 'using unicode strings...' 
    punc = u'' 
    tbl = {} 
    for i in xrange(sys.maxunicode): 
     char = unichr(i) 
     if unicodedata.category(char).startswith('P'): 
      tbl[i] = None 
      punc += char 
else: 
    print 'running ascii punctuation test...' 
    punc = string.punctuation 
    if 'a' in sys.argv[1:]: 
     print 'using unicode strings...' 
     punc = punc.decode() 
     tbl = {ord(ch):None for ch in punc} 
    else: 
     print 'using byte strings...' 
     txt = 's' 
     ptn = '[%s]' 
     def test_trans(s): 
      return s.translate(None, punc) 
     test_enc_trans = test_trans 

exclude = set(punc) 
regex = re.compile(ptn % re.escape(punc)) 

def time_func(func, n=10000): 
    timer = timeit.Timer(
     'func(%s)' % txt, 
     'from __main__ import %s, test_%s as func' % (txt, func)) 
    print '%s: %s' % (func, timer.timeit(n)) 

print 
time_func('set') 
time_func('re') 
time_func('trans') 
time_func('enc_trans') 
time_func('repl') 
time_func('in_repl') 
+0

+1 @ekhumoro, non riesco a dormire :-) Stavo pensando i tuoi commenti e hai ragione, la questione è sviluppato in modo non corretto, la vostra risposta è quella giusta (IMHO), così ho tolto la miniera. – Roberto

+0

@ RobertoSánchez. Grazie! E spero che non ti faccia incubi ;-) – ekhumoro

Problemi correlati