|
| 1 | +package Files; |
| 2 | +import java.io.*; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | + |
| 6 | +import test.*; |
| 7 | +import util.Item; |
| 8 | +import util.List; |
| 9 | +import util.Node; |
| 10 | +import util.Word; |
| 11 | + |
| 12 | +public class FileOps { |
| 13 | + |
| 14 | + private File f; |
| 15 | + private List lList; |
| 16 | + private int minSize, maxSize; |
| 17 | + static int threshold; |
| 18 | + final int tokenSize = maxSize + 4; |
| 19 | + |
| 20 | + |
| 21 | + public FileOps(String fNam,List l, int t,int low, int up) throws FileNotFoundException |
| 22 | + { |
| 23 | + |
| 24 | + f = new File(fNam); |
| 25 | + minSize = low; |
| 26 | + maxSize = up; |
| 27 | + lList = l; |
| 28 | + threshold = t; |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + public File getFile() |
| 36 | + { |
| 37 | + return this.f; |
| 38 | + } |
| 39 | + |
| 40 | + public void retrieveContext() throws IOException,FileNotFoundException |
| 41 | + { |
| 42 | + BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(f))); |
| 43 | + String line; |
| 44 | + int lCount = 0; |
| 45 | + LineItem l; |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + //itterate through the file, line - wise |
| 50 | + while((line = r.readLine()) != null) |
| 51 | + { |
| 52 | + |
| 53 | + |
| 54 | + //process, get an Item back |
| 55 | + l = (LineItem) formatInput(line.trim(), lCount); |
| 56 | + |
| 57 | + //store LineItem |
| 58 | + if(l != null) |
| 59 | + storeLine(lList,l); |
| 60 | + |
| 61 | + |
| 62 | + //count to next line. |
| 63 | + lCount++; |
| 64 | + |
| 65 | + } |
| 66 | + //close reader |
| 67 | + |
| 68 | + r.close(); |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + /* |
| 78 | + * |
| 79 | + * Store the context of a (double) linked list given as String, in a .txt file. |
| 80 | + * |
| 81 | + * */ |
| 82 | + |
| 83 | + public void storeContext() throws IOException,FileNotFoundException |
| 84 | + { |
| 85 | + |
| 86 | + //create a writer object showing on the given file. Whatever inside the file f will be deleted. |
| 87 | + BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f))); |
| 88 | + |
| 89 | + Node curr = lList.getHead(); |
| 90 | + |
| 91 | + //itterate through the whole text - list. |
| 92 | + while(curr != null) |
| 93 | + { |
| 94 | + |
| 95 | + |
| 96 | + //write on file, line wise. |
| 97 | + w.write(curr.getValue().toString()); |
| 98 | + w.newLine(); |
| 99 | + |
| 100 | + //proceed |
| 101 | + |
| 102 | + curr = curr.getNext(); |
| 103 | + } |
| 104 | + |
| 105 | + w.close(); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + /* |
| 112 | + * -Gets a String representing an ASCII text and a line Number. Returns a LineItem with info given or null if given string is emptyLine. |
| 113 | + * -Trims given string - input in case that this exceeds threshold characters. |
| 114 | + * |
| 115 | + * */ |
| 116 | + |
| 117 | + public static Item formatInput(String lContext, int lNum) |
| 118 | + { |
| 119 | + |
| 120 | + if(lContext.length() > threshold) |
| 121 | + { |
| 122 | + |
| 123 | + //trim given input to meet size requirements, ignore characters exceeding limit |
| 124 | + lContext = lContext.substring(0, threshold); |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + // exclude the empty or whitespaced line. |
| 131 | + if(lContext.isEmpty()) |
| 132 | + return null; |
| 133 | + |
| 134 | + //create a LineItem |
| 135 | + LineItem l = new LineItem(lContext); |
| 136 | + |
| 137 | + |
| 138 | + return l; |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + /* |
| 143 | + * Based on a list of multiple words, the function splits them and creates a sorted ArrayList of tuples<String word, Integer lineOfText> (see Word.java class). |
| 144 | + * Returns those on a lexicographically sorted format. |
| 145 | + * Sorting gets Implemented based on the fact that the tuples class (see Word.java) implements the Comparable Interface. |
| 146 | + * |
| 147 | + * */ |
| 148 | + |
| 149 | + public ArrayList<Word> fillWordMap() |
| 150 | + { |
| 151 | + int index = 0; |
| 152 | + Node curr = lList.getHead(); |
| 153 | + String delim = "\\W+"; |
| 154 | + ArrayList<Word> words = new ArrayList<>(); |
| 155 | + |
| 156 | + |
| 157 | + //itterate through each line |
| 158 | + while(curr != null) |
| 159 | + { |
| 160 | + LineItem lineIt = (LineItem) curr.getValue(); |
| 161 | + Line l = (Line) lineIt.getData(); |
| 162 | + String[] ws = l.getContext().split(delim); |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + for(int i=0; i<ws.length; i++) |
| 167 | + { |
| 168 | + |
| 169 | + //check the validity of the given word. |
| 170 | + if(isValidWord(ws[i])) |
| 171 | + { |
| 172 | + |
| 173 | + //create a word object (string, int lNum) |
| 174 | + Word w = new Word(ws[i],index+1); |
| 175 | + |
| 176 | + //append on an arraylist. |
| 177 | + words.add(w); |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + index++; |
| 185 | + curr = curr.getNext(); |
| 186 | + |
| 187 | + } |
| 188 | + |
| 189 | + //sort the array list based on the "word"-key. |
| 190 | + Collections.sort(words); |
| 191 | + |
| 192 | + ///return the sorted arraylist. |
| 193 | + |
| 194 | + return words; |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + /* |
| 205 | + * Stores a created line item in a double linked list. |
| 206 | + * |
| 207 | + * */ |
| 208 | + |
| 209 | + public void storeLine(List l, LineItem line) |
| 210 | + { |
| 211 | + |
| 212 | + l.append(line); |
| 213 | + |
| 214 | + } |
| 215 | + |
| 216 | + /* |
| 217 | + * Decides for the validity of the given string based on thresholds. (see FileOps class constructor). |
| 218 | + * |
| 219 | + * */ |
| 220 | + |
| 221 | + private boolean isValidWord(String w) |
| 222 | + { |
| 223 | + if(w.length() > maxSize || w.length() < minSize) |
| 224 | + return false; |
| 225 | + return true; |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | + |
| 230 | +} |
0 commit comments