Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Assignment1 {
  5.  
  6.     public static void main(String[] args) {
  7.         try {
  8.             System.out.print("Enter name of a directory> ");
  9.             Scanner scan = new Scanner(System.in);
  10.             File dir = new File(scan.nextLine());
  11.             File[] fileList = dir.listFiles();
  12.  
  13.             //stop words array
  14.             List<String> stopWords = Arrays.asList("i", "was", "the", "and", "am", "an", "it", "is", "a", "of", "&", "for", "this", "in", "with");
  15.  
  16.             //stemmer stuff
  17.             Stemmer s = new Stemmer();
  18.  
  19.             String filePath = dir.toString();
  20.  
  21.             //Map<String, String> wordanDoc = new HashMap<String, String>();
  22.             Map<String, String> invIndex = new HashMap<String, String>();
  23.             Set<String> allWords= new HashSet<String>();
  24.             for(File f: fileList) {
  25.  
  26.                 Scanner sc = new Scanner(f);
  27.  
  28.                 while(sc.hasNextLine()) {
  29.                     StringTokenizer st = new StringTokenizer(sc.nextLine());
  30.                     while (st.hasMoreTokens()) {
  31.                         String word = st.nextToken();
  32.  
  33.                         //file location testing purpose: C:\Users\brobs\OneDrive\Desktop\Classes\taglines\taglines
  34.                         //       /home/brobs0111/Documents/taglines/
  35.                         ///     /home/brobs0111/Documents/alldocs/
  36.  
  37.  
  38.                         //replace all punc besides hyphens and forward slashes and convert all to lower case
  39.                         word = word.replaceAll("[^\\w\\/\\+\\-]","").toLowerCase();
  40.  
  41.                         //if the word is a stop word it breaks out of loop
  42.                         if(stopWords.contains(word) )
  43.                             continue;
  44.  
  45.                         //the current file path is saved to docName then everything besides the name is removed
  46.                         String docName=f.getPath()
  47.                                 .replace(filePath,"")
  48.                                 .replace("/","")
  49.                                 .replace(".txt","");
  50.  
  51.  
  52.                         //if(invIndex.containsValue(word)){
  53.                          //   String temp = wordanDoc.get(word).concat("," + docName);
  54.                          //   wordanDoc.put(word, temp);
  55.                         //}else{
  56.                         //    wordanDoc.put(word, docName);
  57.                         //}
  58.                         allWords.add(word);
  59.                         // adding to stemmer
  60.                         for (int i=0; i<word.length(); i++)
  61.                         {  if(!word.contains("-"))
  62.                             if (Character.isLetter(word.charAt(i))) {
  63.                                 s.add(word.charAt(i));
  64.                             }
  65.                         }
  66.                         //prevent hyphened words from being stemmed
  67.                         if(!word.contains("-")) {
  68.                             s.stem();
  69.                             if(invIndex.containsKey(s.toString())){
  70.                                 String temp = invIndex.get(s.toString()).concat("," + docName);
  71.                                 invIndex.put(s.toString(), temp);}
  72.                             else invIndex.put(s.toString(), docName);
  73.                         }
  74.                         //adds unstemmed word to wordIndex
  75.                         else {
  76.                             if(invIndex.containsKey(word)){
  77.                                 String temp = invIndex.get(word).concat("," + docName);
  78.                             invIndex.put(word, temp);}
  79.                             else invIndex.put(word, docName);
  80.  
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             //Map<String, String> sorted= new TreeMap<>();
  87.             //sorted.putAll(wordanDoc);
  88.             //for(Map.Entry<String, String> entry: sorted.entrySet())
  89.               //  System.out.println("Word: " + entry.getKey()+ "   DocID's<" + entry.getValue() + ">");
  90.  
  91.             //wordanDoc.entrySet().forEach(entry->{
  92.             //    System.out.println("Word: "+entry.getKey() + "|| DocID <" + entry.getValue()+"> ");
  93.             //});
  94.            
  95.             invIndex.entrySet().forEach(entry->{
  96.                 System.out.println("Word: "+entry.getKey() + "|| DocID <" + entry.getValue()+"> ");
  97.             });
  98.         }
  99.  
  100.         catch(Exception e) {
  101.             System.out.println("Error: " + e.toString());
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement