-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomate_odt_extractionOO2.py
More file actions
92 lines (78 loc) · 2.91 KB
/
Copy pathautomate_odt_extractionOO2.py
File metadata and controls
92 lines (78 loc) · 2.91 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
import zipfile
import xml.etree.ElementTree as ET
import pandas as pd
import os
import matplotlib.pyplot as plt
class RE:
def __init__(self):
self.bold_text = []
os.chdir('/home/saul/Desktop/generative-AI/RE/')
print(os.getcwd())
def get_bold_text_from_odt(self, odt_file):
print(odt_file)
#bold_text = []
# Open the ODT file as a zip archive
with zipfile.ZipFile(odt_file, 'r') as odt_zip:
print('ODT_ZIP ', odt_zip)
# Extract content.xml from the ODT archive
with odt_zip.open('content.xml') as content_file:
# Parse content.xml using ElementTree
tree = ET.parse(content_file)
root = tree.getroot()
print('Root Tag ', root.tag)
print('Root Attrib ', root.attrib)
#for body in root.iter('{urn:oasis:names:tc:opendocument:xmlns:text:1.0}body'):
for child in root:
print(child.tag, child.attrib)
for body in child.iter('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}body'):
print('BODY ', body)
print(body.attrib.get('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}body', ''))
# Find all text:span elements with a text:style-name attribute containing "emphasis"
for span in root.iter('{urn:oasis:names:tc:opendocument:xmlns:text:1.0}span'):
#print('span :', span)
#print('root :', root)
style_name = span.attrib.get('{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name', '')
#unknown = root.attrib.get('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document-content')
#print('unkown :', unknown)
#print('style_name :', style_name.lower())
if 'strong' in style_name.lower():
# Get the text content of the span
text = ''.join(span.itertext())
#print('Text :', text)
self.bold_text.append(text)
print(self.bold_text)
#subject_freqs = self.__get_subject_freq(self.bold_text)
return self.bold_text
def __get_subject_freq(self, topics):
frequency = {}
# iterating over the list
for item in topics:
# checking the element in dictionary
if item in frequency:
# incrementing the counr
frequency[item] += 1
else:
# initializing the count
frequency[item] = 1
#sort frequencies
sorted_freq = sorted(frequency.items(), key=lambda x:x[1], reverse=True)
sorted_freq = dict(sorted_freq )
#self.__convert_dict_to_dataframe(sorted_freq)
#printing the frequency
#print(sorted_freq)
return sorted_freq
def __convert_dict_to_dataframe(self, freq):
#print(freq)
freq_dataframe = pd.DataFrame(list(freq.items()))
freq_dataframe.columns =['Name', 'Freq']
print(freq_dataframe.head())
#freq_dataframe.to_csv('subject_freq.csv', index=False)
#freq_dataframe.to_csv('cb_freq.csv', index=False)
#os.chdir('/home/saul/Desktop/generative-AI/RE/')
#
#odt_file = 'Gen_AI_Real_estate.odt'
#odt_file = 'use_of_cb_in_re_AU.odt'
odt_file = 'USE_CASE_1.odt'
re = RE()
strong_in_odt = re.get_bold_text_from_odt(odt_file)
#print(strong_in_odt)