2012-04-03 119 views
-4

在python中,我該如何開發此算法來查找數組中文本的常見模式,並說這些是這些項目出現的次數。python查找數組中的所有常見模式

For ex: 
line_arr=""" :)hello hi there,My name is 'pixel' can i speak to 'Tom' 
Hi, tom here :) 'pixel' 
how are u doing today. 
i just called to ask whats the cost of the microwae oven is it $50 or $60 
it is $75 
any d $iscounts on this.. 
10% to 30%""" 

reg_dict={} 
for l in line_arr: 
    #find all common patterns and update it in an dictionary 

我們可以得到所有的表情,在單引號的名字,currecncy開始$和percentages..Also如果有更多的共同things.and說,我們在這dictionary..Is可能在所有更新這個..

+0

它的一個非常複雜的程序,所以我沒有提及它..... – Rajeev 2012-04-03 11:40:32

回答

3

你有什麼是一個字符串,而不是一個數組。你應該首先標記它。如果你想找到表情,使用不同的標記生成器比我上面使用的RE \w+

>>> from collections import Counter 
>>> import re 
>>> Counter(re.findall("\w+", line_arr)).most_common()[:5] 
[('is', 3), ('to', 3), ('pixel', 2), ('it', 2), ('i', 2)] 

:一旦你做到了這一點,你可以使用collections.Counter.most_common

+0

很酷的感謝..我可以從這裏進一步... – Rajeev 2012-04-03 12:32:05