forked from lizaku/Programming-and-computer-instruments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_practice.py
45 lines (37 loc) · 1.32 KB
/
debug_practice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Программа должна читать текст из файла и сообщать,
# есть ли в тексте хотя бы одна биграмма (два слова, стоящих в тексте друг за другом),
# встречающаяся больше двух раз. Регистр и знаки препинания нужно не учитывать.
# В программе хотя бы один раз нужно использовать list comprehensions.
import codecs
def open_file(file_name):
f = codecs.open(file_name, 'r', 'utf-8')
words = []
for line in f:
line = line.strip()
words += line.split()
for word in words:
word = word.strip('.,!?:;()\'\"1234567890')
word = word.lower()
return words
def bigramms(words):
# bi = [words[i]+words[i+1] for i in range(len(words)) if i<len(words)]
bi = create_list(words)
dic = {}
for j in bi:
if j not in dic:
dic[j] = 1
else:
dic[j] += 1
answer = ''
answer = [n+'\r\n' for n in dic]
print(answer)
return answer
def create_list(words):
bi = []
for i in range(len(words)):
if i < len(words):
j = i+1
bi.append(words[i] + words[j])
return bi
words = open_file('text.txt')
bigramms(words)