2

这是我的字典格式:

词频

走了60

去 10

去 30

到目前为止,系统会返回单词,例如以“g”开头的 go30、gos10、goone60 作为列表。(按字母顺序)。我想提高系统的准确性,以便搜索结果基于频率。高频词首先出现。请帮助。

这是逐行读取字典的 Text midlet 类。

      public class Text extends MIDlet {
     // Fields 

private static final String[] DEFAULT_KEY_CODES = {
    // 1
    ".,?!'\"1-()@/:_",
    // 2
    "ABC2",
    // 3
    "DEF3",
    // 4
    "GHI4",
    // 5
    "JKL5",
    // 6
    "MNO6",
    // 7
    "PQRS7",
    // 8
    "TUV8",
    // 9
    "WXYZ9",
};

//Initializing inner Classes
public ComposeText() {
    cmdHandler = new CommandHandler();
    lineVector = new Vector();
}

//Calling All InitMethods, setting Theme, Show MainForm
public void startApp() {
    Display.init(this);
    setTheme();
    initCmd();
    initMainGui();
    mainFrm.show();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

//Initializing all the Commands
public void initCmd() {
    exitCmd = new Command("Exit");
    selectCmd = new Command("Ok");
    cancelCmd = new Command("Cancel");
    predCmd = new Command("Prediction");
    sendCmd = new Command("Send");

    tfPredArea = new TextField();

         //check dictionary
    try {
            readFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
}

//Initiating MainScreen
public void initMainGui() {
    mainFrm = new Form("Compose Text");
    mainFrm.setLayout(new BorderLayout());
    mainFrm.setLayout(new CoordinateLayout(150, 150));
    mainFrm.addCommand(exitCmd);
    mainFrm.addCommand(predCmd);
    mainFrm.addCommand(sendCmd);
    mainFrm.addCommandListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            if(ae.getSource() == predCmd){
                            initPredGui();
            } else if(ae.getSource() == exitCmd){
            destroyApp(true);
            notifyDestroyed();
            }
        }
    });
    // To : 07xxxxxxxxxx
    Dimension d1 = new Dimension(130, 20);
    lbTo = new Label("To:");
    lbTo.setX(10);
    lbTo.setY(10);
    tfTo = new TextField();
    tfTo.setReplaceMenu(false);
    tfTo.setConstraint(TextField.NUMERIC);
    tfTo.setInputModeOrder(new String[]{"123"});
    tfTo.setMaxSize(13);
    tfTo.setX(40);
    tfTo.setY(10);
    tfTo.setPreferredSize(d1);

    //Message : Compose Text
    Dimension d2 = new Dimension(135, 135);
    lbSms = new Label("Message:");
    lbSms.setX(5);
    lbSms.setY(40);
    tfSms = new TextField();
    tfSms.setReplaceMenu(false);
    tfSms.setX(40);
    tfSms.setY(40);
    tfSms.setPreferredSize(d2);

    //add stuff
    mainFrm.addComponent(lbTo);
    mainFrm.addComponent(lbSms);
    mainFrm.addComponent(tfTo);
    mainFrm.addComponent(tfSms);
  }
  //Initiating FilterSelection Screen
  public void initPredGui() {
    predForm = new Form("Prediction on");           
    predForm.setLayout(new CoordinateLayout(150, 150));
    predForm.addCommand(cancelCmd);
    predForm.addCommand(selectCmd);

    //textfied in prediction form
    final Dimension d5 = new Dimension(200, 200);
    tfPredArea = new TextField();
    tfPredArea.setReplaceMenu(false);
    tfPredArea.setX(10);
    tfPredArea.setY(10);
    tfPredArea.setPreferredSize(d5);

    predForm.addComponent(tfPredArea);

    final ListModel underlyingModel = new DefaultListModel(lineVector);
  //        final ListModel underlyingModel = new           

  DefaultListModel(tree.getAllPrefixMatches(avail));

  // this is a list model that can narrow down the underlying model
    final SortListModel proxyModel = new SortListModel(underlyingModel);

    final List suggestion = new List(proxyModel);


    tfPredArea.addDataChangeListener(new DataChangedListener() {

        public void dataChanged(int type, int index) {

            int len = 0;
            int i = 0;
            String input = tfPredArea.getText();
            len = tfPredArea.getText().length();

            //ensure start search character is set for each word 
            if (!(len == 0)) {
                for (i = 0; i < len; i++) {
                    if (input.charAt(i) == ' ') {
                        k = i;
                    }     
                }
                String currentInput = input.substring(k + 1, len);
                proxyModel.filter(currentInput);

            }
        }
        });

        Dimension d3 = new Dimension(110, 120);
        suggestion.setX(80);
        suggestion.setY(80);
        suggestion.setPreferredSize(d3);

        predForm.addComponent(suggestion);

        suggestion.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                    String string = suggestion.getSelectedItem().toString();
                if (tfPredArea.getText().charAt(0) == 0) {
                    tfPredArea.setText(string);
                }
               else if (tfPredArea.getText().length() == 0) {
                    tfPredArea.setText(string);
                } else {
                    tfPredArea.setText(tfPredArea.getText() + string);
                }

            }
    });
        predForm.addCommandListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == addCmd) {
                newDictionaryFrm.show();
            } else {
                mainFrm.show();
            }
        }
    });
         predForm.show();
   }

  //Setting Theme for All Forms
   public void setTheme() {
    try {
        Resources r = Resources.open("/theme.res");
        UIManager.getInstance().setThemeProps(r.getTheme(
                r.getThemeResourceNames()[0]));
    } catch (java.io.IOException e) {
        System.err.println("Couldn't load the theme");
    }
  }

  //Inner class CommandHandler
  public class CommandHandler implements ActionListener {

    public void actionPerformed(ActionEvent ae) {
        //cancelCommand from predictionForm
        if (ae.getSource() == cancelCmd) {
            if (edit) {
                mainFrm.show();
    //                    clearFields();
            } else if (ae.getSource() == selectCmd){
                tfPredList.addDataChangeListener(model);
                predForm.show();
            }
            else{}
        }
    }
 }

 // method that reads dictionary line by line
 public void readFile() throws IOException {
    tree = new Trie();
    InputStreamReader reader = new InputStreamReader(
            getClass().getResourceAsStream("/Maa Corpus.txt-01-ngrams-Alpha.txt"));
    String line = null;
    // Read a single line from the file. null represents the EOF.
    while ((line = readLine(reader)) != null) {
        // Append to a vector to be used as a list
        lineVector.addElement(line);
    }
  }

  public String readLine(InputStreamReader reader) throws IOException {
    // Test whether the end of file has been reached. If so, return null.
    int readChar = reader.read();
    if (readChar == -1) {
        return null;
    }
    StringBuffer string = new StringBuffer("");
    // Read until end of file or new line

    while (readChar != -1 && readChar != '\n') {
        // Append the read character to the string.
        // This is part of the newline character
        if (readChar != '\r') {
            string.append((char) readChar);
        }
        // Read the next character
        readChar = reader.read();
    }
    return string.toString();
 }
 }
}

SortListModel 类具有从文本字段 datachangeLister 获取前缀的过滤器方法

     class SortListModel implements ListModel, DataChangedListener {
   private ListModel underlying;
   private Vector filter;
   private Vector listeners = new Vector();
   public SortListModel(ListModel underlying) {
   this.underlying = underlying;
   underlying.addDataChangedListener(this);
  }

  private int getFilterOffset(int index) {
   if(filter == null) {
       return index;
   }
   if(filter.size() > index) {
       return ((Integer)filter.elementAt(index)).intValue();
   }
   return -1;
  }

 private int getUnderlyingOffset(int index) {
   if(filter == null) {
       return index;
   }
   return filter.indexOf(new Integer(index));
  }

   public void filter(String str) {
   filter = new Vector();
   str = str.toUpperCase();
   for(int iter = 0 ; iter < underlying.getSize() ; iter++) {
       String element = (String)underlying.getItemAt(iter);
       if(element.toUpperCase().startsWith(str)) // suggest only if smthing
       {
           filter.addElement(new Integer(iter));
       }
   }
   dataChanged(DataChangedListener.CHANGED, -1);
   }

  public Object getItemAt(int index) {
   return underlying.getItemAt(getFilterOffset(index));
  }

  public int getSize() {
    if(filter == null) {
       return underlying.getSize();
   }
   return filter.size();
 }

 public int getSelectedIndex() {
   return Math.max(0, getUnderlyingOffset(underlying.getSelectedIndex()));
  }

 public void setSelectedIndex(int index) {
   underlying.setSelectedIndex(getFilterOffset(index));
 }

 public void addDataChangedListener(DataChangedListener l) {
   listeners.addElement(l);
 }

 public void removeDataChangedListener(DataChangedListener l) {
   listeners.removeElement(l);
}

  public void addSelectionListener(SelectionListener l) {
    underlying.addSelectionListener(l);
  }

  public void removeSelectionListener(SelectionListener l) {
   underlying.removeSelectionListener(l);
 }

 public void addItem(Object item) {
   underlying.addItem(item);
 }

 public void removeItem(int index) {
    underlying.removeItem(index);
  }

  public void dataChanged(int type, int index) {
   if(index > -1) {
       index = getUnderlyingOffset(index);
       if(index < 0) {
           return;
       }
    }
    for(int iter = 0 ; iter < listeners.size() ; iter++) {

       ((DataChangedListener)listeners.elementAt(iter)).dataChanged(type, index);
    }
     }
  }
4

0 回答 0