Advertisement
DanX

Untitled

Aug 23rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Random;
  13. import java.util.Scanner;
  14. import java.util.Set;
  15.  
  16. public class text_generator_mk2 {
  17.     private ArrayList<Word2> training;
  18.     private ArrayList<Word2> firstWords;
  19.     private static final int WORD_CAP=150;
  20.     public text_generator_mk2() {
  21.         training = new ArrayList<Word2>();
  22.         firstWords = new ArrayList<Word2>();
  23.     }
  24.     public void train(File text){
  25.         PartOfSpeechLabel posl = new PartOfSpeechLabel("partsofspeech/91K nouns.txt","partsofspeech/31K verbs.txt",
  26.                 "partsofspeech/28K adjectives.txt","partsofspeech/6K adverbs.txt",
  27.                 "partsofspeech/3 articles.txt","partsofspeech/150 prepositions.txt",
  28.                 "partsofspeech/46 conjuctions.txt","partsofspeech/77 pronouns.txt");
  29.         ArrayList<Word2> a = new ArrayList<Word2>();
  30.         try {
  31.             Scanner scanner = new Scanner(text);
  32.             if (scanner.hasNext()) {
  33.                 String word = scanner.next();
  34.                 Word2 w = new Word2(word);
  35.                 a.add(w);
  36.                 firstWords.add(w);
  37.             }
  38.             while (scanner.hasNext()) {
  39.                 Word2 w2 = new Word2(scanner.next());
  40.                 a.add(w2);
  41.                 //System.out.println(w2.toString()+ " " + isEndOfSentence(w2));
  42.                 if(isEndOfSentence(w2)){
  43.                     Sentence alpha = new Sentence(a);
  44.                     //System.out.println(alpha);
  45.                     posl.partOfSpeech(alpha);
  46.                     for(Word2 v: alpha.getSentence()){
  47.                         training.add(v);
  48.                     }
  49.                     a = new ArrayList<Word2>();
  50.                 }
  51.             }
  52.             training.add(new Word2("EOF"));
  53.             scanner.close();
  54.         }
  55.         catch (FileNotFoundException ex) {
  56.             ex.printStackTrace();
  57.         }
  58.     }
  59.     public void generate() throws IOException {
  60.         File file = new File("speech.txt");
  61.         FileWriter fw = new FileWriter(file);
  62.         BufferedWriter bff = new BufferedWriter(fw);
  63.         Map<Word2, Set<Word2>> map = getMap();
  64.         Map<Word2, ArrayList<Word2>> newMap = this.expandValues(map);
  65.         Random r = new Random();
  66.         int rand = r.nextInt(firstWords.size());
  67.         Word2 word = firstWords.get(rand);
  68.         bff.write(word + " ");
  69.         int loc = rand;
  70.         int count = 1;
  71.         ArrayList<Word2> temp = new ArrayList<Word2>();
  72.         while (hasNextWord(newMap,word) && !word.equals(new Word2("EOF"))&&count<WORD_CAP) {
  73.             partsofspeech pos;
  74.             if(training.get(loc+1).getPos().size()==0){
  75.                 pos = partsofspeech.UNKNOWN;
  76.             }
  77.             else{
  78.                 pos = training.get(loc+1).getPos().get(0);
  79.             }
  80.             if(pos.equals(partsofspeech.UNKNOWN)||pos.equals(partsofspeech.ARTICLE)||pos.equals(partsofspeech.CONJUNCTION)){
  81.                 word = training.get(loc+1);
  82.             }
  83.             else{
  84.                 word = this.getNextWord(newMap,word,r,pos);
  85.             }
  86.             temp.add(word);
  87.             if(isEndOfSentence(word)){
  88.                 Sentence s = new Sentence(temp);
  89.                 if(s.isProbableSentence()) bff.write(s + "\r\n");
  90.                 else{
  91.                     count-=temp.size();
  92.                 }
  93.                 temp = new ArrayList<Word2>();
  94.             }
  95.             //System.out.println(word);
  96.             loc++;
  97.             count++;
  98.         }
  99.         bff.close();
  100.     }
  101.     private Word2 getNextWord(Map<Word2, ArrayList<Word2>> newMap, Word2 word,Random r,partsofspeech s) {
  102.         ArrayList<Word2> list = newMap.get(word);
  103.         //System.out.println(list.size());
  104.         ArrayList<Word2> edit = removeNon(list,s);
  105.         //System.out.println(edit.size());
  106.         return edit.get(r.nextInt(edit.size()));
  107.     }
  108.  
  109.     private boolean hasNextWord(Map<Word2, ArrayList<Word2>> newMap, Word2 word) {
  110.         return newMap.containsKey(word) && newMap.get(word).size() > 0;    
  111.     }
  112.     private Map<Word2, Set<Word2>> getMap() {
  113.         HashMap<Word2,Set<Word2>> map = new HashMap<Word2,Set<Word2>>();
  114.         for (int k = 0; k < training.size()-1; k++) {
  115.             if (!map.containsKey(training.get(k))) {
  116.                 HashSet<Word2> set = new HashSet<Word2>();
  117.                 set.add(training.get(k+1));
  118.                 map.put(training.get(k), set);
  119.             }
  120.             else {
  121.                 if (!map.get(training.get(k)).contains(training.get(k+1))) {
  122.                     map.get(training.get(k)).add(training.get(k+1));
  123.                 }
  124.                 else {
  125.                     Iterator<Word2> it = map.get(training.get(k)).iterator();
  126.                     while (it.hasNext()) {
  127.                         Word2 w = it.next();
  128.                         if (w.equals(training.get(k+1))) {
  129.                             w.increment();
  130.                             break;
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.         return map;
  137.     }
  138.     private Map<Word2, ArrayList<Word2>> expandValues(Map<Word2, Set<Word2>> map) {
  139.         Map<Word2,ArrayList<Word2>> m = new HashMap<Word2,ArrayList<Word2>>();
  140.         for (Word2 s: map.keySet()) {
  141.             ArrayList<Word2> list = new ArrayList<Word2>();
  142.             for (Word2 w: map.get(s)) {
  143.                 int count = 0;
  144.                 while (count < w.getCount()) {
  145.                     list.add(w);
  146.                     count++;
  147.                 }
  148.             }
  149.             m.put(s,list);
  150.         }
  151.         return m;
  152.     }
  153.     private boolean isEndOfSentence(Word2 s){
  154.         if(s.getWord().contains("."))return true;
  155.         return false;
  156.     }
  157.     private ArrayList<Word2> removeNon(ArrayList<Word2> w, partsofspeech p){
  158.         ArrayList<Word2> toReturn = new ArrayList<Word2>();
  159.         for(Word2 v : w){
  160.             boolean yes = false;
  161.             for(partsofspeech i : v.getPos()){
  162.                 if(i.equals(p)) yes = true;
  163.             }
  164.             if(yes) toReturn.add(v);
  165.         }
  166.         if(toReturn.size()==0){
  167.             return w;
  168.         }
  169.         return toReturn;
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement