-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
138 lines (123 loc) · 5.54 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
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
#step1.importing the image
#step2.Extracting the words from image
#step3.after extraction of words creating the vector of very less ie only meanin#gful information is kept
#step4. data preprocessing using any of nlp library
#step5.after preprocessing of information creating a training set and basically #creating a sort of mapping using rnn
#step1. Importing of the libraries
"""
Author:Shashank Jain
github@username:Shashankjain12
"""
#!usr/bin/python
import cv2
import os
import sys
from PIL import Image
import pytesseract
import re
import nltk
import numpy as np
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import PyPDF2
from translate import Translator
import argparse
#l=input("Enter the language you want to convert your notes:")
class NotesCreator:
def __init__(self,file_name):
self.translator=Translator(to_lang="Hindi")
self.lemmatizer=WordNetLemmatizer()
self.k=input("Do you want to translate your notes to Hindi? ")
self.file_name=file_name
#self.p=os.path.splitext(os.path.basename(os.path.join(os.getcwd(),file_name)))
self.p=os.path.splitext(file_name)
if self.p[1]==".png":
self.pngnotes()
else:
self.pdfnotes()
def pngnotes(self):
"""
Convert png files to notes this function extracts text from images using Pytesseract.
By this we aims to create a graph and then use that graph to construct the insights.
"""
if self.p[1]=='.png':
img=cv2.imread(self.file_name)
a=pytesseract.image_to_string(img)
cv2.imshow('image',img)
cv2.waitKey()
#b=a.split()
#print(b)
sentences=nltk.sent_tokenize(a)
word_tags=[]
for i in range(len(sentences)):
sentences[i]=re.sub(r"[@#$%^&|?!'\"]"," ",sentences[i])
words=nltk.word_tokenize(sentences[i])
newwords=[self.lemmatizer.lemmatize(word) for word in words if word not in stopwords.words('english')]
sentences[i]=' '.join(newwords)
"""
tagged_words=nltk.pos_tag(newwords)
for tw in tagged_words:
word_tags.append(tw[0]+" "+tw[1])
tagged_par=" ".join(word_tags)
namedEnt=nltk.ne_chunk(tagged_words)
print(namedEnt)
namedEnt.draw()
print(tagged_par)
"""
print(sentences)
paragraph="\n".join(sentences)
if self.k=='yes' or self.k=='y':
translation=self.translator.translate(paragraph)
print(translation)
else:
print(paragraph)
words=nltk.word_tokenize(paragraph)
tagged_words=nltk.pos_tag(words)
namedEnt=nltk.ne_chunk(tagged_words)
#for i in range(len(namedEnt)):
# print(namedEnt[i][1])
# print(namedEnt[i][1][i]
namedEnt.draw()
#print(paragraph)
def pdfnotes(self):
"""
Extract text from pdf file and then aims to construct a tree structure of semantics which can be used to
create notes from the pdf file.
"""
if self.p[1]==".pdf":
pdfFileObject = open('/home/shashank/Downloads/'+self.file_name, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObject)
count = pdfReader.numPages
sentence=[]
word_tags=[]
for i in range(count):
page = pdfReader.getPage(i)
sentence.append(page.extractText().split('\n'))
sentences=nltk.sent_tokenize(page.extractText())
for j in range(len(sentences)):
sentences[j]=re.sub(r"[Ò¥.@#$%^&|?!':\n\"//]"," ",sentences[j])
words=nltk.word_tokenize(sentences[j])
newwords=[self.lemmatizer.lemmatize(word) for word in words if word not in stopwords.words('english')]
sentences[j]=' '.join(newwords)
#print(sentences)
paragraph="\n".join(sentences)
#translation from english to any other language
if self.k=='yes' or self.k=='y':
translation=self.translator.translate(paragraph)
print(translation)
else:
print(paragraph)
words=nltk.word_tokenize(paragraph)
tagged_words=nltk.pos_tag(words)
namedEnt=nltk.ne_chunk(tagged_words)
print("page "+str(i)+":")
namedEnt.draw()
if __name__=="__main__":
parser=argparse.ArgumentParser(description='''NLP MACHINE LEARNING PROJECT
This project deals with creation of certain notes from a book
This project involves one of the research work of creating a project that deals with the creation of visually interactive graphs when we pass that module any kind of notebook of any kind of image file you just need to specify the type of image you need to pass on to that document and it will do all the hard work for you by itself by creating of certain nodes with the help of one of the most advanced library of python for desktop creation using tkinter
Follow the project on Github - https://github.com/Shashankjain12/bookish-invention''',formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('filename', type=str, help='Relative path to the image/pdf file to extract')
args=parser.parse_args()
filename = args.filename
notes=NotesCreator(filename)