-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfile_read_write.py
More file actions
64 lines (61 loc) · 2.72 KB
/
Copy pathfile_read_write.py
File metadata and controls
64 lines (61 loc) · 2.72 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
import timeit
import sys
import json
import os
from pathlib import Path
import pandas as pd
class file_read_write:
"""
This class contains the methods for performing reading and writing
operations on text files
"""
def cache_reader(self,query_refined,start):
"""
This method checks if the user query has already been answered
in the past.If the results already exist in cache.txt,then
results are directly given and unnecessary computations saved.
:param query_refined: list of tokenized user input strings
:param start: float value which references the starting time of running the main file
:return:None
"""
cache_file_reader = open('cache.txt')
line_iterator = 1
while True:
line = cache_file_reader.readline() # read line
if line_iterator % 2 != 0 and line.strip() == str(query_refined):
print(cache_file_reader.readline())
stop = timeit.default_timer()
print('Time: ', stop - start)
cache_file_reader.close()
sys.exit()
line = cache_file_reader.readline() # move to next line
if not line: # check if line is not empty
break
cache_file_reader.close()
def dataset_reader(self):
"""
This method reads all the text files present in the dataset and
adds them to a dataframe which contains columns namely
['headline', 'brief', 'article', 'type', 'filename']
:return: returns a dataframe of the read documents
"""
workingDirectory = os.getcwd() # gets the current working directory
directory = os.path.join(workingDirectory, "dataset\\bbc") # concatenates
print(directory)
data = [] # list to store each news article
folders = os.listdir(directory) # list of all files in the directory
for folder in os.listdir(directory):
if os.path.isdir(os.path.join(directory, folder)) is True:
for file in os.listdir(os.path.join(directory, folder)):
filePath = os.path.join(directory, folder, file)
txt = Path(filePath).read_text()
headline, brief, article = txt.split('\n\n', 2)
newsArticle = list() # current news article
newsArticle.append(headline)
newsArticle.append(brief)
newsArticle.append(article)
newsArticle.append(folder)
newsArticle.append(file)
data.append(newsArticle)
df = pd.DataFrame(data, columns=['headline', 'brief', 'article', 'type', 'filename'])
return df