I am not a fan of article spinner. This is just for fun. As the name implies, an article spinner spins the content of an article. In this simple program, when a button is clicked, user will be given with a number of options (words) that he can use to replace certain words contained in a textarea. Below is the complete program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class ArticleSpinner extends JFrame{ JTextArea tarea=null; JScrollPane spane=null; JButton bspin=null; String starea=""; ArrayList<String> al=new ArrayList<String>(); public ArticleSpinner(){ super("Article Spinner"); setSize(800,600); setLayout(new FlowLayout()); tarea=new JTextArea(); tarea.setLineWrap(true); tarea.addMouseListener(new MouseHandler()); spane=new JScrollPane(tarea); spane.setPreferredSize(new Dimension(750,500)); add(spane); bspin=new JButton("Spin"); bspin.addActionListener(new ButtonHandler()); add(bspin); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class MouseHandler implements MouseListener{ public void mouseClicked(MouseEvent e){ try{ starea=tarea.getText(); if(!starea.contains("{")){ return; } int indexclicked=tarea.getCaretPosition(); int index1=0; int index2=0; for(int i=indexclicked;i>0;i--){ if(starea.charAt(i)=='{'){ index1=i; break; } else if(starea.charAt(i)=='}'){ return; } } for(int i=indexclicked;i<starea.length();i++){ if(starea.charAt(i)=='}'){ index2=i; break; } else if(starea.charAt(i)=='{'){ return; } } int index1b=0; int index2b=0; for(int i=indexclicked;i>=index1;i--){ if(starea.charAt(i)=='|'){ index1b=i; break; } } for(int i=indexclicked;i<=index2;i++){ if(starea.charAt(i)=='|'){ index2b=i; break; } } if(index1b==0){ index1b=index1; } if(index2b==0){ index2b=index2; } tarea.setText(""); for(int i=0;i<index1;i++){ tarea.append(starea.charAt(i)+""); } if((index1b!=index1)&&(index2b!=index2)){ for(int i=index1b+1;i<index2b;i++){ tarea.append(starea.charAt(i)+""); } } if((index1b==index1)&&(index2b!=index2)){ for(int i=index1b+1;i<index2b;i++){ tarea.append(starea.charAt(i)+""); } } if((index1b!=index1)&&(index2b==index2)){ for(int i=index1b+1;i<index2b;i++){ tarea.append(starea.charAt(i)+""); } } for(int i=index2;i<starea.length();i++){ tarea.append(starea.charAt(i+1)+""); } }catch(Exception ex){ } } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ readFile(); } } public void readFile(){ try{ ArrayList<String> al1=new ArrayList<String>(); ArrayList<String> al2=new ArrayList<String>(); ArrayList<String> altemp=new ArrayList<String>(); Scanner sc=new Scanner(new FileInputStream("synonyms.txt")); int counter=0; while(sc.hasNext()){ StringTokenizer stoken=new StringTokenizer(sc.nextLine(),"|"); al1.add(counter,stoken.nextToken()); al2.add(counter,stoken.nextToken()); counter++; } starea=""; starea=tarea.getText().toLowerCase(); for(int i=0;i<al2.size();i++){ if(starea.contains(al2.get(i))){ String stemp=al1.get(i); String stemp2=""; for(int j=0;j<al1.size();j++){ if(stemp.equals(al1.get(j))){ if(stemp2.equals("")){ stemp2=stemp2+al2.get(j); } else{ stemp2=stemp2+"|"+al2.get(j); } } } stemp2="{"+stemp2+"}"; starea=starea.replaceAll(" "+al2.get(i)+" "," "+stemp2+" "); starea=starea.replaceAll(" "+al2.get(i)+","," "+stemp2+","); starea=starea.replaceAll(" "+al2.get(i)+"."," "+stemp2+"."); starea=starea.replaceAll(" "+al2.get(i)+"?"," "+stemp2+"?"); starea=starea.replaceAll(" "+al2.get(i)+"!"," "+stemp2+"!"); starea=starea.replaceAll(","+al2.get(i)+" ",","+stemp2+" "); } } tarea.setText(""); tarea.setText(starea); }catch(Exception e) { } } public static void main(String[] args){ ArticleSpinner ap=new ArticleSpinner(); } } |
public void readFile(){ . . . }
The function of this method is to fetchs all the synonyms in the text file. Each word has an id to determine its synonyms. The id will be stored in the ArrayList "al1". The word itself will be stored in an ArrayList named "al2". Below is the format of the text file that is used to store all the synonyms along with their ids.
for(int i=0;i<al2.size();i++){ if(starea.contains(al2.get(i))){ String stemp=al1.get(i); String stemp2=""; for(int j=0;j<al1.size();j++){ if(stemp.equals(al1.get(j))){ if(stemp2.equals("")){ stemp2=stemp2+al2.get(j); } else{ stemp2=stemp2+"|"+al2.get(j); } } } . . . } }
After all the words in the file "synonym.txt" have been retrieved, they will be checked, one by one, against the content of the text area to determine which ones have a match. This occurs in the first for loop. The second for loop gets the synonyms of all the words by comparing their ids. All will be stored in the variable "stemp2". The content of this variable will replace the words in the text area which have synonyms.
class MouseHandler implements MouseListener{ . . . . . }
The class "MouseHandler" enables user to easily replace certain words with their most appropriate synonym.
if(!starea.contains("{")){ return; }
This if statement ensures that after all matching words have been replaced by their synonyms, no further action can be made by user. This condition is achieved when there is no "{" sign on the textarea.
int indexclicked=tarea.getCaretPosition();
This code gets the current position of the cursor. It will be used to determine the selected synonym.
if(starea.charAt(indexclicked)=='|'){ return; }
This short portion of program ensures that there is no effect when user clicks the "|" sign.
int index1=0; int index2=0; for(int i=indexclicked;i>0;i--){ if(starea.charAt(i)=='{'){ index1=i; break; } else if(starea.charAt(i)=='}'){ return; } } for(int i=indexclicked;i<starea.length();i++){ if(starea.charAt(i)=='}'){ index2=i; break; } else if(starea.charAt(i)=='{'){ return; } } int index1b=0; int index2b=0; for(int i=indexclicked;i>=index1;i--){ if(starea.charAt(i)=='|'){ index1b=i; break; } } for(int i=indexclicked;i<=index2;i++){ if(starea.charAt(i)=='|'){ index2b=i; break; } }
Put it simply, the variables "index1", "index2", "index1b", and "index2b" store the closest position of the "{", "}", "|" signs from the current position of the cursor.
for(int i=0;i<index1;i++){ tarea.append(starea.charAt(i)+""); } if((index1b!=index1)&&(index2b!=index2)){ for(int i=index1b+1;i<index2b;i++){ tarea.append(starea.charAt(i)+""); } } if((index1b==index1)&&(index2b!=index2)){ for(int i=index1b+1;i<index2b;i++){ tarea.append(starea.charAt(i)+""); } } if((index1b!=index1)&&(index2b==index2)){ for(int i=index1b+1;i<index2b;i++){ tarea.append(starea.charAt(i)+""); } } for(int i=index2;i<starea.length();i++){ tarea.append(starea.charAt(i+1)+""); }
These lines of code perform the replacement process. Synonyms that are not chosen by user, including the "{", "}", and "|" signs are removed from the textarea. Firstly, the content of the textarea is stored in the variable "starea" and the textarea is made empty. The first for loop then fills it back with characters contained in the variable "starea", starting from index 0 to index "index1". The other for loops do a similar thing.
No comments:
Post a Comment