forked from p46318075/CodePattern
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.
36 lines
916 B
36 lines
916 B
9 months ago
|
from cppy.cp_util import *
|
||
|
from collections import Counter
|
||
|
|
||
|
class Pipe:
|
||
|
def __init__(self, func, *args, kwargs=None):
|
||
|
self.func = func
|
||
|
# self.args, self.kwargs= None ,None
|
||
|
if args : self.args = args
|
||
|
if kwargs: self.kwargs = kwargs
|
||
|
# print( self.args, self.kwargs)
|
||
|
|
||
|
def __or__(self, other):
|
||
|
return other(self._value)
|
||
|
|
||
|
def __call__(self, data):
|
||
|
self._value = self.func(data, *self.args, self.kwargs)
|
||
|
|
||
|
def read_file(filename):
|
||
|
with open(filename, 'r') 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(extract_file_words) | Pipe(get_frequencies) | Pipe(sort_dict) | Pipe(print_word_freqs, 5)
|
||
|
result = pipe(testfilepath)
|
||
|
print(result)
|