0

我使用以下代码通过共享菜单将文本从其他应用程序发送到我的应用程序,并在 EditText 中显示文本。

Intent receivedIntent = getIntent();    
    String receivedAction = receivedIntent.getAction();     
    String receivedType = receivedIntent.getType();
    TextView txtView = (EditText) findViewById(R.id.edWord);
    //if(receivedAction.equals(Intent.ACTION_SEND)){
    if (Intent.ACTION_SEND.equals(receivedAction) && receivedType != null) {
        if(receivedType.startsWith("text/")) {                                      
            String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT).toLowerCase();  
            if (receivedText != null)
            {                   
                txtView.setText(receivedText);
                txtView.requestFocus();
                ListView myList=(ListView) findViewById(R.id.lstWord);
                myList.setFocusableInTouchMode(true);
                myList.setSelection(0);
            }
            else
                txtView.setText(""); 
        } 
    }  

一切正常,即发送的文本显示在我的 EditText 中(即edWord在上面的代码中)。但问题是通过 Share 发送的文本有时包含无意义的元素或派生词,例如:"word, word',word,looked, books, tomatoes

现在我想要的是格式化文本,使其在添加到 EditText 之前只包含真实单词或单词的基本形式。

我听说过approximate string matching或者fuzzy searching但我不知道如何将它应用到我的代码中。我想知道您是否可以给我一点帮助来解决上述问题,至少在格式化/剥离非单词元素方面。

提前致谢。

4

1 回答 1

0

我想我已经找到了问题第一部分的答案,即从字符串中删除非单词元素(开始和/或结束字符串)。这是我使用的带有一些正则表达式算法的代码:

String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);  
            if (receivedText != null)                
          {                 
                receivedText = receivedText.toLowerCase();

                //Remove all non-word elements starting and/or ending a string 
                String strippedInput = receivedText.replaceAll("^\\W+|\\W+$", "");
                System.out.println("Stripped string: " + strippedInput); 

                txtView.setText(strippedInput);
                txtView.requestFocus();
                ListView myList=(ListView) findViewById(R.id.lstWord);
                myList.setFocusableInTouchMode(true);
                myList.setSelection(0);
            }

对于我的问题的第二部分,关于模糊搜索,我猜它或多或少涉及重新编码我的应用程序如何从其 SQLlite 数据库中搜索结果。这仍然是我未回答的问题。

于 2013-04-19T05:08:45.233 回答