2016-02-11 15 views
8

मैं this question के माध्यम से जा रहा था।पायथन re.split() बनाम nltk word_tokenize और sent_tokenize

क्या मैं सोच रहा हूं कि एनएलटीके शब्द/वाक्य टोकननाइजेशन में रेगेक्स से तेज होगा या नहीं।

+0

... और आपने इसे करने से क्या रखा? एक नमूना चलाना और इसे 'टाइमिट' के साथ समय देना? – lenz

+0

am अजगर में नया नया, esp nltk। मैंने अभी ध्यान दिया है कि जब मैं nltk से स्विच करता हूं तो re.split(), s.split() तेज़ था। मैं इसका उपयोग करता था: वाक्यों = sent_tokenize (txt), अब यह: वाक्य = re.split (आर '(? <= [^ एजेड]। [।?]) + (? = [एजेड])', txt) – wakamdr

+0

ऐसा इसलिए हो सकता है क्योंकि इसे रनटाइम के दौरान वर्डनेट लोड करना पड़ता है, एनएलटीके धीमा होने का कारण है? – wakamdr

उत्तर

15

डिफ़ॉल्ट nltk.word_tokenize()Treebank tokenizer का उपयोग कर रहा है जो Penn Treebank tokenizer से टोकनज़र को अनुकरण करता है।

ध्यान दें कि str.split() भाषा विज्ञान अर्थ में टोकन प्राप्त नहीं करता है, उदा .:

>>> sent = "This is a foo, bar sentence." 
>>> sent.split() 
['This', 'is', 'a', 'foo,', 'bar', 'sentence.'] 
>>> from nltk import word_tokenize 
>>> word_tokenize(sent) 
['This', 'is', 'a', 'foo', ',', 'bar', 'sentence', '.'] 

यह आमतौर पर निर्दिष्ट सीमांकक, उदा अलग तार करने के लिए प्रयोग किया जाता है एक टैब से अलग फ़ाइल में, आप str.split('\t') का उपयोग कर सकते हैं या जब आप न्यूलाइन \n द्वारा स्ट्रिंग को विभाजित करने का प्रयास कर रहे हैं, जब आपकी टेक्स्टफाइल प्रति पंक्ति एक वाक्य है।

और के python3 में कुछ बेंच मार्किंग करते हैं:

import time 
from nltk import word_tokenize 

import urllib.request 
url = 'https://raw.githubusercontent.com/Simdiva/DSL-Task/master/data/DSLCC-v2.0/test/test.txt' 
response = urllib.request.urlopen(url) 
data = response.read().decode('utf8') 

for _ in range(10): 
    start = time.time() 
    for line in data.split('\n'): 
     line.split() 
    print ('str.split():\t', time.time() - start) 

for _ in range(10): 
    start = time.time() 
    for line in data.split('\n'): 
     word_tokenize(line) 
    print ('word_tokenize():\t', time.time() - start) 

[बाहर]:

str.split():  0.05451083183288574 
str.split():  0.054320573806762695 
str.split():  0.05368804931640625 
str.split():  0.05416440963745117 
str.split():  0.05299568176269531 
str.split():  0.05304527282714844 
str.split():  0.05356955528259277 
str.split():  0.05473494529724121 
str.split():  0.053118228912353516 
str.split():  0.05236077308654785 
word_tokenize():  4.056122779846191 
word_tokenize():  4.052812337875366 
word_tokenize():  4.042144775390625 
word_tokenize():  4.101543664932251 
word_tokenize():  4.213029146194458 
word_tokenize():  4.411528587341309 
word_tokenize():  4.162556886672974 
word_tokenize():  4.225975036621094 
word_tokenize():  4.22914719581604 
word_tokenize():  4.203172445297241 

अगर हम एक another tokenizers in bleeding edge NLTKhttps://github.com/jonsafari/tok-tok/blob/master/tok-tok.pl से प्रयास करें:

import time 
from nltk.tokenize import ToktokTokenizer 

import urllib.request 
url = 'https://raw.githubusercontent.com/Simdiva/DSL-Task/master/data/DSLCC-v2.0/test/test.txt' 
response = urllib.request.urlopen(url) 
data = response.read().decode('utf8') 

toktok = ToktokTokenizer().tokenize 

for _ in range(10): 
    start = time.time() 
    for line in data.split('\n'): 
     toktok(line) 
    print ('toktok:\t', time.time() - start) 

[बाहर]:

toktok: 1.5902607440948486 
toktok: 1.5347232818603516 
toktok: 1.4993178844451904 
toktok: 1.5635688304901123 
toktok: 1.5779635906219482 
toktok: 1.8177132606506348 
toktok: 1.4538452625274658 
toktok: 1.5094449520111084 
toktok: 1.4871931076049805 
toktok: 1.4584410190582275 

(नोट: पाठ फ़ाइल के स्रोत https://github.com/Simdiva/DSL-Task से है)


अगर हम देशी perl कार्यान्वयन को देखो, ToktokTokenizer के लिए समय python बनाम perl तुलनीय है। लेकिन उस अजगर कार्यान्वयन में regexes रहे हैं पूर्व संकलित जबकि पर्ल में, ऐसा नहीं है, लेकिन फिर the proof is still in the pudding:

[email protected]:~$ wget https://raw.githubusercontent.com/jonsafari/tok-tok/master/tok-tok.pl 
--2016-02-11 20:36:36-- https://raw.githubusercontent.com/jonsafari/tok-tok/master/tok-tok.pl 
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.31.17.133 
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.31.17.133|:443... connected. 
HTTP request sent, awaiting response... 200 OK 
Length: 2690 (2.6K) [text/plain] 
Saving to: ‘tok-tok.pl’ 

100%[===============================================================================================================================>] 2,690  --.-K/s in 0s  

2016-02-11 20:36:36 (259 MB/s) - ‘tok-tok.pl’ saved [2690/2690] 

[email protected]:~$ wget https://raw.githubusercontent.com/Simdiva/DSL-Task/master/data/DSLCC-v2.0/test/test.txt 
--2016-02-11 20:36:38-- https://raw.githubusercontent.com/Simdiva/DSL-Task/master/data/DSLCC-v2.0/test/test.txt 
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.31.17.133 
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.31.17.133|:443... connected. 
HTTP request sent, awaiting response... 200 OK 
Length: 3483550 (3.3M) [text/plain] 
Saving to: ‘test.txt’ 

100%[===============================================================================================================================>] 3,483,550 363KB/s in 7.4s 

2016-02-11 20:36:46 (459 KB/s) - ‘test.txt’ saved [3483550/3483550] 

[email protected]:~$ time perl tok-tok.pl <test.txt> /tmp/null 

real 0m1.703s 
user 0m1.693s 
sys 0m0.008s 
[email protected]:~$ time perl tok-tok.pl <test.txt> /tmp/null 

real 0m1.715s 
user 0m1.704s 
sys 0m0.008s 
[email protected]:~$ time perl tok-tok.pl <test.txt> /tmp/null 

real 0m1.700s 
user 0m1.686s 
sys 0m0.012s 
[email protected]:~$ time perl tok-tok.pl <test.txt> /tmp/null 

real 0m1.727s 
user 0m1.700s 
sys 0m0.024s 
[email protected]:~$ time perl tok-tok.pl <test.txt> /tmp/null 

real 0m1.734s 
user 0m1.724s 
sys 0m0.008s 

(नोट: जब tok-tok.pl समय, हम पाइप के लिए एक फ़ाइल में उत्पादन किया था, इतना समय यहाँ टाइम मशीन उत्पादन के लिए ले जाता है दायर करने के लिए भी शामिल है, जबकि nltk.tokenize.ToktokTokenizer समय में, यह उत्पादन करने के लिए समय शामिल नहीं है एक फ़ाइल में) है


साथ sent_tokenize() के संबंध

, यह थोड़ा अलग है और सटीकता पर विचार किए बिना स्पीड बेंचमार्क की तुलना करना थोड़ा क्विर्की है। 0 काम एक regex एक textfile/पैरा 1 वाक्य में विभाजन

  • है, तो गति लगभग तात्कालिक है, अर्थात:

    इस पर विचार करें। लेकिन यह एक भयानक वाक्य टोकननाइज़र होगा ...

  • एक फ़ाइल में वाक्य पहले से ही \n से अलग किया जाता है, तो उस बस की तुलना कैसे str.split('\n') बनाम re.split('\n') और nltk वाक्य tokenization कोई लेना देना नहीं होता है का एक मामला है; पी

जानकारी के लिए NLTK में कैसे sent_tokenize() काम करता है पर, देखें:

तो प्रभावी ढंग से बनाम अन्य regex आधारित विधियों sent_tokenize() तुलना करने के लिए (नहीं str.split('\n')), एक भी सटीकता का मूल्यांकन करें और एक tokenized प्रारूप में आदमियत का मूल्यांकन वाक्य के साथ एक डाटासेट है करना होगा।

इस कार्य पर विचार करें: https://www.hackerrank.com/challenges/from-paragraphs-to-sentences

पाठ को देखते हुए:

तीसरी श्रेणी में उन्होंने उन ब्रदर्स शामिल (बहुमत) जो फ़्रीमासोंरी लेकिन बाह्य रूपों और रीति-रिवाजों, और में कुछ भी नहीं देखा था उनके purport या महत्व के बारे में परेशान किए बिना इन रूपों के सख्त प्रदर्शन का मूल्यवान। विलारस्की और यहां तक ​​कि ग्रैंड प्रिंसिपल लॉज के मास्टर भी थे। आखिरकार, चौथी श्रेणी में भी बहुत से ब्रदर्स थे, विशेष रूप से जिनके पास हाल ही में शामिल थे। ये पियरे की टिप्पणियों के अनुसार पुरुषों के लिए जो कुछ भी करने के लिए कुछ भी करने में कोई विश्वास, और न ही इच्छा थी थे, लेकिन फ्रीमेसंस केवल धनी युवा ब्रदर्स जो प्रभावशाली थे उनके कनेक्शन या रैंक के माध्यम से के साथ संबद्ध करने में शामिल हो गए, और जिनमें से वहाँ बहुत थे लॉज में कई। पियर्रे ने के साथ असंतुष्ट महसूस करना शुरू कर दिया था। फ्रीमेसनरी, जिसने इसे यहां देखा था, कभी-कभी उसे बाहरी पर आधारित लग रहा था। उन्होंने फ्रीमेसनरी पर संदेह करने के बारे में नहीं सोचा था, लेकिन संदेह था कि रूसी चिनाई ने गलत मार्ग लिया था और इसके मूल सिद्धांतों से विचलित हो गया था। और की ओर उस वर्ष के अंत में वह विदेश में गए आदेश के रहस्यों में शुरू किया गया। इन परिस्थितियों में क्या किया जाना है? क्रांति का पक्ष लेते हैं, सबकुछ उखाड़ फेंकते हैं, बलपूर्वक मजबूर हो जाते हैं? नहीं! हम उससे बहुत दूर हैं। प्रत्येक हिंसक सुधार संवेदना का हकदार है, क्योंकि पुरुष बुराई का समाधान करने में असफल रहता है जबकि पुरुष रहते हैं, और क्योंकि ज्ञान को कोई हिंसा की आवश्यकता नहीं है। "लेकिन में ऐसा करने में क्या चल रहा है?" Ilagin के दूल्हे ने कहा। "एक बार जब वह इसे याद कर लेती है और इसे दूर कर देती है, तो कोई भी मोन्गल इसे ले सकता है," इलाजिन उसी समय पर कह रहा था, उसके गलियारे और उसके उत्साह से सांसहीन।

हम इस प्राप्त करना चाहते हैं:

In the third category he included those Brothers (the majority) who saw nothing in Freemasonry but the external forms and ceremonies, and prized the strict performance of these forms without troubling about their purport or significance. 
Such were Willarski and even the Grand Master of the principal lodge. 
Finally, to the fourth category also a great many Brothers belonged, particularly those who had lately joined. 
These according to Pierre's observations were men who had no belief in anything, nor desire for anything, but joined the Freemasons merely to associate with the wealthy young Brothers who were influential through their connections or rank, and of whom there were very many in the lodge. 
Pierre began to feel dissatisfied with what he was doing. 
Freemasonry, at any rate as he saw it here, sometimes seemed to him based merely on externals. 
He did not think of doubting Freemasonry itself, but suspected that Russian Masonry had taken a wrong path and deviated from its original principles. 
And so toward the end of the year he went abroad to be initiated into the higher secrets of the order. 
What is to be done in these circumstances? 
To favor revolutions, overthrow everything, repel force by force? 
No! 
We are very far from that. 
Every violent reform deserves censure, for it quite fails to remedy evil while men remain what they are, and also because wisdom needs no violence. 
"But what is there in running across it like that?" said Ilagin's groom. 
"Once she had missed it and turned it away, any mongrel could take it," Ilagin was saying at the same time, breathless from his gallop and his excitement. 

तो बस str.split('\n') कर आप कुछ भी नहीं दे देंगे।वाक्यों के आदेश पर विचार किए बिना, आप 0 सकारात्मक परिणाम प्राप्त करेंगे:

>>> text = """In the third category he included those Brothers (the majority) who saw nothing in Freemasonry but the external forms and ceremonies, and prized the strict performance of these forms without troubling about their purport or significance. Such were Willarski and even the Grand Master of the principal lodge. Finally, to the fourth category also a great many Brothers belonged, particularly those who had lately joined. These according to Pierre's observations were men who had no belief in anything, nor desire for anything, but joined the Freemasons merely to associate with the wealthy young Brothers who were influential through their connections or rank, and of whom there were very many in the lodge.Pierre began to feel dissatisfied with what he was doing. Freemasonry, at any rate as he saw it here, sometimes seemed to him based merely on externals. He did not think of doubting Freemasonry itself, but suspected that Russian Masonry had taken a wrong path and deviated from its original principles. And so toward the end of the year he went abroad to be initiated into the higher secrets of the order.What is to be done in these circumstances? To favor revolutions, overthrow everything, repel force by force?No! We are very far from that. Every violent reform deserves censure, for it quite fails to remedy evil while men remain what they are, and also because wisdom needs no violence. "But what is there in running across it like that?" said Ilagin's groom. "Once she had missed it and turned it away, any mongrel could take it," Ilagin was saying at the same time, breathless from his gallop and his excitement. """ 
>>> answer = """In the third category he included those Brothers (the majority) who saw nothing in Freemasonry but the external forms and ceremonies, and prized the strict performance of these forms without troubling about their purport or significance. 
... Such were Willarski and even the Grand Master of the principal lodge. 
... Finally, to the fourth category also a great many Brothers belonged, particularly those who had lately joined. 
... These according to Pierre's observations were men who had no belief in anything, nor desire for anything, but joined the Freemasons merely to associate with the wealthy young Brothers who were influential through their connections or rank, and of whom there were very many in the lodge. 
... Pierre began to feel dissatisfied with what he was doing. 
... Freemasonry, at any rate as he saw it here, sometimes seemed to him based merely on externals. 
... He did not think of doubting Freemasonry itself, but suspected that Russian Masonry had taken a wrong path and deviated from its original principles. 
... And so toward the end of the year he went abroad to be initiated into the higher secrets of the order. 
... What is to be done in these circumstances? 
... To favor revolutions, overthrow everything, repel force by force? 
... No! 
... We are very far from that. 
... Every violent reform deserves censure, for it quite fails to remedy evil while men remain what they are, and also because wisdom needs no violence. 
... "But what is there in running across it like that?" said Ilagin's groom. 
... "Once she had missed it and turned it away, any mongrel could take it," Ilagin was saying at the same time, breathless from his gallop and his excitement.""" 
>>> 
>>> output = text.split('\n') 
>>> sum(1 for sent in text.split('\n') if sent in answer) 
0 
+0

ग्रेट उत्तर। मुझे कुछ सरल मानक शामिल करना पसंद आया। – erewok

+0

मुझे लगता है कि प्रश्न वाक्य विभाजन को संदर्भित करता है, शब्द टोकनिसेशन नहीं। – lenz

+0

लेंस, यह अभी भी एक बहुत अच्छा जवाब है – wakamdr

संबंधित मुद्दे