-
Notifications
You must be signed in to change notification settings - Fork 2
/
Algorithm_for_Text Processing Tool.py
59 lines (41 loc) · 1.47 KB
/
Algorithm_for_Text Processing Tool.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
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# coding: utf-8
# In[4]:
def read_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
def count_words(text):
words = text.split()
return len(words)
def count_sentences(text):
sentences = text.split('.')
return len(sentences) - 1 # Subtract 1 because the last split is usually empty
def calculate_word_frequency(text):
words = text.split()
frequency = {}
for word in words:
if word in frequency:
frequency[word] += 1
else:
frequency[word] = 1
return frequency
def replace_word(text, old_word, new_word):
return text.replace(old_word, new_word)
def process_text(file_path, old_word=None, new_word=None):
text = read_file(file_path)
word_count = count_words(text)
sentence_count = count_sentences(text)
word_frequency = calculate_word_frequency(text)
if old_word and new_word:
text = replace_word(text, old_word, new_word)
print(f'Word count: {word_count}')
print(f'Sentence count: {sentence_count}')
print('Word frequencies:')
for word, frequency in word_frequency.items():
print(f'{word}: {frequency}')
if old_word and new_word:
print(f'\nText after replacing "{old_word}" with "{new_word}":\n{text}')
# Example usage
file_path = file_path = 'C:\\example.txt' # Replace 'example.txt' with the path to your text file
process_text(file_path, 'oldWord', 'newWord')
# In[ ]: