You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.3 KiB
42 lines
1.3 KiB
from collections import Counter
|
|
from cppy.cp_util import *
|
|
import re
|
|
|
|
class Pipe:
|
|
def __init__(self, func, *args, **kwargs):
|
|
# print( self, func, *args, **kwargs )
|
|
self.func = func
|
|
self.args = args
|
|
self.kwargs = kwargs
|
|
|
|
def __or__(self, other):
|
|
return Pipe(lambda x: self.func(x) or other.func(x))
|
|
# print(self.func.__name__, other.func.__name__ )
|
|
def composed_func():
|
|
print( other.func(self.func(self.args, self.kwargs), other.args, other.kwargs) )
|
|
return other.func(self.func(self.args, self.kwargs), other.args, other.kwargs)
|
|
return Pipe(composed_func)
|
|
|
|
# def __call__(self, *args, **kwargs):
|
|
# print( *args, **kwargs )
|
|
# return self.func(*args, **kwargs)
|
|
def __call__(self, data):
|
|
return self.func(data)
|
|
|
|
def read_file(filename):
|
|
with open(filename, 'r',encoding='utf-8') as f:
|
|
return f.read()
|
|
|
|
def split_words(text):
|
|
return re.findall(r'\b\w+\b', text.lower())
|
|
|
|
def count_words(words):
|
|
return Counter(words)
|
|
|
|
def top_n_words(word_counts, n):
|
|
return word_counts.most_common(n)
|
|
|
|
# 使用管道
|
|
pipe = Pipe(read_file) | Pipe(split_words) | Pipe(count_words) | Pipe(top_n_words, 10)
|
|
result = pipe(testfilepath)
|
|
print(result) |