This repository was archived by the owner on Sep 24, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathAnalysis_text.py
More file actions
644 lines (501 loc) · 26.7 KB
/
Copy pathAnalysis_text.py
File metadata and controls
644 lines (501 loc) · 26.7 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
from datetime import datetime
from fileinput import close
from collections import Counter
time_data = datetime.now().strftime('%c') # переменная времени
time_data = time_data[-4:] + time_data[3:-4] # нужный мне формат
Mode_selection=''
while True:
if Mode_selection=='':
Mode_selection=input('''
Hello!!! I'm a text analyzer! I can help you with analysis of any text!
You can choose analysis of text from file or analysis of the entered text!
Please choose!!! :
If you want analysis of text from file. Write me : (1)
if you want analysis of the entered text. Write me : (2)
if you want finish the program. Write me : (0)''')
elif Mode_selection =='123':
print('\nInvalid input! Try again!')
Mode_selection = input('''
Please choose!!! :
If you want analysis of text from file. Write me : (1)
if you want analysis of the entered text. Write me : (2)
if you want finish the program. Write me : (0)''')
if Mode_selection == '1':
while True: # просим ввести имя файла и присваеваем его пееременной текст если файла нет просим ввести по новой
file=input('''
Write me name your file!
The file must be location in the program folder!
Input form for file name : (file_name.txt)''')
try:
with open(file, encoding='utf-8') as file_text:
text=file_text.read()
break
except FileNotFoundError:
print('''
\n\nI didn't find a file with this name!
Try entering the file name again!\n''')
text_length = len(text)
#количество символов в тексте с пробелами |
text_length_without_spaces_and_newline = len(text.replace(' ','')\
.replace('\n', ''))
#количество всех символов за исключенем переноса строки и пробела
spaces_in_text = text.count(' ')
#количество пробелов в строке
newline_in_text = text.count('\n')
#количество переносов строки
text = text.lower()
#приводим текст к нижнему регистру для адекватного анализа
i = 0
# переменая цыкла
text_letters = ''
#переменная для букв
text_words = ''
#переменная для слов
text_SSymbol = ''
# переменая для спец символов
text_digit = ''
#переменная для цыфр без потери смысла (тоесть 3.14 даты и тд)
text_digit_symbol = ''
# переменная для цыфр (для проверки сколько 1 или другой символ вст в тексте)
proposals_in_text = 0
# переменная для подсчета количества предложений
download1=0
average_length = 0
download2=0
print('\n')
name_file = input('Write me a name for the file name with text analysis!')
name_file = name_file.strip() + '.txt'
while len(text)>i:
# цыкл для распределения данных из текста
if text[i].isalpha():
# проверяем на букву если да добовляем к переменной буква слово
text_words += text[i]
text_letters += text[i]
if i>1 and text[i-1].isdigit() and(not text[i+1].isalpha() or not text[i+2].isalpha()):
text_digit += text[i]
if text[i].isdigit():
#проверка на цыфру если добовляем к переменным цыфрам
text_digit += text[i]
text_digit_symbol += text[i]
if text[i] == ' ' or text[i] == '\n':
# проверка на пробел\перенос если да добовляем ко всем спискам
text_words += text[i]
text_digit += text[i]
if text[i] == '\n':
proposals_in_text +=1
if i>=2 and not text[i].isalnum():
# проверка на спец символ добовляем к переменной спец символов
text_SSymbol += text[i] + ' '
if i>=2 and not i+1==len(text) and ((text[i-1].isdigit() or text[i+1].isdigit())
# проверка на то являеться спецсимвол важным для числа
and not text[i-1].isalpha()):
text_digit += text[i]
if text[i-1].isalpha() and not text[i-2].isalnum():
# проверка на анограмы
text_words += text[i]
else: text_words += ' '
# на всякий добовляем пробел после спец символа
if ((i>2 and (text[i] in ( '!', '.', '?', )))
and text[i-1].isalpha() and text[i-2].isalpha()
and not text[i+1].isalpha()):
# проверка на количество предложений
proposals_in_text += 1
download=100/len(text)
download1+=download
download2+=download
if download1>=1: #окно загрузки
print('█', end='')
download-=1
if download2>=(100-download):
print('\n' + ' ' *60 + 'Analise finish! \n ')
i += 1
print('\n')
text_words = text_words.split()
#переводим к списку с сохранением структуры и смысла
text_digit = text_digit.split()
#переводим к списку с сохранением структуры и смысла
text_letters = list(text_letters)
#приводим к списку для анализа каждой буквы отдельно
text_SSymbol = text_SSymbol.split()
#переводим к списку с сохранением структуры и смысла
text_digit_symbol = list(text_digit_symbol)
# приводи к списку для анализа каждой цыфры отдельно
number_of_text_digit = len(text_digit)
# узнаем количество цыфр
number_of_text_words = len(text_words)
# узнаем количество слов
number_of_text_SSymbol = len(text_SSymbol)
# узнаем количество символов
number_of_text_letters = len(text_letters)
# узнаем количество букв
number_of_text_digit_symbol = len(text_digit_symbol)
# узнаем количество цыфр отдельно каждую
unique_SSymbols = len(set(text_SSymbol))
# узнаем количество унткальных симвалов
unique_words = len(set(text_words))
# узнаем количество уникальных слов
unique_digits = len(set(text_digit))
# узнаем количество уникальных цыфр целых
unique_digits_symbol = len(set(text_digit_symbol))
# узнаем количество уникальных цыфр по одной
unique_litters = len(set(text_letters))
# узнаем количество уникальных букв
if len(text_digit)>0:
average_length = round(number_of_text_letters
/
number_of_text_words, 3)
#узнаем средную длину слова и округляем до 3 знаков
counter_word = Counter(text_words)
# делаем словаоь для выявления чяястотности слов
counter_digit = Counter(text_digit)
# делаем словаоь для выявления чяястотности цыфр целых
counter_SSymbol = Counter(text_SSymbol)
# делаем словаоь для выявления чяястотности символов
counter_letters = Counter(text_letters)
# делаем словаоь для выявления чяястотности букв
counter_digit_symbol=Counter(text_digit_symbol)
# делаем словаоь для выявления чяястотности цыфр по отдельности
max_min_number_word_in_text = sorted(set(text_words), key=len)
# сортруем словарь чтобы найти самое длинное короткое слово 5 тех и 5 тех
d=max_min_number_word_in_text # создаю псевдоним
words_list = list(counter_word.items())
# делаем список из слов
words_list.sort(key=len)
digits_list = list(counter_digit.items())
# делаем список из цифр
digits_list.sort(key=len)
letters_list = list(counter_letters.items())
# делаем список из букв
letters_list.sort()
SSymbol_list = list(counter_SSymbol.items())
# делаем список из спец символов
digits_symbol_list = list(counter_digit_symbol.items())
# делаем список из цыфр по символьно
digits_symbol_list.sort()
range_len = max(len(words_list), len(digits_list),
len(letters_list), len(SSymbol_list),
len(digits_symbol_list))
#узнаем максимальную длинну среди всех списков
t=len("Analysis text from file : ")+len(file)+4
t2=len("Text analysis saved in file : ")+len(name_file)+4
if t%2 == 1:
t+=1
if t2%2 ==1:
t2+=1
if t2>t:
t=t2
# для графической подгонки и формирования таблицы даты и названия файла
if len(d)<5:
r=5-len(d)
d+=['-',]*r
# для ситуаций с маленьким текстом
text_for_file=f'''
{'*'*t:^140}
{"Analysis text from file : "+file:^142}
{"Text analysis saved in file : " + name_file:^142}
{'Data & time : '+time_data:^142}
{'*'*t:^140}
{'_'*142}
|{'Analysis Report':^140}|
{'_'*142}
| Total number of characters | {f'{text_length} pcs':<102}|
| Without spaces and newlines | {f'{text_length_without_spaces_and_newline} pcs':<102}|
| Number of spaces | {f'{spaces_in_text} pcs':<102}|
| Number of newlines | {f'{newline_in_text} pcs':<102}|
| Number of sentences | {f'{proposals_in_text} pcs':<102}|
| Average word length | {f'{average_length} pcs':<102}|
|{'-'*36}|{'-'*103}|
| 5 shortest words | {d[0]+', '+d[1]+', '+d[2]+',':<102}|
| | {d[3]+', '+d[4]+'.':<102}|
|{'-'*36}|{'-'*103}|
| 5 longest words | {d[-1]+', '+d[-2]+',':<102}|
| | {d[-3]+', '+d[-4]+', '+ d[-5]+'.':<102}|
|{'-'*36}|{'-'*103}|
| Letters in text | {f'{number_of_text_letters} pcs':<102}|
| Unique letters | {f'{unique_litters} pcs':<102}|
|{'-'*36}|{'-'*103}|
| Words in text | {f'{number_of_text_words} pcs':<102}|
| Unique words | {f'{unique_words} pcs':<102}|
|{'-'*36}|{'-'*103}|
| Numbers in text | {f'{number_of_text_digit} pcs':<102}|
| Unique numbers | {f'{unique_digits} pcs':<102}|
|{'-'*36}|{'-'*103}|
| Individual digits | {f'{number_of_text_digit_symbol} pcs':<102}|
| Unique individual digits | {f'{unique_digits_symbol} pcs':<102}|
|{'-'*36}|{'-'*103}|
| Special symbols in text | {f'{number_of_text_SSymbol} pcs':<102}|
| Unique special symbols | {f'{unique_SSymbols} pcs':<102}|
{'='*142}
{'-'*142}
|{'Text frequency analysis table':^140}|
{'-'*142}
|{'Word':^23}|{'Count':^12}| |{'letters':^9}|{'Count':^12}| |{'SSymbol':^9}|{'Count':^12}| |{'SymDigit':^10}|{'Count':^12}| |{'Digit':^12}|{'Count':^12}|
{'-'*142}
'''
for i in range(range_len):
if len(words_list)>i:
w=words_list[i]
else:
w=('-', '0')
if len(digits_list) > i:
d = digits_list[i]
else:
d = ('-', '0')
if len(letters_list) > i:
l = letters_list[i]
else:
l = ('-', '0')
if len(SSymbol_list) > i:
SS = SSymbol_list[i]
else:
SS = ('-', '0')
if len(digits_symbol_list) > i:
ds = digits_symbol_list[i]
else:
ds = ('-', '0')
text_for_file+=\
f'|{w[0]:^23}|{f'{w[1]} pcs':^12}| |{l[0]:^9}|{f'{l[1]} pcs':^12}| |{SS[0]:^9}|{f'{SS[1]} pcs':^12}| |{ds[0]:^10}|{f'{ds[1]} pcs':^12}| |{d[0]:^12}|{f'{d[1]} pcs':^12}|\n'
text_for_file+=f'{'-'*142}'
with open(name_file, 'a+', encoding='utf-8') as file_text:
file_text.write('\n'*5+text_for_file)
print(text_for_file)
Mode_selection = input('''
The program has finished analyzing the text!!!
If you want finish the program Write me : (0)
If you want a new text analysis Write me : (1)''')
if not Mode_selection == 0 and Mode_selection == 1:
Mode_selection='123'
continue
elif Mode_selection == '2':
text = input('Write me your text!!!')
text_length = len(text)
# количество символов в тексте с пробелами |
text_length_without_spaces_and_newline = len(text.replace(' ', '') \
.replace('\n', ''))
# количество всех символов за исключенем переноса строки и пробела
spaces_in_text = text.count(' ')
# количество пробелов в строке
newline_in_text = text.count('\n')
# количество переносов строки
text = text.lower()
# приводим текст к нижнему регистру для адекватного анализа
i = 0
# переменая цыкла
text_letters = ''
# переменная для букв
text_words = ''
# переменная для слов
text_SSymbol = ''
# переменая для спец символов
text_digit = ''
# переменная для цыфр без потери смысла (тоесть 3.14 даты и тд)
text_digit_symbol = ''
# переменная для цыфр (для проверки сколько 1 или другой символ вст в тексте)
proposals_in_text = 0
# переменная для подсчета количества предложений
download1 = 0
download2 = 0
print('\n')
name_file = input('Write me a name for the file name with text analysis!')
name_file = name_file.strip() + '.txt'
while len(text) > i:
# цыкл для распределения данных из текста
if text[i].isalpha():
# проверяем на букву если да добовляем к переменной буква слово
text_words += text[i]
text_letters += text[i]
if i > 1 and text[i - 1].isdigit() and (not text[i + 1].isalpha() or not text[i + 2].isalpha()):
text_digit += text[i]
if text[i].isdigit():
# проверка на цыфру если добовляем к переменным цыфрам
text_digit += text[i]
text_digit_symbol += text[i]
if text[i] == ' ' or text[i] == '\n':
# проверка на пробел\перенос если да добовляем ко всем спискам
text_words += text[i]
text_digit += text[i]
if text[i] == '\n':
proposals_in_text += 1
if i >= 2 and not text[i].isalnum():
# проверка на спец символ добовляем к переменной спец символов
text_SSymbol += text[i] + ' '
if i >= 2 and not i + 1 == len(text) and ((text[i - 1].isdigit() or text[i + 1].isdigit())
# проверка на то являеться спецсимвол важным для числа
and not text[i - 1].isalpha()):
text_digit += text[i]
if text[i - 1].isalpha() and not text[i - 2].isalnum():
# проверка на анограмы
text_words += text[i]
else:
text_words += ' '
# на всякий добовляем пробел после спец символа
if ((i > 2 and (text[i] in ('!', '.', '?',)))
and text[i - 1].isalpha() and text[i - 2].isalpha()
and not text[i + 1].isalpha()):
# проверка на количество предложений
proposals_in_text += 1
download = 100 / len(text)
download1 += download
download2 += download
if download1 >= 1: # окно загрузки
print('█', end='')
download -= 1
if download2 >= (100 - download):
print('\n' + ' ' * 60 + 'Analise finish! \n ')
i += 1
print('\n')
text_words = text_words.split()
# переводим к списку с сохранением структуры и смысла
text_digit = text_digit.split()
# переводим к списку с сохранением структуры и смысла
text_letters = list(text_letters)
# приводим к списку для анализа каждой буквы отдельно
text_SSymbol = text_SSymbol.split()
# переводим к списку с сохранением структуры и смысла
text_digit_symbol = list(text_digit_symbol)
# приводи к списку для анализа каждой цыфры отдельно
number_of_text_digit = len(text_digit)
# узнаем количество цыфр
number_of_text_words = len(text_words)
# узнаем количество слов
number_of_text_SSymbol = len(text_SSymbol)
# узнаем количество символов
number_of_text_letters = len(text_letters)
# узнаем количество букв
number_of_text_digit_symbol = len(text_digit_symbol)
# узнаем количество цыфр отдельно каждую
unique_SSymbols = len(set(text_SSymbol))
# узнаем количество унткальных симвалов
unique_words = len(set(text_words))
# узнаем количество уникальных слов
unique_digits = len(set(text_digit))
# узнаем количество уникальных цыфр целых
unique_digits_symbol = len(set(text_digit_symbol))
# узнаем количество уникальных цыфр по одной
unique_litters = len(set(text_letters))
# узнаем количество уникальных букв
average_length=0
if len(text_digit) > 0:
average_length = round(number_of_text_letters
/
number_of_text_words, 3)
# узнаем средную длину слова и округляем до 3 знаков
counter_word = Counter(text_words)
# делаем словаоь для выявления чяястотности слов
counter_digit = Counter(text_digit)
# делаем словаоь для выявления чяястотности цыфр целых
counter_SSymbol = Counter(text_SSymbol)
# делаем словаоь для выявления чяястотности символов
counter_letters = Counter(text_letters)
# делаем словаоь для выявления чяястотности букв
counter_digit_symbol = Counter(text_digit_symbol)
# делаем словаоь для выявления чяястотности цыфр по отдельности
max_min_number_word_in_text = sorted(set(text_words), key=len)
# сортруем словарь чтобы найти самое длинное короткое слово 5 тех и 5 тех
d = max_min_number_word_in_text # создаю псевдоним
words_list = list(counter_word.items())
# делаем список из слов
words_list.sort(key=len)
digits_list = list(counter_digit.items())
# делаем список из цифр
digits_list.sort(key=len)
letters_list = list(counter_letters.items())
# делаем список из букв
letters_list.sort()
SSymbol_list = list(counter_SSymbol.items())
# делаем список из спец символов
digits_symbol_list = list(counter_digit_symbol.items())
# делаем список из цыфр по символьно
digits_symbol_list.sort()
range_len = max(len(words_list), len(digits_list),
len(letters_list), len(SSymbol_list),
len(digits_symbol_list))
# узнаем максимальную длинну среди всех списков
t = len("Text analysis saved in file : ") + len(name_file) + 4
if t % 2 == 1:
t += 1
# для графической подгонки и формирования таблицы даты и названия файла
if len(d)<5:
r=5-len(d)
d+=['-',]*r
text_for_file = f'''
{'*' * t:^140}
{"Text analysis saved in file : " + name_file:^142}
{'Data & time : ' + time_data:^142}
{'*' * t:^140}
{'_' * 142}
|{'Analysis Report':^140}|
{'_' * 142}
| Total number of characters | {f'{text_length} pcs':<102}|
| Without spaces and newlines | {f'{text_length_without_spaces_and_newline} pcs':<102}|
| Number of spaces | {f'{spaces_in_text} pcs':<102}|
| Number of newlines | {f'{newline_in_text} pcs':<102}|
| Number of sentences | {f'{proposals_in_text} pcs':<102}|
| Average word length | {f'{average_length} pcs':<102}|
|{'-' * 36}|{'-' * 103}|
| 5 shortest words | {d[0] + ', ' + d[1] + ', ' + d[2] + ',':<102}|
| | {d[3] + ', ' + d[4]+'.':<102}|
|{'-' * 36}|{'-' * 103}|
| 5 longest words | {d[-1] + ', ' + d[-2]+',':<102}|
| | {d[-3] + ', ' + d[-4] + ', ' + d[-5]+'.':<102}|
|{'-' * 36}|{'-' * 103}|
| Letters in text | {f'{number_of_text_letters} pcs':<102}|
| Unique letters | {f'{unique_litters} pcs':<102}|
|{'-' * 36}|{'-' * 103}|
| Words in text | {f'{number_of_text_words} pcs':<102}|
| Unique words | {f'{unique_words} pcs':<102}|
|{'-' * 36}|{'-' * 103}|
| Numbers in text | {f'{number_of_text_digit} pcs':<102}|
| Unique numbers | {f'{unique_digits} pcs':<102}|
|{'-' * 36}|{'-' * 103}|
| Individual digits | {f'{number_of_text_digit_symbol} pcs':<102}|
| Unique individual digits | {f'{unique_digits_symbol} pcs':<102}|
|{'-' * 36}|{'-' * 103}|
| Special symbols in text | {f'{number_of_text_SSymbol} pcs':<102}|
| Unique special symbols | {f'{unique_SSymbols} pcs':<102}|
{'=' * 142}
{'-' * 142}
|{'Text frequency analysis table':^140}|
{'-' * 142}
|{'Word':^23}|{'Count':^12}| |{'letters':^9}|{'Count':^12}| |{'SSymbol':^9}|{'Count':^12}| |{'SymDigit':^10}|{'Count':^12}| |{'Digit':^12}|{'Count':^12}|
{'-' * 142}
'''
for i in range(range_len):
if len(words_list) > i:
w = words_list[i]
else:
w = ('-', '0')
if len(digits_list) > i:
d = digits_list[i]
else:
d = ('-', '0')
if len(letters_list) > i:
l = letters_list[i]
else:
l = ('-', '0')
if len(SSymbol_list) > i:
SS = SSymbol_list[i]
else:
SS = ('-', '0')
if len(digits_symbol_list) > i:
ds = digits_symbol_list[i]
else:
ds = ('-', '0')
text_for_file += \
f'|{w[0]:^23}|{f'{w[1]} pcs':^12}| |{l[0]:^9}|{f'{l[1]} pcs':^12}| |{SS[0]:^9}|{f'{SS[1]} pcs':^12}| |{ds[0]:^10}|{f'{ds[1]} pcs':^12}| |{d[0]:^12}|{f'{d[1]} pcs':^12}|\n'
text_for_file += f'{'-' * 142}'
with open(name_file, 'a+', encoding='utf-8') as file_text:
file_text.write('\n'*5+text_for_file)
print(text_for_file)
Mode_selection = input('''
The program has finished analyzing the text!!!
If you want finish the program Write me : (0)
If you want a new text analysis Write me : (1)''')
if not Mode_selection == 0 and Mode_selection == 1:
Mode_selection = '123'
continue
elif Mode_selection == '0':
print('Program finish! Have Good day')
break
else:
Mode_selection='123'