-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.py
More file actions
144 lines (111 loc) · 4.23 KB
/
Copy pathanalytics.py
File metadata and controls
144 lines (111 loc) · 4.23 KB
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
143
144
from utils import read_pdf_file, clean_pdf_page, get_sections, clean_text, get_similar_sentences
import pandas as pd
import streamlit as st
from sklearn.feature_extraction.text import CountVectorizer
import re
@st.cache(allow_output_mutation=True)
def get_words_in_sentances(pages, words, sections = None):
"""Get all words in the words parameter
Arguments:
pages {list} -- list of pages
words {list} -- list of words
Returns:
dict -- Dictionary containing the results of the query in the foramt {Word:DataFrame}
"""
words = [w.strip().lower() for w in words]
outputs = {}
for word in words:
all_matches = []
for page_ind, page in enumerate(pages):
clean_page = clean_pdf_page(page)
for sentance in clean_page:
if word in sentance.lower():
d = {"Sentance": sentance, "Page": page_ind + 1}
if sections is not None:
d['Section'] = sections[page_ind+1]
all_matches.append(d)
outputs[word] = pd.DataFrame(all_matches)
return outputs
@st.cache
def get_headers(pages):
"""Find Section headers and sub headers in a dataframe
Arguments:
pages {list} -- list of pages to extract headers from
Returns:
list -- list of all header titles
"""
results = []
page_nums = []
page_num = 0
for page in pages:
clean_page = clean_pdf_page(page)
for i in clean_page:
if (
i.startswith("Section") and "page" not in i
): # If the sentence starts with Secion X.
results.append(i)
page_nums.append(page_num + 1)
elif re.findall("^(\d+\.\d+\.*)(?![\d\.])", i) and not re.findall(
"\.\.\.", i
): # Else if the sentence begins with a section id (3.2.1, 1.1, etc)
results.append(i)
page_nums.append(page_num + 1)
page_num += 1
last_num = 1
cleaned_results = []
cleaned_page_nums = []
for ind, val in enumerate(results):
if str(val).lower() == "section.":
continue
if "section" in str(val).lower() or int(val.split(".")[0]) == last_num:
cleaned_results.append(val)
cleaned_page_nums.append(page_nums[ind])
elif int(val.split(".")[0]) == last_num + 1:
cleaned_results.append(val)
cleaned_page_nums.append(page_nums[ind])
last_num += 1
df = pd.DataFrame(
[cleaned_page_nums, cleaned_results], index=["Page Number", "Header"]
).T
return df
@st.cache
def get_frequent_words(pages):
"""Find the most common words in a list of pages
Arguments:
pages {list} -- list of pages
Returns:
dict -- Dictionary containing the section namea and values inside
"""
sections, _ = get_sections(pages)
sections = {key: clean_text(" ".join(val)) for key, val in sections.items()}
cv = CountVectorizer(min_df=1, max_df=0.8)
cv.fit(sections.values())
for key in sections:
trans = cv.transform([sections[key]]).toarray()[0]
s = pd.Series(trans, index=cv.get_feature_names()).sort_values(ascending=False)
s = (
s[s > 0][:10]
.to_frame(name="Count")
.reset_index()
.rename(columns={"index": "Word"})
)
sections[key] = s
return sections
def get_comparison_similar_words(pages_1, pages_2, words):
"""Finds similar sentences between two dataframes
Arguments:
pages_1 {list} -- list of first set of pages
pages_2 {list} -- list of second set of pages
words {list} -- list of words to be included in the search
Returns:
dict -- dictionary of results
"""
pages_1_words = get_words_in_sentances(pages_1, words)
pages_2_words = get_words_in_sentances(pages_2, words)
results = {}
for word in words:
if pages_2_words[word].shape[0] == 0 or pages_1_words[word].shape[0]==0:
results[word +"_2"]=pd.DataFrame()
else:
results[word +"_2"]=get_similar_sentences(pages_1_words[word], pages_2_words[word])
return results