Link to home
Start Free TrialLog in
Avatar of cancer_66
cancer_66Flag for United Arab Emirates

asked on

Search:- 3 For Mr ozymandias

Please answer questions 11,12
forget 10 for the time being.
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Mr Ozymandias still not finished your School project for you?

:-P ;-)
Tim lol ]:-)
11) No. The chnage I gave you was so that the number would start from 1 rather than 0. It will still be 1,2,3 etc. As I said the ranking is based on the score. Where the scores are equal then the order of precedence is based on the order in which the sentences were found. I would advise against having joint rankings.


12) OK. This works for me but I have a question for you.
Consider the following sentence :

"computer graphics is the display of digital images which is defined by science"

This matches both pattern1 ("is defined by") and pattern2("is the"). When it is given its score do you want the score to be :

a) the total of all sucessful matches
b) the highest score from any individual match
c) the score from the first match found

Option a) would be the best representation of its true rank i.e. high score = very good match. Option c) would be the most efficient in terms of speed, i.e. you could stop the matching once one match was successful and save time. Option b) has no particular advantages I can think of, but it is an option all the same.
10) I don't know very much about Aglets other than the papers from IBM and others that I have skimmed over. I have never implemented an aglet before. I assumed that you already had that bit done actually.
12) I added the pattern "is the" to my search.
It didn;t match any sentences because no sentence in the current files contains the word "the", hard to believe, but true.

So I added the test sentence I mentioned above :

"computer graphics is the display of digital images which is defined by science"

and it worked fine.

As for the question I asked above I have implemented both options a) and c) and called them "Full" and "Fast" respectively. I have added a new option box to the GUI so that the user can choose between Full Scoring and Fast Scoring.

I have mailed you all the new code and copies of the test files etc.
Avatar of cancer_66

ASKER

11)in answer to question 11 you commented " I would advise against having joint rankings." what do u mean by that.

lets say i have two identical sentence. therefore they both should be ranked first isnt it ? but the program ranks it 1,2.. eventhough it should be 1,1..

hmm or are u saying that if they score the same result than the order that the sentence come in the file effects the ranking ?

12)acually when i added the pattern "is the" i also added a sentence in file a1.txt inorder to check if it is working, and it does work but the only sentences that it retrives are the one's with pattern "is the".

sentences with "is defined as"..etc are not matched?

13)just downloaded the new code. as i compiled Defintion.java got the following error

C:\aglets\public\Expert Exchange +ranking 2\DefinitionChecker.java:39: cannot resolve symbol
symbol  : class PatternMatcher  
location: class DefinitionChecker
          PatternMatcher pm = new PatternMatcher();
                ^
C:\aglets\public\Expert Exchange +ranking 2\DefinitionChecker.java:39: cannot resolve symbol
symbol  : class PatternMatcher  
location: class DefinitionChecker
          PatternMatcher pm = new PatternMatcher();
                                        ^
C:\aglets\public\Expert Exchange +ranking 2\DefinitionChecker.java:60: cannot resolve symbol
symbol  : class PatternMatcher  
location: class DefinitionChecker
          PatternMatcher km = new PatternMatcher();
                ^
C:\aglets\public\Expert Exchange +ranking 2\DefinitionChecker.java:60: cannot resolve symbol
symbol  : class PatternMatcher  
location: class DefinitionChecker
          PatternMatcher km = new PatternMatcher();
                                        ^
4 errors
11) I don't think that any 2 sentences should ever have exactly the same rank. If they have the same score then something else has to differentiate them. In this case it is which one was found first.

12) Try the new code I sent you. It works fine with both patterns.
13)you forgot to add the PatternMatcher file in the "definitions" sub directory.
13) Make sure that you have unzipped all the files and that all the files that are supposed to be in the definitions subdirectory are in there, and not in the same directory as the DefinitionChecker.
Hold on ! Scrap that. I just checked the zip file and PatternMatcher.java was not in there, I must have missed it out.

I will mail it to you now.
13) i have unzipped it properly. i have the following file only

DefinitionChecker.java + UI1.java

definition (sub -directory)
contains:-sentence,stemmer,wordlist and wordPattern.java only. PatternMatcher is not there?

13)ok thanks ill just check it,
13)ok thanks ill just check it,
14)ok. now it will work if i added new patterns. ill try adding "can be defined as".
14)private static WordList list5 = new WordList("the",",",false);

what is the "false" for at the end?
15)ok it worked. fine. ill test it properly tomorrow.

16)about the aglets i have done some basic examples. but the master-slave pattern didnt work with me for some reason. it suppose to be quite straight forward if you know java. frankly speaking i find it complicated. anyways we will talk about this tomorrow.

its quite later over here. so ill call it a day. ill talk to you tomorrow.

thanks for all the help.

14) The boolean value on the end of the constructor for the WordList class tells the WordList whether to use stemming or not. If you set it to true then whenever a word from a sentence is compared to the WordList it will try to match it. If that fails it will try to match the stems of the words.

For instance you will notice that the WordLists containing the keywords "computer" and "graphics" use stemming, which is why you get a match for "computers" and "graphic".

You will also see that the WordList that contains "defined", "described" and "delimited" are set to use stemming, so it will match :

defined
defines
defining
define
described
decribes
describing
describe
delimited
delimit
delimiting
delimits

and so on.
15) OK. Let me know how you get on.

16) I would need to see some of these examples. If the whole idea is to make the program mobile, i.e. it is able to persist its state, move to another machine, and then carry on processing where it left off, then presumably you need at least two machines running aglet-supporting environments to test all this ?
Can we post the code here?

It is against the EE rules to email each other, as someone may find this on Google, and be royally fed-up that the code isn't here for their usage...

And someone is bound to be on the same course as cancer_66 next year, and will want their projects doing for them too ;-)
Yes. At the end of each question I have been posting the full code of the program at that stage. I am about to post the current code to the previous question https://www.experts-exchange.com/questions/20534863/Search-2-For-Mr-ozymandias.html and I will do the same for this question.

>>And someone is bound to be on the same course as cancer_66 next year, and will want their projects doing for them too ;-)

True, but I believe it is customary (with tutor approval) for students to pick their own projects, rather than have the same ones assigned year on year ;-)
> Yes. At the end of each question I have been posting the full code of the program at that stage. I am about to post the current code to the previous question

Sorry, I didn't notice that... :-/

Sorry for jumping the gun :-/

WOW!  What a lot of code!!  You should be paid for this, not getting points... ;-)

> True, but I believe it is customary (with tutor approval) for students to pick their own projects, rather than have the same ones assigned year on year ;-)

Hee hee, not in England, where the tutors are lazy ;-)
>>Sorry for jumping the gun :-/

It's OK. It was a valid point and they are long threads to have to read through and check.

>>WOW!  What a lot of code!!  You should be paid for this, not getting points... ;-)

Thanks. I'm sure it's quantity not quality there though :0)

Actually the largest file is Stemmer.java which I didn't write, it was borrowed from here http://www.tartarus.org/~martin/PorterStemmer/ and then had some modifications added to it by me to make it more useful to this sepcific program.
>>Hee hee, not in England, where the tutors are lazy ;-)


!!! That's not true in all cases. When I wrote my thesis I was specifcally prohibited from choosing any subject that came to close to any previous thesis. Titles and subjects submitted for approval were vetted pretty thoroughly by the faculty to prevent "recycling" :0)
17)hello, ill just try testing it right away. thanks
18)regarding the "fasle" at the end of the "wordList" i should only make it true for the mainMarkers right? i.e {defined,described,delimited..etc}

in case of two Patterns such as "is the" in this case its ambigous there isnt a mainMarker. but you cannot say its not a definition.
19)can you give me an example of a sentence and how would "full" and "fast" scoring effect it. please.

lets assume:-
"computer graphics is the display of digital images which is defined by science"

take ur time.
20)give me a while ill test it. i want to just finish the presentation first.

ill be waiting 4 ur answers.plz
please answer me whenever ill be waiting
18) There is no point setting stemming to on in a wordlist that only contains words that cannot be stemmed :

"is","are","were","be","by" and "as" etc have no stem value.

19) Sentence 25 is the only sentence in the current test set that is affected by Full/Fast scoring. Full/Fast scoring only affects the pattern score, not the key score.

"computer graphics is the display of digital images which is defined by science"

scored against two patterns "is defined by" and "is the" using STRICT matching

in FAST SCORING

is = 2 points
defined = 2 points
by = 2 points
strict match = 3 points

1st pattern score = 9

At this point, because we are fast scoring, the final pattern score returned is 9 because once a pattern match is found the program does not go on to check for any others.

However, in FULL SCORING, it would continue :

is = 2 points
the = 2 points
strict match = 3 points

2nd pattern score = 7 points


Total Pattern Score = 16

So, FULL and FAST will not make any difference to which sentences get returned, but it will chnage the way they are ranked. However, the major difference over greater volumes of data should be speed because fast matching will generally be quicker.
19)

sorry wasnt at my seat. yes i thought as much.
ok i understood now ! how it works.
ill test it in a short while.
thanks
20)would you help me with integrating it with the aglet? please.

am not sure if you recived my mail.
21)guess u r busy. take ur time. ill be waiting 4 ur reply
20) I got your mail and I have been looking at the resources you sent me.

Firstly, the IBM Aglet SDK will not run on JDK 1.2 or later. I cannot get Tahiti to run at all.

It looks like the project went open source and is now being run from SourceForge and Aglets.org and the specification has now reached verysion 2.0.2.

I have downloaded this and I am trying to set up and aglets 2.0.2 environment.
20)ok thanks, i think the aglets 2.02 works with jdk 1.2 and later versions.
21)iam using aglets1.1 and i was using JDk1.1.8 in the begining. but then started using jdk1.3.1 since the program u've written wasnt compiling.

Tahiti server works?
21)just to stay on the safe side its better to use aglets2.02
Yes. I now have two instances of Tahiti running on my machine and I can dispatch and retarct sample aglets etc etc.

I now have to work out how to :

Convert eitheer UI1 or DefinitionChecker or both to an Aglet or AgletProxy.
Sort out the cloning and dispatching and communications between the master & slave etc.
21)ok thanks alot. (am testing the scoring have some comments but would save them for later)
21)take ur time. thanks 4 all the help
How do you actually envisage this working ?
Do you expect to run/deploy the application from within Tahiti or what ?

This is looking like a very big job in territory with which I am not familiar.
22)yes exatcly after compiling the code simply add it to the list in Tahiti server , then create it. user interface should appear (note the destination of the slave is hard coded)

23)i think its better to try the basic master and slave pattern first. from example when the slave reaches the destination it prints something, then comes back to the master and prints "iam back"

sorry for the trouble..
please try and help me out
take ur time. i can wait,there is no hurry
i hope something worked. iam very worrid.
hi. iam back. any news?
hi. iam back. any news?
please answer me whenever u can. ill be waiting
guess you r busy, take ur time.. i dont mind waiting
I am not having much luck with this.

I have tried to create an aglet version of the DefinitinChecker class which uses then UI1.class as its GUI. It all compiles OK but I am getting instantiation errors when I try to create it as an aglet in tahiti.
OK. I now have an aglet I can create but I am having trouble working out how to set the tahiti server's ecdurity settings so that the aglet is allowed to read the text files. Any ideas.
1) are u getting security errors?

run the tahiti at command prompt using "agletsd -nosecurity"
2)after running the Tahiti server go to "options" sercurity" then for both trusted and untrusted option add the file name e.g "c:\b1.txt;"
2) this is another way of getting permission.
3)hope it works
please answer me whenever you are free.. ill be waiting, i really hope something works !
take ur time ill wait !
1) Yes. I have alreday tried adding files to the security options. It does not work. In my version of tahiti there is no option for trusted and untrusted aglets. You have to upply the codebase for each aglet and then specify its individual security permissions.

I will try running it with nosecurity.
1)i.c ok try it with no security.
OK. The -nosecurity option makes no difference. I have managed to make it work. In the end I had toi manually edit the .aglets/aglets.policy file. I think there may be a bug in the Securty Options dialog code in my version of tahiti.

OK, now I will see if I can dispatch the aglet to another server etc.
2)any luck ??
2)yes i guess there are bugs, thats a good news. at least one step ahead. hope something works.

thanks 4 ur help
ill be waiting. take ur time
any progress ??;/

thanks
I have made some progress. I now have an aglet (DefinitionChecker) which uses UI1 as its GUI. The problem was that when I tried to dispatch it to another server the dispatch fails because it could't serialize the PatternMatcher class as part of the dispatch.

It sems that any class that is going to take part in the aglet code must also extend aglet.

I think I'm nearly there, I just have to work out the inter-aglet communications.
THANKKKKKKKKKSSSSSSSS Alot you really made my day.seriously you dont know how happy u've made me.
iam glad something worked.

thanks for all the help.
take your time, iam ready to wait.
thanks for all the help and support
It's done.

I will mail you the code.
First here are the instructions :

1) You need to run two versions of Tahiti.
The first version keeps all defaults. The second one must run on post 5434 instaed of 4434.

2) You will need to find your aglets security policy file and replace it with the one I am sending you. It will probabkly be located in your home or user directory in a subdirectory called .aglets/security/aglets.policy.

3) The othe files I have sent need to be unzipped and put in %AGLETS_HOME%/public.
DefinitionChecker.java, UI1.java and all the text files go in public all the others must go in a subdirectory called definitions.

4) Now compile DefinitionChecker.java and that should cause everything to be compiled.

5) In the tahiti console for the instance of tahiti running on port 4434 create an instance of DefinitionChecker by simply adding "DefinitionChecker" to the list and then creating an instance of it.

6) To bring up the gui either double click on the running instance in the console or click on the "dialog" button.

7) First run a local search by typing in "computer graphics" and clicking on the go button. You should get a set of results. You will notice that there is an extra column in the results which indicates which host the file was found on.

8) Now run a remote search by clicking on the "Send Clone" button. This will create another instance of the DefinitionChecker which will be dispatched to the other tahiti server. It will run a search there and then revert back to its home and deliver its results to the original DefinitionChecker which will incorporate them into its own result set, re-sort it and display the whole lot.
Oh, there's one extra thing.

3a) You will need to modify line 32 on DefinitionChecker.java. It currently has the location of the text files on my machine hard coded in it so you will need to change the paths to point to the location on your machine.
a)sory i wasnt at my seat i just came back. ill check it right away..

thanks alot
b)this configuration looks quite complex. hope everything goes fine.

ill just do it. give me few min
I have just mailed you some new files.
c)shall i install aglets 2.02??
c)which version are you using. so that i could use the same.
You need to extract the jar file into a directory where you want to install Aglets and the run ant from the bin directory.

You may also then need to run ant install-home after than.

Then copy in all the new files I gave you including the security and props files.

Then compile DefinitionChecker.java in the public directory.

Then run "runboth.bat" in the bin directory.
d)ok just so we stay on track iam going to extact the jar file in C:\aglets
e)i extracted the jar file in aglets2

i have the following folder:-bin,cnf,lib,META-INF,Public

correct?
f)its saying java_home environment not set?

iam using windows xp
please answer me whenever iam waiting
Did you run ant from within the c:\aglets\bin durectory ?
d)i managed to run the two server 4434+5545

i extracted the DefinitionChecker.zip in

c:\aglets\public

now when i compile it

i get many errors

pakage import.ibm...etc not found

package defintions not found?
OK. You need to make sure that when you compile the code that the aglets_2.0.2.jar file is in your classpath.

Try running this command from within the public directory:

javac -cp .;c:\aglets\lib\aglets_2.0.2.jar;%classpath% DefinitionChecker.java
i added ur to my msn list but u r offline
Spectators may be interested to hear that the aglet code worked fine. I will post the current version of it below.

Make sure you read the instructions first :

1) You will need the Aglet SDK from :

http://prdownloads.sourceforge.net/aglets/aglets-2.0.2.jar?download 

2) Unpack the jar to the directory where you want to insall it and then run ant from the bin directory. Examine the output from ant. You may be directed to run ant -install-home.

2) You will need to find your aglets security policy file and replace it with the one I am sending you. It will probabkly be located in your home or user directory in a subdirectory called .aglets/security/aglets.policy.

3) The othe files I have sent need to be unzipped and put in %AGLETS_HOME%/public.
DefinitionChecker.java, UI1.java and all the text files go in public all the others must go in a subdirectory called definitions.

4) You will need to modify line 32 on DefinitionChecker.java. It currently has the location of the text files on my machine hard coded in it so you will need to change the paths to point to the location on your machine.

5) Now compile DefinitionChecker.java and that should cause everything to be compiled.

6) You now need to run two versions of Tahiti.
The first version keeps all defaults. The second one must run on port 5434 instaed of 4434. You can do this by making a copy of aglets.props in the aglets/cnf directory and aclling it aglets5434.props. Edit the copy and change the maf.port=4434 entry to maf.port=5434. Then in the bin directory run :

agletsd -f ../cnf/aglets.props

and

agletsd -f ../cnf/aglets5434.props

7) In the tahiti console for the instance of tahiti running on port 4434 create an instance of DefinitionChecker by simply adding "DefinitionChecker" to the list and then creating an instance of it.

8) To bring up the gui either double click on the running instance in the console or click on the "dialog" button.

9) First run a local search by typing in "computer graphics" and clicking on the go button. You should get a set of results. You will notice that there is an extra column in the results which indicates which host the file was found on.

10) Now run a remote search by clicking on the "Send Clone" button. This will create another instance of the DefinitionChecker which will be dispatched to the other tahiti server. It will run a search there and then revert back to its home and deliver its results to the original DefinitionChecker which will incorporate them into its own result set, re-sort it and display the whole lot.
Sorry, for instruction 2) above [yes, the second one :0)] the security policy file should read as follows :

grant
 codeBase "file:///-/"
{
  // can do anything
  permission java.security.AllPermission "*", "*";
};

grant
 codeBase "file://e:\\java\\aglets\\public\\DefinitionChecker"
{
  // can do anything
  permission java.security.AllPermission "*", "*";
};

grant
 codeBase "atp://*:*/"
{
  // can do anything
  permission java.security.AllPermission "*", "*";
};

grant
 codeBase "http://*:*/"
{
  // can do anything
  permission java.security.AllPermission "*", "*";
};

This is a completly open and unsecure policy so it should only be used for tseting.
/*
*
* UI1.java : this is the GUI for the DefinitionChecker class.
*
*/


import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.Vector;
import java.net.URL;

import com.ibm.aglet.*;
import com.ibm.aglet.system.*;

import definitions.*;

public class UI1 extends Aglet implements ActionListener{

      /*
      * UI Components
      */
      transient JFrame frame;
      DefinitionChecker dc;
      TextField search = new TextField(18);
      Label searchlab = new Label("Search for");
      Scrollbar bar = new Scrollbar();
      JTable table;
      JScrollPane scroller;
      Vector columns = new Vector();
      Vector rows = new Vector();
      Button go = new Button("Go...");
      Button send = new Button("Send Clone");
      Button close = new Button("Close");
      Button clear = new Button("Clear");
      Panel Resultpanel = new Panel();
      Panel Buttonpanel = new Panel();
      Panel Inputpanel = new Panel();
      Panel checkbox = new Panel();

      CheckboxGroup cbg1 = new CheckboxGroup();
      Checkbox ran = new Checkbox("Random",cbg1,false);
      Checkbox seq = new Checkbox("Normal",cbg1,true);
      Checkbox sseq = new Checkbox("Strict",cbg1,false);

      CheckboxGroup cbg2 = new CheckboxGroup();
      Checkbox full = new Checkbox("Full",cbg2,false);
      Checkbox fast = new Checkbox("Fast",cbg2,true);

      public UI1(DefinitionChecker dc){

            this.dc = dc;
            frame = new JFrame("Aglet Interface Example");
            frame.addWindowListener(new WindowAdapter(){
                  public void windowClosing(WindowEvent e){
                        frame.hide();
                  }
            });

            GridBagLayout gridbag = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            Container content = frame.getContentPane();
            content.setLayout(gridbag);

            c.insets = new Insets(3,3,3,3);

            c.fill = GridBagConstraints.NONE;
            c.gridx = 0;
            c.gridy = 0;
            c.gridwidth = 1;
            c.weightx = 0.0;
            c.anchor = GridBagConstraints.NORTHWEST;
            gridbag.setConstraints (searchlab, c);
            content.add(searchlab);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 0;
            c.gridwidth = 1;
            c.weightx = 1.0;
            c.anchor = GridBagConstraints.NORTHEAST;
            gridbag.setConstraints(search, c);
            content.add(search);

            c.fill = GridBagConstraints.BOTH;
            c.gridx = 0;
            c.gridy = 1;
            c.gridwidth = 2;
            c.weightx = 1.0;
            c.weighty = 1.0;
            c.anchor = GridBagConstraints.CENTER;
            columns.add("Rank");
            columns.add("Text");
            columns.add("S1");
            columns.add("S2");
            columns.add("Host");
            columns.add("File");
            table = new JTable(rows,columns);
            scroller = new JScrollPane(table);
            gridbag.setConstraints(scroller, c);
            content.add(scroller);

            c.weighty = 0.0;
            c.fill = GridBagConstraints.NONE;
            c.gridx = 0;
            c.gridy = 2;
            c.gridwidth = 2;
            c.weightx = 1.0;
            c.anchor = GridBagConstraints.CENTER;
            checkbox.setLayout(new GridLayout(1,8));
            checkbox.add(new Label("Search : "));
            checkbox.add(ran);
            checkbox.add(seq);
            checkbox.add(sseq);
            checkbox.add(new Label("         "));
            checkbox.add(new Label("Scoring : "));
            checkbox.add(full);
            checkbox.add(fast);
            gridbag.setConstraints(checkbox, c);
            content.add(checkbox);

            c.fill = GridBagConstraints.NONE;
            c.gridx = 0;
            c.gridy = 3;
            c.gridwidth = 2;
            c.weightx = 1.0;
            c.anchor = GridBagConstraints.CENTER;

            Buttonpanel.setLayout(new GridLayout(1,7));
            Buttonpanel.add(go);
            Buttonpanel.add(new Label("    "));
            Buttonpanel.add(clear);
            Buttonpanel.add(new Label("    "));
            Buttonpanel.add(close);
            Buttonpanel.add(new Label("    "));
            Buttonpanel.add(send);
            gridbag.setConstraints(Buttonpanel, c);
            content.add(Buttonpanel);

            go.addActionListener(this);
            send.addActionListener(this);
            close.addActionListener(this);
            clear.addActionListener(this);

            KeyListener kl = new KeyListener() {
                  public void keyPressed(KeyEvent e) {}

                  public void keyReleased(KeyEvent e) {
                        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                              System.out.println(search.getText());
                        }
                  }

                  public void keyTyped(KeyEvent e) {}
            };

            search.addKeyListener(kl);
      }

      public void showDialog(){
            if (frame != null){
                  frame.pack();
                  frame.resize(frame.preferredSize());
                  frame.reshape(20,20,600,400);
                  setColumnWidths();
                  frame.show();
            }
      }

      public static void main(String args[]){
            DefinitionChecker dc = new DefinitionChecker();
            UI1 agletFrame = new UI1(dc);
            agletFrame.showDialog();
      }

      private boolean validSearch(String s){

            // if the search term list less that 7 characters it can't be valid
            if (s.length() < 7){
                  return false;
            }
            // if the search term does not have a space it can't be valid
            if (s.indexOf(" ") == -1){
                  return false;
            }
            // if any of the search terms words are less than 3 characters
            // it can't be valid.
            StringTokenizer st = new StringTokenizer(s);
            while (st.hasMoreTokens()){
                  if (st.nextToken().length() < 3){
                        return false;
                  }
            }
            return true;
      }

      private void showMsg(String msg){
            JOptionPane.showMessageDialog(frame,msg);
      }

      public void actionPerformed(ActionEvent event){

            if (event.getSource() == go){
                  if (validSearch(search.getText())){
                        int mode = 2;
                        if (cbg1.getSelectedCheckbox() == ran){
                              mode = 1;
                        }else if(cbg1.getSelectedCheckbox() == seq){
                              mode = 2;
                        }else{
                              mode = 3;
                        }
                        boolean quick = true;
                        if (cbg2.getSelectedCheckbox() == full){
                              quick = false;
                        }
                        dc.setQuickScoring(quick);
                        dc.setMatchMode(mode);
                        dc.setKeyString(search.getText());
                        dc.check();
                  }else{
                        showMsg("You must provide a valid search term.\n\nA valid search term must have a minimum of two words\neach of which must have at least three chracaters.");
                  }
            }else if (event.getSource() == close){
                  frame.hide();
            }else if(event.getSource() == send){
                  if (validSearch(search.getText())){
                        int mode = 2;
                        if (cbg1.getSelectedCheckbox() == ran){
                              mode = 1;
                        }else if(cbg1.getSelectedCheckbox() == seq){
                              mode = 2;
                        }else{
                              mode = 3;
                        }
                        boolean quick = true;
                        if (cbg2.getSelectedCheckbox() == full){
                              quick = false;
                        }
                        dc.setQuickScoring(quick);
                        dc.setMatchMode(mode);
                        dc.setKeyString(search.getText());
                        try{
                              dc.sendClone(new URL("atp://localhost:5434/"));
                        }catch(Exception e){
                              showMsg(e.getMessage());
                        }
                  }else{
                        showMsg("You must provide a valid search term.\n\nA valid search term must have a minimum of two words\neach of which must have at least three chracaters.");
                  }
            }else if(event.getSource() == clear){
                  rows = new Vector();
                  table.setModel(new DefaultTableModel(rows,columns));
                  setColumnWidths();
                  dc.clearMatches();
            }
      }

      public void doUpdates(Sentence[] sentences){
            rows = new Vector();
            for (int i = 0; i < sentences.length; i++){
                  Vector v = new Vector();
                  v.add(Integer.toString(i+1));
                  v.add(sentences[i].getSentence());
                  v.add(Integer.toString(sentences[i].getKeyScore()));
                  v.add(Integer.toString(sentences[i].getPatternScore()));
                  v.add(sentences[i].getHostingURL().getHost() + ":" + sentences[i].getHostingURL().getPort());
                  v.add(sentences[i].getLocation().toString());
                  rows.add(v);
            }
            table.setModel(new DefaultTableModel(rows,columns));
            setColumnWidths();
      }

      private void setColumnWidths(){
            table.getColumnModel().getColumn(0).setPreferredWidth(15);
            table.getColumnModel().getColumn(1).setPreferredWidth(350);
            table.getColumnModel().getColumn(2).setPreferredWidth(15);
            table.getColumnModel().getColumn(3).setPreferredWidth(15);
            table.getColumnModel().getColumn(4).setPreferredWidth(75);
            table.getColumnModel().getColumn(5).setPreferredWidth(75);
      }
}
/*
* DefinitionChecker.java
*
*/

import com.ibm.aglet.*;
import com.ibm.aglet.system.*;
import com.ibm.aglet.event.*;

import java.util.Vector;
import java.util.StringTokenizer;
import java.io.*;
import java.net.URL;
import definitions.*;

public class DefinitionChecker extends Aglet{


      /*
      *
      * These are some static WordLists which can be used to create
      * the WordPatterns that this PatterMatcher will use
      *
      */
      private static WordList list1 = new WordList("is,was,are,be",",",false);
      private static WordList list2 = new WordList("described,defined,delimited",",",true);
      private static WordList list3 = new WordList("as,by",",",false);
      private static WordList list4 = new WordList("is",",",false);
      private static WordList list5 = new WordList("the",",",false);

      private String keyword;
      private String[] files = new String[]{"e:\\java\\aglets\\public\\a1.txt","e:\\java\\aglets\\public\\b1.txt","e:\\java\\aglets\\public\\c1.txt","e:\\java\\aglets\\public\\d1.txt"};
      private Sentence[] sentences;
      private Vector matches = new Vector();
      private boolean quick = true;
      private int matchMode = WordPattern.NORMAL_MATCH;
      private String keyString = "";
      private boolean atHome = true;
      private URL home;

      private AgletProxy myParent;

      private PatternMatcher pm;
      private PatternMatcher km;
      private UI1 gui = null;


      /**
      *
      * Constructor for the DefinitionChecker
      *
      */
      //public DefinitionChecker(int matchMode, boolean quick){
      public void onCreation(Object init){

            if (init != null){
                  myParent = (AgletProxy) init;
            }

            home = getAgletContext().getHostingURL();
            addMobilityListener(new MobilityAdapter(){
                  public void onArrival(MobilityEvent me){
                        if (atHome){
                              atHome = false;
                              System.out.println("I'm out on the prowl...");
                              check();
                        }else{
                              atHome = true;
                              System.out.println("Honey, I'm home and I found " + matches.size() + " matches.");
                              if (myParent != null & matches.size() > 0){
                                    try{
                                          DefinitionChecker dc = (DefinitionChecker)myParent.getAglet();
                                          dc.addMatches(matches);
                                          dispose();
                                    }catch(Exception e){
                                          e.printStackTrace();
                                    }
                              }
                        }
                  }
            });

            // let's build our PatternMatcher
            pm = new PatternMatcher();

            // create a WordPattern
            WordPattern pattern1 = new WordPattern();
            // add the appropriate WordLists
            pattern1.addList(list1);
            pattern1.addList(list2);
            pattern1.addList(list3);
            // add the WordPattern to the vector
            pm.addPattern(pattern1);

            // create a WordPattern
            WordPattern pattern2 = new WordPattern();
            // add the appropriate WordLists
            pattern2.addList(list4);
            pattern2.addList(list5);
            // add the WordPattern to the vector
            pm.addPattern(pattern2);

      }

      public void dialog(Message msg){
            if (gui == null){
                  gui = new UI1(this);
            }
            gui.showDialog();
      }

      public void run(){
            //gui.showDialog();
      }

      public void sendClone(URL destination) throws Exception{
            AgletContext ac = getAgletContext();
            AgletProxy thisProxy = getProxy();
            AgletProxy proxy = ac.createAglet(null, "DefinitionChecker", thisProxy);
            DefinitionChecker dc = (DefinitionChecker)proxy.getAglet();
            dc.setMatchMode(this.matchMode);
            dc.setQuickScoring(this.quick);
            dc.setKeyString(this.keyString);
            proxy = proxy.dispatch(destination);
      }

      public void addMatches(Vector v){
            for (int i = 0; i < v.size(); i++){
                  matches.add(v.elementAt(i));
            }
            sortMatches();
            gui.doUpdates(getMatchedSentences());
      }

      public void setKeyString(String s){
            keyString = s;
      }

      public String getKeyString(){
            return keyString;
      }

      public boolean handleMessage(Message msg) {
            if (msg.sameKind("dialog")) {
                  dialog(msg);
            }else{
                  return false;
            }
            return true;
      }

      public void setMatchMode(int mode){
            this.matchMode = mode;
      }

      public void setQuickScoring(boolean quick){
            this.quick = quick;
      }

      public void check(){

            // let's build a PatternMatcher to hold our keyword pattern
            // and use a StemmedWordList to do so.
            matches = new Vector();
            km = new PatternMatcher();
            WordPattern keyPattern = new WordPattern();
            StringTokenizer st = new StringTokenizer(keyString);
            while (st.hasMoreTokens()){
                  keyPattern.addList(new WordList(st.nextToken()," ",true));
            }
            km.addPattern(keyPattern);

            // loop through each file in the list of files
            for (int f = 0; f < files.length; f++){
                  File file = null;
                  try{
                        // get all the sentences
                        file = new File(files[f]);
                        sentences = getSentencesFromFile(file);
                  }catch(IOException ioe){
                        System.out.println(ioe);
                  }
                  // loop through all the sentences
                  for (int i = 0; i < sentences.length; i++){
                        // if any sentence contains the keyword and matches any of the patterns specified in the PatternMatcher
                        int keyScore = km.scoreSentence(sentences[i],WordPattern.STRICT_MATCH,false,false);
                        int patternScore = pm.scoreSentence(sentences[i],matchMode,quick,false);
                        if (keyScore > 0 && patternScore > 0){
                              sentences[i].setKeyScore(keyScore);
                              sentences[i].setPatternScore(patternScore);
                              // if this is the first match found in this file
                              matches.add(sentences[i]);
                        }
                  }
            }
            if (atHome){
                  sortMatches();
                  gui.doUpdates(getMatchedSentences());
            }else{
                  try{
                        URL u = null;
                        dispatch(home);
                  }catch(Exception e){
                        System.out.println("Poor little aglet unable to return home...");
                  }
            }
      }

      private void sortMatches(){
            Object[] o = matches.toArray();
            java.util.Arrays.sort(o);
            matches = new Vector();
            for (int i = 0; i < o.length; i++){
                  matches.add(o[i]);
            }
      }

      /**
      * getMatches()
      *
      * Returns an array of strings which are all the matched sentences found by the DefinitionChecker.
      *
      */
      public String[] getMatches(){
            String[] m = new String[matches.size()];
            for (int i = 0; i < matches.size(); i++){
                  m[i] = ((Sentence)matches.elementAt(i)).toString();
            }
            return m;
      }

      public void clearMatches(){
            matches = new Vector();
      }

      /**
      * getMatchedSentences()
      *
      * Returns an array of sentences which are all the matched sentences found by the DefinitionChecker.
      *
      */
      public Sentence[] getMatchedSentences(){
            Sentence[] s = new Sentence[matches.size()];
            s = (Sentence[])matches.toArray(s);
            return s;
      }

      /**
      *
      * GetArrayFromFile
      *
      * This function reads a specified file and breaks the contents into
      * and array of strings (sentences) using the # character as a delimiter
      *
      */
      private Sentence[] getSentencesFromFile(File f) throws IOException{
            FileReader reader = new FileReader(f);
            Vector sentences = new Vector();
            char[] cbuf = new char[1];
            String delimiter = "#";
            String sentence = "";
            String c = "";
            // read the file character by character
            while (reader.read(cbuf) != -1){
                  c = new String(cbuf);
                  // if the chracter is a delimiter (#)
                  if (c.equals(delimiter)){
                        // add the sentence to the Vector and start a new blank sentence
                        Sentence s = new Sentence(sentence);
                        s.setLocation(f);
                        s.setHostingURL(getAgletContext().getHostingURL());
                        sentences.add(s);
                        sentence = "";
                  }else{
                        // otherwise just add the character to the current sentence string
                        sentence += c;
                  }
            }
            reader.close();
            Sentence[] sentenceArray = new Sentence[sentences.size()];
            // convert the Vector to an array and return it
            sentenceArray = (Sentence[])sentences.toArray(sentenceArray);
            return sentenceArray;
      }

/*
// this  main method has been commeneted out as it seems to stop this class being instantiated properly as an aglet

      public static void main(String[] args){

            int matchMode = WordPattern.NORMAL_MATCH;
            boolean quick = true;
            String s = "";
            int numKeywords = 0;
            // first lets check what the arguments are
            for (int i = 0;i < args.length;i++){
                  //if any of them are -? then we print the usage message
                  if (args[i].equalsIgnoreCase("-?")){
                        printUsage("");
                        System.exit(1);
                  }
                  //if any of them are -q then we are in quick mode
                  if (args[i].equalsIgnoreCase("-q")){
                        quick = true;
                        continue;
                  }
                  //if any of them are -f then we are in full mode
                  if (args[i].equalsIgnoreCase("-f")){
                        quick = false;
                        continue;
                  }
                  //if any of them are -s then we are in strict mode
                  if (args[i].equalsIgnoreCase("-s")){
                        matchMode = WordPattern.STRICT_MATCH;
                        continue;
                  }
                  //if any of them are -r then we are in random mode
                  if (args[i].equalsIgnoreCase("-r")){
                        matchMode = WordPattern.RANDOM_MATCH;
                        continue;
                  }
                  //if any of them are -r then we are in normal mode
                  if (args[i].equalsIgnoreCase("-n")){
                        matchMode = WordPattern.NORMAL_MATCH;
                        continue;
                  }
                  // make sure they are all 3 chracaters or longer
                  if (args[i].length() < 3){
                        printUsage("Input Error : " + args[i] + "\nAll component words of the SearchTerm must be three characters or more.");
                        System.exit(1);
                  }
                  // concatenate the arguments into one search string
                  s = s + args[i] + " ";
                  numKeywords++;
            }
            // now make sure that we have at least two valid keywords
            if (numKeywords < 2){
                  printUsage("");
                  System.exit(1);
            }
            s = s.trim();
            // finally instantiate a DefinitionChecker and pass it the string and tell it which match mode to use
            DefinitionChecker dc = new DefinitionChecker(matchMode,true);
            dc.check(s);
            String[] matches = dc.getMatches();
            for (int m = 0; m < matches.length; m++){
                  System.out.println(matches[m]);
            }

      }

      private static void printUsage(String msg){
            if (msg.length() > 0){
                  System.out.println("\n" + msg);
            }
            System.out.println("\nUSAGE : DefintionChecker [Mode] [Scoring] SearchTerm\n\n\tMode Options :\n\t-r\trandom pattern matching\n\t-n\tnormal pattern matching (default)\n\t-s\tstrict pattern matching\n\n\tScoring Options :\n\t-q\tquick scoring (default)\n\t-f\tfull scoring\n\n\tSearchTerm : \n\tA minimum of 2 words each consisting of 3 chracters\n\tor more must be provided to make a valid SearchTerm.");
      }
*/
}
/*
* PatternMatcher.java
*
*/

package definitions;

import java.util.StringTokenizer;
import java.util.Vector;
import com.ibm.aglet.*;

public class PatternMatcher extends Aglet{

     private Vector patterns;

     /**
     *
     * Constructor for the PatternMatcher. This adds the
     * WordPatterns to the PatternMatchers list of patterns
     * ready for matching.
     *
     */
     public PatternMatcher(){
          // create the vector to store our WordPatterns
          patterns = new Vector();
     }

     /**
     *
     * This is just a function for adding WordPatterns
     * to the PatternMatcher. It's not used currently
     * but it will probably come in handy.
     */
     public void addPattern(WordPattern pattern){
          patterns.add(pattern);
     }

     /**
     *
     * This is the key function on the PatternMatcher. It is
     * passed a String (sentence) and information on "strictnesss".
     * It thens cycles through all its patterns seeing if any of them
     * are found in the sentence.
     *
     */
     public int scoreSentence(Sentence s, int matchMode, boolean quick, boolean all){

          // loop through all the WordPatterns checking to see if
          // any of them match the sentence.
          int hiScore = 0;
          for (int i = 0; i < patterns.size();i++){
               int score = 0;
               WordPattern wp = (WordPattern)patterns.elementAt(i);
               if ((score = wp.containsPattern(s,matchMode,all)) > 0){
                    if (quick){
                         return score;
                    }else{
                         hiScore += score;
                    }
               }
          }
          return hiScore;
     }

}
/**
*
* WordPattern.java
*
* This class contains the core of the "comparison logic". Each WordPattern
* contains one or more word lists which it uses in sequence to do a word by
* word comparison with the sentence provided.
*
*/

package definitions;

import java.util.Vector;
import java.util.StringTokenizer;
import com.ibm.aglet.*;


public class WordPattern extends Aglet{

      /*
      *
      * Some static integers to denote the various modes
      * available for pattern matching
      */
      public final static int STRICT_MATCH = 3;
      public final static int NORMAL_MATCH = 2;
      public final static int RANDOM_MATCH = 1;

      private Vector lists;

      /**
      *
      * This constructor takes an array of WordLists
      * and uses them to populate its own Vector
      * of WordLists
      */
      public WordPattern(WordList[] wl){
            lists = new Vector();
            for (int i = 0; i < wl.length; i++){
                  lists.add(wl[i]);
            }
      }

      /**
      *
      * This constructor simply initialises a blank Vector
      * to be used to store the WordLists which can be added
      * using the addList() method
      */
      public WordPattern(){
            lists = new Vector();
      }

      /**
      *
      * This function adds a WordList to the Word Pattern
      *
      */
      public void addList(WordList list){
            lists.add(list);
      }

      /**
      *
      * This function does all the real work. It breaks the supplied
      * String into iuts component words and then compares them either
      * strictly or not, to the words in the WordLists.
      *
      */
      public int containsPattern(Sentence s, int matchMode, boolean all){
            //System.out.println(s);
            String[] words = s.getWordArray();
            int totalScore = 0;
            int score = 0;
            int stop = 0;
            if (!all){
                  stop = (matchMode - 1);
            }
            // if there are less words that lists then the sentence cannot
            // possibly contain a full pattern, so return false
            if (words.length < lists.size()){
                  return 0;
            }
            for (int m = matchMode; m > stop; m--){
                  totalScore = 0;
                  // this counter will hold the number of words matched
                  int count = 0;
                  // this counter will hold the number of words matched contiguously (i.e. in strict sequence)
                  int sequence = 0;
                  // this value will tell us whether the previous word was a match
                  boolean inSequence = false;
                  // simultaneously loop through the array of words and the Vector
                  // of WordLists, starting by comparing the first word with the first WordList
                  for (int l = 0, w = 0; ((l < lists.size()) && (w < words.length));){
                        WordList wordlist = (WordList)lists.elementAt(l);
                        String word = words[w];
                        // if the wordlist contains the word then we can move to the next wordlist
                        // and to the next word in the word array, unless we are in random mode.
                        // If we are in random mode, we move back to the beginning of the word array
                        // and start checking from the beginning becuase the words can appear in any order.
                        if ((score = wordlist.containsWord(word)) > 0){
                              totalScore += score;
                              //System.out.println(word + " : scores : " + score + " : total = " + totalScore);
                              l++;
                              if (m == RANDOM_MATCH){
                                    w = 0;
                              }else{
                                    w++;
                              }
                              count++;
                              // if we are are in sequence (i.e. the previous word was a match
                              // then we increment the number of seqential words found
                              if (inSequence || sequence == 0){
                                    sequence++;
                              }
                              // set the value to indicate that this word was matched
                              inSequence = true;
                        }else{
                              // if the wordlist does not contain the word then we can move to the next word
                              // but we do not move to the next wordlist
                              w++;
                              // if we are in strict mode and had started a sequence but not finished it then
                              // we may as well abandon it and start with the  first list again just in case
                              // there is a full sequence later in the sentence.
                              if (m == STRICT_MATCH && inSequence && sequence < lists.size()){
                                    l = 0;
                                    w--;
                                    sequence = 0;
                                    count = 0;
                                    totalScore = 0;
                              }
                              // set the value to indicate that we are no longer in strict sequence
                              inSequence = false;
                        }
                  }

                  // if the number of words matched is the same as the number of lists
                  // then we have a match
                  if (count == lists.size()){
                        switch (m){
                              case STRICT_MATCH:
                                    if(sequence == lists.size()){
                                          //System.out.println("strict : scored " + totalScore);
                                          return totalScore + 3;
                                    }else{
                                          totalScore = 0;
                                    }
                                    break;
                              case NORMAL_MATCH:
                                    //System.out.println("normal : scored " + totalScore);
                                    return (totalScore + 2);
                              case RANDOM_MATCH:
                                    //System.out.println("random : scored " + totalScore);
                                    return (totalScore + 1);
                        }
                  }
            }
            //System.out.println("fail : scored " + totalScore);
            return 0;
      }

      /**
      *
      * This function returns the length of the longest word list.
      * It's not used at the moment but may be useful
      *
      */
      public int maxListLength(){
            int length = 0;
            for (int l = 0; l < lists.size(); l ++){
                  if (((WordList)lists.elementAt(l)).numWords() > length){
                        length = ((WordList)lists.elementAt(l)).numWords();
                  }
            }
            return length;
      }

}
/**
*
* WordList.java
*
* This class holds an array of strings (words) which
* can be combined in a WordPattern with other WordLists
*
*/

package definitions;

import java.util.StringTokenizer;
import java.util.Vector;
import com.ibm.aglet.*;

public class WordList extends Aglet{

     private Vector words;
     private Stemmer stemmer;
     private boolean stemming = false;

     /**
     *
     * This constructor takes a string and a delimiter string
     * and then uses a StringTokenizer to break the string into
     * an array of words
     */
     public WordList(String s, String delimiter, boolean stem){
          if (stem){
               stemmer = new Stemmer();
               stemming = true;
          }
          StringTokenizer st = new StringTokenizer(s,delimiter);
          words = new Vector();
          while (st.hasMoreTokens()){
               words.add(st.nextToken());
          }
     }

     /**
     *
     * This is just an accessor function that lets you get the words
     * held in the list. Not used at the moment, but probably useful
     * for debugging.
     */
     public String[] getWords(){
          String[] wordArray = new String[words.size()];
          wordArray = (String[])words.toArray(wordArray);
          return wordArray;
     }

     /**
     *
     * This is just an accessor function that lets you get the number of
     * words held in the list. Not used at the moment, but probably useful
     * for debugging.
     */
     public int numWords(){
          return words.size();
     }

     /**
     *
     * This function takes a string (word) and checks to
     * see if it matches any of the words in its list.
     */
     public int containsWord(String s){
          //System.out.println("Looking for " + s + " in :");
          //this.print();
          String word1 = s.trim();
          for (int i = 0; i < words.size(); i++){
               String word2 = (String)words.elementAt(i);
               if (word1.equalsIgnoreCase(word2)){
                    //System.out.println("match : "+ word1 + " : " + word2);
                    return 2;
               }
          }
          if (stemming){
               word1 = stemmer.getStem(word1);
               for (int i = 0; i < words.size(); i++){
                    String word2 = stemmer.getStem((String)words.elementAt(i));
                    if (word1.equalsIgnoreCase(word2)){
                         //System.out.println("stem match : "+ word1 + " : " + word2);
                         return 1;
                    }
               }
          }
          return 0;
     }

     /**
     *
     * This is just an accessor function that prints out the words
     * held in the list. Not used at the moment, but probably useful
     * for debugging.
     */
     public void print(){
          for (int i = 0; i < words.size(); i++){
               System.out.println((String)words.elementAt(i));
          }
     }

     public boolean isStemming(){
          return stemming;
     }

}
/**
*
* Sentence.java
*
*/

package definitions;

import java.util.StringTokenizer;
import java.io.File;
import java.net.URL;
import com.ibm.aglet.*;

public class Sentence  extends Aglet implements Comparable{

     private String sentence;
     private int keyScore;
     private int patternScore;
     private File location;
     private URL host;

     public Sentence(String s){
          sentence = s;
          keyScore = 0;
          patternScore = 0;
     }

     public String[] getWordArray(){
          int token = 0;
          StringTokenizer st = new StringTokenizer(sentence);
          String[] words = new String[st.countTokens()];
          while (st.hasMoreTokens()){
               words[token++] = st.nextToken();
          }
          return words;
     }

     public int getScore(){
          return (keyScore + patternScore);
     }

     public URL getHostingURL(){
          return host;
     }

     public void setHostingURL(URL host){
          this.host = host;
     }

     public int getKeyScore(){
          return keyScore;
     }

     public void setKeyScore(int s){
          keyScore = s;
     }

     public void addKeyScore(int s){
          keyScore += s;
     }

     public int getPatternScore(){
          return patternScore;
     }

     public void setPatternScore(int s){
          patternScore = s;
     }

     public void addSPatterncore(int s){
          patternScore += s;
     }

     public void setLocation(File f){
          location = f;
     }

     public File getLocation(){
          return location;
     }

     public int compareTo(Object o){
          int c = ((Sentence)o).getScore();
          if (c == this.getScore()){
               return 0;
          }else{
               return (c - this.getScore());
          }
     }

     public String getSentence(){
          return sentence;
     }

     public String toString(){
          return sentence + "[" + keyScore + "][" + patternScore + "](" + location.toString() + ")";
     }
}
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ill open up a new thread "Title: Search:- 4 For Mr ozymandias"