-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (42 loc) · 1.47 KB
/
main.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
56
57
58
59
60
import os
import re
import requests
from bs4 import BeautifulSoup
def download_and_save_text(url, filename):
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
text = soup.get_text()
with open(filename, 'w', encoding='utf-8') as file:
file.write(text)
def preprocess_text(text):
# Convert text to lowercase
text = text.lower()
text = re.sub(r'[^\w\s]', '', text)
text = re.sub(r'\s+', ' ', text)
return text
def word_tokenize(text):
# Split the text into individual words
words = text.split()
return words
def character_tokenize(text):
# Split the text into individual characters
characters = list(text)
return characters
if __name__ == "__main__":
url = input("Enter the URL of the webpage to parse: ")
filename = "parsed_text.txt"
# Download the web page content and save it to a text file
# you can try with https://www.eluniversal.com.mx/
download_and_save_text(url, filename)
# Read the contents of the file
with open(filename, 'r', encoding='utf-8') as file:
input_text = file.read()
# Text Preprocessing
preprocessed_text = preprocess_text(input_text)
print("Preprocessed Text:", preprocessed_text)
# Word Tokenization
words = word_tokenize(preprocessed_text)
print("Word Tokens:", words)
# Character Tokenization
characters = character_tokenize(preprocessed_text)
print("Character Tokens:", characters)