rockerritesh commited on
Commit
ac96a59
·
verified ·
1 Parent(s): 46bafe8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -30
app.py CHANGED
@@ -2,9 +2,8 @@ import streamlit as st
2
  import PyPDF2
3
  import io
4
  import os
5
- import re
6
 
7
- # Updated Unicode mappings
8
  unicodeatoz = ["ब", "द", "अ", "म", "भ", "ा", "न", "ज", "ष्", "व", "प", "ि", "फ", "ल", "य", "उ", "त्र", "च", "क", "त", "ग", "ख", "ध", "ह", "थ", "श"]
9
  unicodeAtoZ = ["ब्", "ध", "ऋ", "म्", "भ्", "ँ", "न्", "ज्", "क्ष्", "व्", "प्", "ी", "ः", "ल्", "इ", "ए", "त्त", "च्", "क्", "त्", "ग्", "ख्", "ध्", "ह्", "थ्", "श्"]
10
  unicode0to9 = ["ण्", "ज्ञ", "द्द", "घ", "द्ध", "छ", "ट", "ठ", "ड", "ढ"]
@@ -47,25 +46,40 @@ symbolsDict = {
47
  "÷": "/"
48
  }
49
 
50
- # Add normalization to handle conjunct consonants
51
  def normalizePreeti(preetitxt):
52
- # Replace complex combinations for conjunct consonants first
53
- preetitxt = re.sub(r'qm', 'ष', preetitxt)
54
- preetitxt = re.sub(r'f]', '', preetitxt)
55
- preetitxt = re.sub(r'km', '', preetitxt)
56
- preetitxt = re.sub(r'0f', '', preetitxt)
57
- preetitxt = re.sub(r'If', 'क्ष', preetitxt)
58
- preetitxt = re.sub(r'if', '', preetitxt)
59
- preetitxt = re.sub(r'cf', '', preetitxt)
60
- preetitxt = re.sub(r'ग्घ', '', preetitxt) # Handle conjunct consonants
61
-
62
- # Additional normalization logic for complex combinations
63
- preetitxt = re.sub(r'([a-zA-Z])\{', r'\1्', preetitxt) # Handle half letters like क्, न्, म्, etc.
64
-
65
- # Special handling for common Nepali conjuncts
66
- preetitxt = preetitxt.replace('ज्ञ', 'ज्ञ') # Handle frequently used conjuncts
67
-
68
- return preetitxt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  def convert(preeti):
71
  converted = ''
@@ -87,13 +101,10 @@ def convert(preeti):
87
 
88
  def extract_text_from_pdf(pdf_file):
89
  text = ''
90
- try:
91
- with open(pdf_file, 'rb') as file:
92
- reader = PyPDF2.PdfReader(file)
93
- for page in reader.pages:
94
- text += page.extract_text()
95
- except Exception as e:
96
- text = f"Error extracting text: {e}"
97
  return text
98
 
99
  def process_file(inputfile):
@@ -101,12 +112,12 @@ def process_file(inputfile):
101
  if ext == '.pdf':
102
  preeti = extract_text_from_pdf(inputfile)
103
  else:
104
- with open(inputfile, "r", encoding="utf-8") as fp:
105
  preeti = fp.read()
106
  return convert(preeti)
107
 
108
  def main():
109
- st.title("Preeti to Unicode Converter")
110
 
111
  uploaded_file = st.file_uploader("Choose a PDF or TXT file", type=["pdf", "txt"])
112
 
@@ -138,4 +149,4 @@ def main():
138
  )
139
 
140
  if __name__ == "__main__":
141
- main()
 
2
  import PyPDF2
3
  import io
4
  import os
 
5
 
6
+
7
  unicodeatoz = ["ब", "द", "अ", "म", "भ", "ा", "न", "ज", "ष्", "व", "प", "ि", "फ", "ल", "य", "उ", "त्र", "च", "क", "त", "ग", "ख", "ध", "ह", "थ", "श"]
8
  unicodeAtoZ = ["ब्", "ध", "ऋ", "म्", "भ्", "ँ", "न्", "ज्", "क्ष्", "व्", "प्", "ी", "ः", "ल्", "इ", "ए", "त्त", "च्", "क्", "त्", "ग्", "ख्", "ध्", "ह्", "थ्", "श्"]
9
  unicode0to9 = ["ण्", "ज्ञ", "द्द", "घ", "द्ध", "छ", "ट", "ठ", "ड", "ढ"]
 
46
  "÷": "/"
47
  }
48
 
 
49
  def normalizePreeti(preetitxt):
50
+ normalized = ''
51
+ previoussymbol = ''
52
+ preetitxt = preetitxt.replace('qm', 's|')
53
+ preetitxt = preetitxt.replace('f]', '')
54
+ preetitxt = preetitxt.replace('km', '')
55
+ preetitxt = preetitxt.replace('0f', '')
56
+ preetitxt = preetitxt.replace('If', 'क्ष')
57
+ preetitxt = preetitxt.replace('if', '')
58
+ preetitxt = preetitxt.replace('cf', '')
59
+ index = -1
60
+ while index + 1 < len(preetitxt):
61
+ index += 1
62
+ character = preetitxt[index]
63
+ try:
64
+ if preetitxt[index + 2] == '{':
65
+ if preetitxt[index + 1] == 'f' or preetitxt[index + 1] == 'ो':
66
+ normalized += '{' + character + preetitxt[index + 1]
67
+ index += 2
68
+ continue
69
+ if preetitxt[index + 1] == '{':
70
+ if character != 'f':
71
+ normalized += '{' + character
72
+ index += 1
73
+ continue
74
+ except IndexError:
75
+ pass
76
+ if character == 'l':
77
+ previoussymbol = 'l'
78
+ continue
79
+ else:
80
+ normalized += character + previoussymbol
81
+ previoussymbol = ''
82
+ return normalized
83
 
84
  def convert(preeti):
85
  converted = ''
 
101
 
102
  def extract_text_from_pdf(pdf_file):
103
  text = ''
104
+ with open(pdf_file, 'rb') as file:
105
+ reader = PyPDF2.PdfReader(file)
106
+ for page in reader.pages:
107
+ text += page.extract_text()
 
 
 
108
  return text
109
 
110
  def process_file(inputfile):
 
112
  if ext == '.pdf':
113
  preeti = extract_text_from_pdf(inputfile)
114
  else:
115
+ with open(inputfile, "r") as fp:
116
  preeti = fp.read()
117
  return convert(preeti)
118
 
119
  def main():
120
+ st.title("PDF/TXT to Unicode Converter")
121
 
122
  uploaded_file = st.file_uploader("Choose a PDF or TXT file", type=["pdf", "txt"])
123
 
 
149
  )
150
 
151
  if __name__ == "__main__":
152
+ main()