-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfunctions.py
142 lines (111 loc) · 3.45 KB
/
functions.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
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
# importing the modules
import gettext
import re
import requests
import json
import urllib.parse
from bs4 import BeautifulSoup
from urllib.request import urlopen
# Create a function to add translations to the program
# This function is used to identify the program with translations added to it by contributors
def SetLanguage(CurrentSettings):
# This dictionary is used to define program on language folder and on name of language within program settings
# The dictionary key indicates the name of the language within the program settings. For example: "English"
# The dictionary value indicates the folder that contains the language files. Example: "en"
language = {
"English": "en",
"Arabic": "ar",
"Spanish": "es",
"French": "fr"
}
try:
CurrentLanguage = language[CurrentSettings["language"]]
except:
_ = gettext.gettext
return _
try:
ChangeLanguage = gettext.translation('WikiSearch', localedir='languages', languages=[CurrentLanguage])
ChangeLanguage.install()
_ = ChangeLanguage.gettext
except:
_ = gettext.gettext
return _
def DisableLink(HtmlContent):
HtmlContent = HtmlContent
pattern =r'(href\=\".*?\")'
result = re.sub(pattern, 'role="link" aria-disabled="false"', HtmlContent, flags=re.MULTILINE)
return result
def GetOnlineInfo():
#load and read gson online file
url = "https://raw.githubusercontent.com/tecwindow/WikiSearch/main/WikiSearch.json"
response = urlopen(url)
data_json = json.loads(response.read())
#Putting information into a dictionary.
info = {
"name": data_json["name"],
"version": data_json["version"],
"What's new": data_json["What's new"],
"url": data_json["url"]
}
return info
# Delete The empty lines.
def remove_blank_lines(text):
lines = text.split("\n")
lines = list(filter(lambda x: x.strip(), lines))
text = "\n".join(lines)
return text
# Getting the tables of article.
def GetTables(url):
reqs = requests.get(url)
# using the BeautifulSoup module
soup = BeautifulSoup(reqs.content, "html.parser")
tables = soup.find_all("table")
TablesText = []
for i in range(len(tables)):
content = tables[i].text
content = remove_blank_lines(content)
TablesText.append(content + "\n")
return TablesText
# Include List of languages in JSON format.
def LanguageJSON():
# Check existence of file before running program.
try:
with open('LanguageCodes.json', encoding="utf-8") as json_file:
data = json.load(json_file)
except FileNotFoundError:
wx.MessageBox(_("Some required files are missing."), _("Error"), style=wx.ICON_ERROR)
exit()
# Create a empty list and dictionary.
name = []
code = {}
# Include json file content and add it to list.
for w in data:
name.append(w["name"])
code[w["name"]] = w["code"]
return name, code
# Get title and language of any article from its link.
def GetTitleFromURL(url):
url = urllib.parse.unquote(url)
url = url.split('/')
LanguageCode = url[2].split(".")[0]
title = url[-1]
code = LanguageJSON()[1]
keys = list(code.keys())
Values = list(code.values())
LanguageName = keys[Values.index(LanguageCode)]
return title, LanguageName, LanguageCode
# Analyze the text.
def count_text_items(text):
lines = text.count('\n') + 1
paragraphs = text.count('\n\n') + 1
sentences = len(re.findall(r'[^.!?]+[.!?]', text))
words = len(re.findall(r'\b\w+\b', text))
characters = len(text) + 1
information = {
'lines': lines,
'paragraphs': paragraphs,
'sentences': sentences,
'words': words,
'characters': characters
}
return information