Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 12 additions & 41 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint linebreak-style: ["error", "windows"]*/

import dotenv from 'dotenv';
import express from 'express';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import fs from 'fs';
import multer from 'multer';

import newIndex from './src/inverted-index';
Expand All @@ -29,50 +26,24 @@ server.use('/api', router);
router.route('/create')
.post(upload.array('allFiles'), (req, res) => {
const allfiles = req.files;

/* const filenameAndPath = [];
allfiles.forEach(i => filenameAndPath.push({ [i.originalname]: i.path }));
filenameAndPath.forEach((eachFilenameAndPath) => {
for (const [filename, filepath] of Object.entries(eachFilenameAndPath)) {
const fileContent = JSON.parse(fs.readFileSync(filepath, 'utf8'));
// res.status(200).json(fileContent);
}
});
/* const filenameAndPath = [];
allfiles.forEach(i => filenameAndPath.push({ [i.originalname]: i.path }));
for (const eachFilenameAndPath of filenameAndPath) {
console.log(Object.entries(eachFilenameAndPath));
for (const [filename, filepath] of Object.entries(eachFilenameAndPath)) {
const fileContent = JSON.parse(fs.readFileSync(filepath, 'utf8'));
res.status(200).json(filename);
}
}*/

const fileNameAndContent = newInvertedIndex.readBookDataApiMulter(allfiles);
newInvertedIndex.createIndex(fileNameAndContent);
res.status(200).json(newInvertedIndex.createdIndex);
res.status(200).json({
processedFiles: newInvertedIndex.createdIndex,
fileErrors: newInvertedIndex.errors
});
});
/*
let newArr = [];
Array.from(arr).forEach(a => newArr.push({[a.originalname]: a.path}));

router.post('/create', (req, res) => {

upload(req, res, (err) => {
if (err) {
console.error(err);
return
}

res.status(200).send('hi you');
});
});*/

router.route('/search')
.post((req, res) => {
const searchQueries = req.body;
const individualQueries = newInvertedIndex.takeInSearchQuery(searchQueries);
newInvertedIndex.searchIndex(individualQueries);
const searchQueries = req.body.searchTerms;
const filename = req.body.fileName;
const index = newInvertedIndex.createdIndex;
if (req.body.fileName === undefined) {
newInvertedIndex.searchIndex(index, searchQueries);
} else {
newInvertedIndex.searchIndex(index, filename, searchQueries);
}
res.status(200).json(newInvertedIndex.searchResult);
});

Expand Down
2 changes: 0 additions & 2 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint linebreak-style: ["error", "windows"]*/

import gulp from 'gulp';
// import nodemon from 'gulp-nodemon';
import jasmineNode from 'gulp-jasmine-node';
Expand Down
16 changes: 7 additions & 9 deletions src/inverted-index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint linebreak-style: ["error", "windows"]*/
import path from 'path';
import fs from 'fs';

Expand Down Expand Up @@ -35,7 +34,6 @@ export default class InvertedIndex {
requestFileObject.forEach(i => filenameAndPath.push({ [i.originalname]: i.path }));

for (const eachFilenameAndPath of filenameAndPath) {
console.log(Object.entries(eachFilenameAndPath));
for ([this.filename, this.filepath] of Object.entries(eachFilenameAndPath)) {
try {
this.fileContent = Array.from(JSON.parse(fs.readFileSync(this.filepath, 'utf8')));
Expand All @@ -53,7 +51,7 @@ export default class InvertedIndex {
createIndex(fileObject) {
const mappedIndex = {};
for (this.currentFile of fileObject) {
if (this.validateFileContent(this.currentFile.fileContent)) {
if (this.validateFileContent(this.currentFile.fileContent, this.currentFile.filename)) {
const wordsCollection = {};
for (const [index, newObject] of this.currentFile.fileContent.entries()) {
let titleTextArray = [];
Expand Down Expand Up @@ -122,23 +120,23 @@ export default class InvertedIndex {
}
}

validateFileContent(data) {
validateFileContent(data, filename) {
let hasErrors = false;
if (data.length > 0 && data.some(i =>
(JSON.stringify(i)[0]) !== '{' && JSON.stringify(i)[JSON.stringify(i).length - 1] !== '}')) {
this.errors.push(new DataError('file is not a JSON array', data));
this.errors.push(new DataError('file is not a JSON array', filename));
hasErrors = true;
}

if (!hasErrors && data.some(i =>
JSON.stringify(i) === JSON.stringify({})) && Object.keys(data[0]).length === 0) {
this.errors.push(new DataError('JSON object cannot be empty or contain empty objects', data));
this.errors.push(new DataError('JSON object cannot be empty or contain empty objects', filename));
hasErrors = true;
}

if (!hasErrors && data.some(i =>
i.title === undefined || i.text === undefined)) {
this.errors.push(new DataError('Bad JSON Array format', data));
this.errors.push(new DataError('Bad JSON Array format', filename));
hasErrors = true;
}
return !hasErrors;
Expand All @@ -151,8 +149,8 @@ export default class InvertedIndex {
}

class DataError {
constructor(message, data) {
constructor(message, filename) {
this.message = message;
this.data = data;
this.filename = filename;
}
}
1 change: 0 additions & 1 deletion tests/inverted-index-testSpec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint linebreak-style: ["error", "windows"]*/
import newIndex from '../src/inverted-index';

describe('Inverted Index Tests', () => {
Expand Down