Используя образец файла Lorem Ipsum , я сравнил вашу функцию со встроенной collections.Counter
:
In [1]: from collections import Counter
In [2]: def method1(corpus_text):
...: bag_of_words = " ".join(corpus_text).split()
...: tokens = set(bag_of_words)
...: words_frequency = {}
...: for doc in corpus_text:
...: text = doc.split()
...: for word in tokens:
...: if word in text:
...: if word in words_frequency.keys():
...: words_frequency[word] += 1
...: else:
...: words_frequency[word] = 1
...: return words_frequency
In [3]: def method2(corpus_text):
...: bag_of_words = " ".join(corpus_text).split()
...: tokens = set(bag_of_words)
...: return {
...: token: sum(
...: map(lambda doc: 1 if token in doc.split() else 0, corpus_text))
...: for token in tokens
...: }
In [4]: with open("Latin-Lipsum.txt") as file:
...: text = file.read()
In [5]: method1(text)
Out[5]:
{'L': 1,
'o': 63,
'r': 72,
...
'O': 1,
'P': 1,
'j': 1}
In [6]: Counter(text)
Out[6]:
Counter({'L': 1,
'o': 63,
'r': 72,
...
'O': 1,
'P': 1,
'j': 1})
Они работают почти одинаково, за исключением того, чтоCounter
также подсчитывает \n
символов в этом случае.
Хотя с точки зрения скорости, Counter
намного быстрее:
In [7]: %timeit method1(text)
1.96 ms ± 32.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [8]: %timeit method2(text)
10.5 ms ± 19.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [9]: %timeit Counter(text)
43.8 µs ± 50.4 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Ваш второй метод действительно медленнее, чемваш первый метод, и Counter
намного быстрее, чем оба.