【问题标题】:EditText check if has keywordEditText 检查是否有关键字
【发布时间】:2026-01-23 05:30:02
【问题描述】:

我正在制作一个待办事项应用程序,我想在用户不打字时实现自然语言检测/处理系统。最喜欢流行的待办事项应用程序(Todoist、Ticktick 等)。这应该支持不同的语言,(我可以做翻译)。

我尝试获取 EditText 中的所有单词(在键入时),然后检查写入的内容是否与存储在 strings.xml 中的关键字匹配(因此它可以处理获取一种特定语言 strings.xml 文件的翻译) ,但是,我认为这不是最合适的方法,它可能对性能不利,而且这样做很痛苦,因为我应该写下所有的同义词。

我当前的代码如下所示:

et1.addTextChangedListener(new TextWatcher() {    
    @Override    
    public void onTextChanged(CharSequence s, int start, int before, int count)    
            //Knows that it is a word because it is separated with a space    
            if (s.getCharAt(s.lenght() -1) == ‘ ‘)    
            {    
               String[] words = s.split(“ “);
               for(int i = 0; i < s.words(); i++)    
               {    
                   if (getType(words[i]) == 0)    
                    //Do word stuff
               }
            }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {

        // TODO Auto-generated method stub
    }
});


    private int TYPE = 0;

    private int getType(String givenword)    
    {     
        String[] words = getResources().getStringArray(R.array.words);    
        for(int i = 0; i < words.length(); i++)    
        {    
           //As you can see, each item of the string array has their synonym in the item itself, just divided with a “.”    Ex:   house.home    

           String[] synonyms = words[i].split(“.”);    
           for (int ii = 0; ii < synonyms.length(); i++)    
           {
                if (synonyms[ii].equals(givenword)    
                {    
                   return TYPE;
                } 
           }
        }
    }

我怎样才能正确实施呢?我应该使用神经网络吗?如果有,具体是怎样的?

PS:我宁愿不使用外部库

【问题讨论】:

    标签: java android nlp artificial-intelligence


    【解决方案1】:

    要添加自动完成和翻译,您有三个选项。

    1- 如果您熟悉 AI,您可以使用任何开源数据位置(如 Kaggle)中的数据来训练您的 AI 用于自然语言处理模型。

    2- 使用数据库和正则表达式创建一些逻辑来匹配用户输入。

    3- 如果您不熟悉 Google Cloud Translation API 等 AI 自然语言处理,则使用 API 是首选,Google Cloud APIs 是非常好的选择,您可以找到许多有用的 API。

    【讨论】:

      最近更新 更多