2012-04-03 3 views
-4

파이썬에서는 어떻게 배열에있는 텍스트에서 일반적인 패턴을 찾을 수있는 알고리즘을 개발하고 이러한 항목의 발생 횟수를 말할 수 있습니다.파이썬 배열의 모든 일반적인 패턴을 찾으십시오

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 더 이상 일반적인 일들도 우리가 전혀 dictionary..Is이 가능한이 업데이트 말한다면 ..

+0

매우 복잡한 프로그램이므로 ..... ..... – Rajeev

답변

3

문자열은 배열이 아니라 배열입니다. 먼저 토큰 화해야합니다. 당신이 그 일을 한 후에는 collections.Counter.most_common를 사용할 수 있습니다

>>> 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)] 

을 당신이 스마일을 찾아 내가 위에서 사용 된 RE \w+ 아닌 다른 토큰 화를 사용합니다.

+0

멋진 감사합니다 ..i 여기에서 더 멀리 갈 수 있습니다 ..... – Rajeev

관련 문제