Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
11
Task/Word-frequency/Python/word-frequency-1.py
Normal file
11
Task/Word-frequency/Python/word-frequency-1.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import collections
|
||||
import re
|
||||
import string
|
||||
import sys
|
||||
|
||||
def main():
|
||||
counter = collections.Counter(re.findall(r"\w+",open(sys.argv[1]).read().lower()))
|
||||
print counter.most_common(int(sys.argv[2]))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
20
Task/Word-frequency/Python/word-frequency-2.py
Normal file
20
Task/Word-frequency/Python/word-frequency-2.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
from collections import Counter
|
||||
from re import findall
|
||||
|
||||
les_mis_file = 'les_mis_135-0.txt'
|
||||
|
||||
def _count_words(fname):
|
||||
with open(fname) as f:
|
||||
text = f.read()
|
||||
words = findall(r'\w+', text.lower())
|
||||
return Counter(words)
|
||||
|
||||
def most_common_words_in_file(fname, n):
|
||||
counts = _count_words(fname)
|
||||
for word, count in [['WORD', 'COUNT']] + counts.most_common(n):
|
||||
print(f'{word:>10} {count:>6}')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
n = int(input('How many?: '))
|
||||
most_common_words_in_file(les_mis_file, n)
|
||||
47
Task/Word-frequency/Python/word-frequency-3.py
Normal file
47
Task/Word-frequency/Python/word-frequency-3.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
"""
|
||||
Word count task from Rosetta Code
|
||||
http://www.rosettacode.org/wiki/Word_count#Python
|
||||
"""
|
||||
from itertools import (groupby,
|
||||
starmap)
|
||||
from operator import itemgetter
|
||||
from pathlib import Path
|
||||
from typing import (Iterable,
|
||||
List,
|
||||
Tuple)
|
||||
|
||||
|
||||
FILEPATH = Path('lesMiserables.txt')
|
||||
COUNT = 10
|
||||
|
||||
|
||||
def main():
|
||||
words_and_counts = most_frequent_words(FILEPATH)
|
||||
print(*words_and_counts[:COUNT], sep='\n')
|
||||
|
||||
|
||||
def most_frequent_words(filepath: Path,
|
||||
*,
|
||||
encoding: str = 'utf-8') -> List[Tuple[str, int]]:
|
||||
"""
|
||||
A list of word-frequency pairs sorted by their occurrences.
|
||||
The words are read from the given file.
|
||||
"""
|
||||
def word_and_frequency(word: str,
|
||||
words_group: Iterable[str]) -> Tuple[str, int]:
|
||||
return word, capacity(words_group)
|
||||
|
||||
file_contents = filepath.read_text(encoding=encoding)
|
||||
words = file_contents.lower().split()
|
||||
grouped_words = groupby(sorted(words))
|
||||
words_and_frequencies = starmap(word_and_frequency, grouped_words)
|
||||
return sorted(words_and_frequencies, key=itemgetter(1), reverse=True)
|
||||
|
||||
|
||||
def capacity(iterable: Iterable) -> int:
|
||||
"""Returns a number of elements in an iterable"""
|
||||
return sum(1 for _ in iterable)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
19
Task/Word-frequency/Python/word-frequency-4.py
Normal file
19
Task/Word-frequency/Python/word-frequency-4.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#!/usr/bin/python3
|
||||
import collections
|
||||
import re
|
||||
|
||||
count = 10
|
||||
|
||||
with open("135-0.txt") as f:
|
||||
text = f.read()
|
||||
|
||||
word_freq = sorted(
|
||||
collections.Counter(sorted(re.split(r"\W+", text.lower()))).items(),
|
||||
key=lambda c: c[1],
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
for i in range(len(word_freq)):
|
||||
print("[{:2d}] {:>10} : {}".format(i + 1, word_freq[i][0], word_freq[i][1]))
|
||||
if i == count - 1:
|
||||
break
|
||||
Loading…
Add table
Add a link
Reference in a new issue