My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/lepl/_performance/nat_lang.py
c400d8380bb4 vs. 249af3234ee4 Compare: vs.  Format:
Revision 249af3234ee4
Go to: 
Project members, sign in to write a code review
/src/lepl/_performance/nat_lang.py   c400d8380bb4 /src/lepl/_performance/nat_lang.py   249af3234ee4
1 1
2 # The contents of this file are subject to the Mozilla Public License 2 # The contents of this file are subject to the Mozilla Public License
3 # (MPL) Version 1.1 (the "License"); you may not use this file except 3 # (MPL) Version 1.1 (the "License"); you may not use this file except
4 # in compliance with the License. You may obtain a copy of the License 4 # in compliance with the License. You may obtain a copy of the License
5 # at http://www.mozilla.org/MPL/ 5 # at http://www.mozilla.org/MPL/
6 # 6 #
7 # Software distributed under the License is distributed on an "AS IS" 7 # Software distributed under the License is distributed on an "AS IS"
8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 # the License for the specific language governing rights and 9 # the License for the specific language governing rights and
10 # limitations under the License. 10 # limitations under the License.
11 # 11 #
12 # The Original Code is LEPL (http://www.acooke.org/lepl) 12 # The Original Code is LEPL (http://www.acooke.org/lepl)
13 # The Initial Developer of the Original Code is Andrew Cooke. 13 # The Initial Developer of the Original Code is Andrew Cooke.
14 # Portions created by the Initial Developer are Copyright (C) 2009-2010 14 # Portions created by the Initial Developer are Copyright (C) 2009-2010
15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved.
16 # 16 #
17 # Alternatively, the contents of this file may be used under the terms 17 # Alternatively, the contents of this file may be used under the terms
18 # of the LGPL license (the GNU Lesser General Public License, 18 # of the LGPL license (the GNU Lesser General Public License,
19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions
20 # of the LGPL License are applicable instead of those above. 20 # of the LGPL License are applicable instead of those above.
21 # 21 #
22 # If you wish to allow use of your version of this file only under the 22 # If you wish to allow use of your version of this file only under the
23 # terms of the LGPL License and not to allow others to use your version 23 # terms of the LGPL License and not to allow others to use your version
24 # of this file under the MPL, indicate your decision by deleting the 24 # of this file under the MPL, indicate your decision by deleting the
25 # provisions above and replace them with the notice and other provisions 25 # provisions above and replace them with the notice and other provisions
26 # required by the LGPL License. If you do not delete the provisions 26 # required by the LGPL License. If you do not delete the provisions
27 # above, a recipient may use your version of this file under either the 27 # above, a recipient may use your version of this file under either the
28 # MPL or the LGPL License. 28 # MPL or the LGPL License.
29 29
30 ''' 30 '''
31 Performance related tests. 31 Performance related tests.
32 ''' 32 '''
33 33
34 # pylint: disable-msg=C0103, C0111, C0301, W0702, C0324, C0102, C0321, W0141, W0614, W0401, R0914, R0903 34 # pylint: disable-msg=C0103, C0111, C0301, W0702, C0324, C0102, C0321, W0141, W0614, W0401, R0914, R0903
35 #@PydevCodeAnalysisIgnore 35 #@PydevCodeAnalysisIgnore
36 36
37 37
38 from logging import basicConfig, DEBUG, ERROR 38 from logging import basicConfig, DEBUG, ERROR
39 39
40 from lepl import * 40 from lepl import *
41 from lepl.core.trace import StreamMonitor 41 from lepl.core.trace import StreamMonitor
42 42
43 43
44 def natural_language(): 44 def natural_language():
45 ''' 45 '''
46 This focuses on the LMemo cache. It does not use any monitor or stream. 46 This focuses on the LMemo cache. It does not use any monitor or stream.
47 ''' 47 '''
48 48
49 #basicConfig(level=DEBUG) 49 #basicConfig(level=DEBUG)
50 basicConfig(level=ERROR) 50 basicConfig(level=ERROR)
51 51
52 class VerbPhrase(Node): pass 52 class VerbPhrase(Node): pass
53 class DetPhrase(Node): pass 53 class DetPhrase(Node): pass
54 class SimpleTp(Node): pass 54 class SimpleTp(Node): pass
55 class TermPhrase(Node): pass 55 class TermPhrase(Node): pass
56 class Sentence(Node): pass 56 class Sentence(Node): pass
57 57
58 verb = Literals('knows', 'respects', 'loves') > 'verb' 58 verb = Literals('knows', 'respects', 'loves') > 'verb'
59 join = Literals('and', 'or') > 'join' 59 join = Literals('and', 'or') > 'join'
60 proper_noun = Literals('helen', 'john', 'pat') > 'proper_noun' 60 proper_noun = Literals('helen', 'john', 'pat') > 'proper_noun'
61 determiner = Literals('every', 'some') > 'determiner' 61 determiner = Literals('every', 'some') > 'determiner'
62 noun = Literals('boy', 'girl', 'man', 'woman') > 'noun' 62 noun = Literals('boy', 'girl', 'man', 'woman') > 'noun'
63 63
64 verbphrase = Delayed() 64 verbphrase = Delayed()
65 verbphrase += verb | (verbphrase // join // verbphrase) > VerbPhrase 65 verbphrase += verb | (verbphrase // join // verbphrase) > VerbPhrase
66 det_phrase = determiner // noun > DetPhrase 66 det_phrase = determiner // noun > DetPhrase
67 simple_tp = proper_noun | det_phrase > SimpleTp 67 simple_tp = proper_noun | det_phrase > SimpleTp
68 termphrase = Delayed() 68 termphrase = Delayed()
69 termphrase += simple_tp | (termphrase // join // termphrase) > TermPhrase 69 termphrase += simple_tp | (termphrase // join // termphrase) > TermPhrase
70 sentence = termphrase // verbphrase // termphrase & Eos() > Sentence 70 sentence = termphrase // verbphrase // termphrase & Eos() > Sentence
71 71
72 #sentence.config.auto_memoize().no_full_first_match() 72 #sentence.config.auto_memoize().no_full_first_match()
73 #sentence.config.add_monitor(StreamMonitor)
74 p = sentence.get_match_string() 73 p = sentence.get_match_string()
75 print(repr(p.matcher)) 74 print(repr(p.matcher))
76 for _i in range(100): 75 for _i in range(100):
77 #for _i in range(1): 76 #for _i in range(1):
78 assert len(list(p('every boy or some girl and helen and john or pat knows ' 77 assert len(list(p('every boy or some girl and helen and john or pat knows '
79 'and respects or loves every boy or some girl and pat or ' 78 'and respects or loves every boy or some girl and pat or '
80 'john and helen'))) == 392 79 'john and helen'))) == 392
81 80
82 81
83 def natural_language2(): 82 def natural_language2():
84 ''' 83 '''
85 This focuses on the LMemo cache. It does not use any monitor or stream. 84 This focuses on the LMemo cache. It does not use any monitor or stream.
86 ''' 85 '''
87 86
88 #basicConfig(level=DEBUG) 87 #basicConfig(level=DEBUG)
89 basicConfig(level=ERROR) 88 basicConfig(level=ERROR)
90 89
91 class VerbPhrase(Node): pass 90 class VerbPhrase(Node): pass
92 class DetPhrase(Node): pass 91 class DetPhrase(Node): pass
93 class SimpleTp(Node): pass 92 class SimpleTp(Node): pass
94 class TermPhrase(Node): pass 93 class TermPhrase(Node): pass
95 class Sentence(Node): pass 94 class Sentence(Node): pass
96 95
97 t = Token('[a-z]+') 96 t = Token('[a-z]+')
98 97
99 verb = t(Literals('knows', 'respects', 'loves')) > 'verb' 98 verb = t(Literals('knows', 'respects', 'loves')) > 'verb'
100 join = t(Literals('and', 'or')) > 'join' 99 join = t(Literals('and', 'or')) > 'join'
101 proper_noun = t(Literals('helen', 'john', 'pat')) > 'proper_noun' 100 proper_noun = t(Literals('helen', 'john', 'pat')) > 'proper_noun'
102 determiner = t(Literals('every', 'some')) > 'determiner' 101 determiner = t(Literals('every', 'some')) > 'determiner'
103 noun = t(Literals('boy', 'girl', 'man', 'woman')) > 'noun' 102 noun = t(Literals('boy', 'girl', 'man', 'woman')) > 'noun'
104 103
105 verbphrase = Delayed() 104 verbphrase = Delayed()
106 verbphrase += verb | (verbphrase & join & verbphrase) > VerbPhrase 105 verbphrase += verb | (verbphrase & join & verbphrase) > VerbPhrase
107 det_phrase = determiner & noun > DetPhrase 106 det_phrase = determiner & noun > DetPhrase
108 simple_tp = proper_noun | det_phrase > SimpleTp 107 simple_tp = proper_noun | det_phrase > SimpleTp
109 termphrase = Delayed() 108 termphrase = Delayed()
110 termphrase += simple_tp | (termphrase & join & termphrase) > TermPhrase 109 termphrase += simple_tp | (termphrase & join & termphrase) > TermPhrase
111 sentence = termphrase & verbphrase & termphrase & Eos() > Sentence 110 sentence = termphrase & verbphrase & termphrase & Eos() > Sentence
112 111
113 #sentence.config.auto_memoize(full=True) 112 #sentence.config.auto_memoize(full=True)
114 #sentence.config.add_monitor(StreamMonitor)
115 p = sentence.get_match_string() 113 p = sentence.get_match_string()
116 print(repr(p.matcher)) 114 print(repr(p.matcher))
117 for _i in range(100): 115 for _i in range(100):
118 assert len(list(p('every boy or some girl and helen and john or pat knows ' 116 assert len(list(p('every boy or some girl and helen and john or pat knows '
119 'and respects or loves every boy or some girl and pat or ' 117 'and respects or loves every boy or some girl and pat or '
120 'john and helen'))) == 392 118 'john and helen'))) == 392
121 119
122 120
123 def time(): 121 def time():
124 from timeit import Timer 122 from timeit import Timer
125 t = Timer("natural_language2()", "from __main__ import natural_language2") 123 t = Timer("natural_language2()", "from __main__ import natural_language2")
126 print(t.timeit(number=1)) 124 print(t.timeit(number=1))
127 # 4v5, no special config, x100, python 3.2 on laptop 125 # 4v5, no special config, x100, python 3.2 on laptop
128 # string 31.9,32.0 v 19.3,19.1 126 # string 31.9,32.0 v 19.3,19.1
129 # tokens (2) 26.6,25.8 v 22.6,22.4 127 # tokens (2) 26.6,25.8 v 22.6,22.4
130 128
131 129
132 def profile(): 130 def profile():
133 ''' 131 '''
134 import pstats 132 import pstats
135 p=pstats.Stats('nat_lang.prof') 133 p=pstats.Stats('nat_lang.prof')
136 p.strip_dirs() 134 p.strip_dirs()
137 p.sort_stats('cumulative') 135 p.sort_stats('cumulative')
138 p.sort_stats('time') 136 p.sort_stats('time')
139 p.print_stats(35) 137 p.print_stats(35)
140 ''' 138 '''
141 import cProfile 139 import cProfile
142 cProfile.run('natural_language()', 'nat_lang.prof') 140 cProfile.run('natural_language()', 'nat_lang.prof')
143 141
144 if __name__ == '__main__': 142 if __name__ == '__main__':
145 time() 143 time()
146 # profile() 144 # profile()
147 # natural_language() 145 # natural_language()
Powered by Google Project Hosting