热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在java中自动更正输入的String值-Auto-correctinganinputtedStringvalue,injava

Ihaveasimpleapplicationthataskstheuserforasearchterm,andthenperformsasearchfortha

I have a simple application that asks the user for a search term, and then performs a search for that term.

我有一个简单的应用程序,要求用户提供搜索词,然后搜索该词。

I want to give it the ability to "auto correct" the entered term. This is just for a few terms, that are easy to enter incorrectly. For example, if someone searches for "BestBuy", and what I have in my array to be searched is "Best Buy", I want to automatically convert "BestBuy" into "Best Buy" before the search. I intend to have a list of these in a text file.

我想让它能够“自动更正”输入的术语。这只是几个术语,很容易输入错误。例如,如果有人搜索“BestBuy”,并且我在我的阵列中搜索的是“Best Buy”,我想在搜索之前自动将“BestBuy”转换为“Best Buy”。我打算在文本文件中列出这些列表。

What's the best way to do this? Can I have in my text file, every line be something like bestbuy, Best Buy the first item being the entered term and the second one being what it gets autocorrected into? What should I use to store this data? A hashmap?

最好的方法是什么?我可以在我的文本文件中,每行都是bestbuy,Best Buy第一项是输入的术语,第二项是自动修正的内容吗?我应该用什么来存储这些数据?一个hashmap?

Edit: Just to clarify, I'm not trying to make an actual auto-correct system. That's way, way beyond the scope of this project. This is just to simply replace certain inputs with "corrected" versions to match what is in the array being searched.

编辑:只是为了澄清,我不打算制作一个真正的自动纠正系统。这是超出本项目范围的方式。这只是简单地用“已更正”版本替换某些输入以匹配正在搜索的数组中的内容。

3 个解决方案

#1


Autocorrect, generally has a harder solution than a hashmap, because you cannot predict the user input, so no point of making a hashmap, though you can use it as a key-existence store. One possible way: http://en.wikipedia.org/wiki/Levenshtein_distance with the words you have in your map/dictionary, and then selecting the nearest

自动更正,通常比哈希映射更难解决,因为您无法预测用户输入,因此无需制作哈希映射,尽管您可以将其用作密钥存在库。一种可能的方法:http://en.wikipedia.org/wiki/Levenshtein_distance,用你在地图/字典中的单词,然后选择最近的

#2


you could use HashMap, create a method that translate all different types of input into the correct one. You method takes input like bestbuy, best buy, BestBuy,etc and return only Best Buy. The result Best Buy will be the HashMap key.

你可以使用HashMap,创建一个方法,将所有不同类型的输入转换为正确的输入。你的方法需要像bestbuy,Best Buy,BestBuy等输入,只返回Best Buy。 Best Buy的结果将是HashMap键。

#3


This is bit tricky. To give a high level idea, you need to maintain a hashMap, with string without spaces as key, and the corrected value as value. when user enters a string, trim() it, check with your map' keyset, and replace with value if found matching entry.

这有点棘手。要提供高级别的想法,您需要维护一个hashMap,其中包含不带空格的字符串作为键,并将更正后的值作为值。当用户输入一个字符串,trim()它,检查你的地图'keyset,如果找到匹配的条目,用值替换。


推荐阅读
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了Linux Shell中括号和整数扩展的使用方法,包括命令组、命令替换、初始化数组以及算术表达式和逻辑判断的相关内容。括号中的命令将会在新开的子shell中顺序执行,括号中的变量不能被脚本余下的部分使用。命令替换可以用于将命令的标准输出作为另一个命令的输入。括号中的运算符和表达式符合C语言运算规则,可以用在整数扩展中进行算术计算和逻辑判断。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
author-avatar
风情万种791008
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有