Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment Your assignment is to develop an interactive Java-based system that g

ID: 3860599 • Letter: A

Question

Assignment Your assignment is to develop an interactive Java-based system that gets queries with k terms, where each term is marked by either a plus sign or a minus sign. Additionally, the system has access to a.CSV file that contains the results of the last indexing and crawling performed by the search-engine crawler. Ignoring computational complexity, design and implement (in Java, Java Collections, Java GUI, and Javadoc) the above system under the following spec. 1) The user can insert up to three terms via an input widget, 2) Along, with each term the user has to denote whether it is a plus-term or a minus-term (the plus 3) 4) and minus signs are entered via a widget such as a radio button and not as prefixes). The user hits a "Search" button (you do not have to implement this button) and then: The system checks if the terms entered are in the crawler list of inverted indexes. a. If any of the terms is not in the list, the system responds with "Invalid Terms." b. If the terms are in the list, the system responds with the first up-to 5 matching page-ids. c. If the result of the query has valid terms but does not match any page that complies with the constraints, the system responds with "No Match." 5) You can, use any collection[s] in your solution. There is no requirement to use a hash table and it is not necessarily the best data structure for storing the list of inverted indexes

Explanation / Answer

It accepts words and store in collections, where user can search from this data. Locking and unlocking read/wrire users also done. We need to initialize collections by adding words which we need to call addWord method.

import java.io.IOException;

import java.io.PrintWriter;

import java.nio.charset.Charset;

import java.nio.file.Files;

import java.nio.file.Path;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.SortedMap;

import java.util.TreeMap;

import java.util.TreeSet;

public class InvertedIndex {

private final TreeMap<String, TreeMap<String, TreeSet<Integer>>> inverted_index;

private final MultipleReaderLock _lock;

// constructor

public InvertedIndex() {

this.inverted_index = new TreeMap<String, TreeMap<String, TreeSet<Integer>>>();

this._lock = new MultipleReaderLock();

}

private void indexClear() {

inverted_index.clear();

}

// fetches data from parseFile method and pushes it

public void addWord(String filepath, int total, String _word) {

_lock.lockTheWrite();

if (inverted_index.get(_word) == null) {

inverted_index.put(_word, new TreeMap<String, TreeSet<Integer>>());

TreeMap<String, TreeSet<Integer>> fileMapping = inverted_index.get(_word);

fileMapping.put(filepath, new TreeSet<Integer>());

TreeSet<Integer> positions = inverted_index.get(_word).get(filepath);

positions.add(total);

}

if (inverted_index.get(_word).get(filepath) == null) {

TreeMap<String, TreeSet<Integer>> fileMapping = inverted_index.get(_word);

fileMapping.put(filepath, new TreeSet<Integer>());

TreeSet<Integer> positions = inverted_index.get(_word).get(filepath);

positions.add(total);

}

else {

inverted_index.get(_word).get(filepath).add(total);

}

_lock.unlockWrite(); //unlocking

}

public void storeAll(InvertedIndex _sample) {

_lock.lockTheWrite();

for (String _key : _sample.inverted_index.keySet()) {

if (inverted_index.containsKey(_key)) {

TreeMap<String, TreeSet<Integer>> fileMapping = inverted_index.get(_key);

for (String subMapping : _sample.inverted_index.get(_key).keySet()) {

if (!fileMapping.containsKey(subMapping)) {

fileMapping.put(subMapping, _sample.inverted_index.get(_key).get(subMapping));

} else {

TreeSet<Integer> tempCount = inverted_index.get(_key).get(subMapping);

tempCount.storeAll(_sample.inverted_index.get(_key).get(subMapping));

}

}

}

else {

inverted_index.put(_key, _sample.inverted_index.get(_key));

}

}

_sample.indexClear();

_lock.unlockWrite();

}

public void writeFile(Path outputFilePath) {

_lock.lockTheRead();

try (PrintWriter _outputWriter = new PrintWriter(Files.newBufferedWriter(

outputFilePath, Charset.forName("UTF-8")));) {

for (String _word : (inverted_index.keySet())) {

_outputWriter.write(_word);

_outputWriter.println();

for (String fileNames : inverted_index.get(_word).keySet()) {

_outputWriter.write(""" + fileNames + """);

for (int positions : inverted_index.get(_word).get(fileNames)) {

_outputWriter.write(", " + positions);

}

_outputWriter.println();

}

_outputWriter.println();

}

} catch (IOException e) {

System.out

.println("Error in file writing " + outputFilePath.toString());

}

_lock.unlockRead();

}

public ArrayList<SearchResult> searchResults(String[] queryWords) {

MultipleReaderLock localLock = new MultipleReaderLock();

_lock.lockTheRead();

HashMap<String, SearchResult> _mapResult = new HashMap<>();

for (String _word : queryWords) {

SortedMap<String, TreeMap<String, TreeSet<Integer>>> tempIndexMap = inverted_index

.tailMap(_word);

for (String _wordIndex : tempIndexMap.keySet()) {

if (_wordIndex.startsWith(_word)) {

for (String _wordLocation : inverted_index.get(_wordIndex).keySet()) {

localLock.lockTheWrite();

if (_mapResult.get(_wordLocation) == null) {

_mapResult.put(

_wordLocation,

new SearchResult(_wordLocation, inverted_index

.get(_wordIndex).get(_wordLocation)

.size(), inverted_index.get(_wordIndex)

.get(_wordLocation).ceiling(0)));

} else {

SearchResult tempMapEntry = _mapResult.get(_wordLocation);

tempMapEntry.addFrequency(inverted_index.get(_wordIndex)

.get(_wordLocation).size());

tempMapEntry.setPosition(inverted_index.get(_wordIndex)

.get(_wordLocation).ceiling(0));

}

localLock.unlockWrite();

}

} else {

break;

}

}

}

_lock.unlockRead();

ArrayList<SearchResult> finalList = new ArrayList<>();

finalList.storeAll(_mapResult.values());

Collections.sort(finalList);

return finalList;

}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class MultipleReaderLock {

private int currentWriters;

private int currentReaders;

//constructor

public MultipleReaderLock() {

currentWriters = 0;

currentReaders = 0;

}

// locking read

public synchronized void lockTheRead() {

while (currentWriters > 0) {

try {

this.wait();

} catch (InterruptedException e) {

System.out

.println("Inrerupted procedure --- read lock .");

}

}

currentReaders++;

}

public synchronized void unlockRead() {

currentReaders--;

if (currentReaders == 0) {

this.notifyAll();

}

}

public synchronized void lockTheWrite() {

while (currentReaders > 0 || currentWriters > 0) {

try {

this.wait();

} catch (InterruptedException e) {

System.out

.println("Inrerupted procedure --- write lock");

}

}

currentWriters++;

}

public synchronized void unlockWrite() {

currentWriters--;

this.notifyAll();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote