Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串中的搜索建议_Java_Search Suggestion - Fatal编程技术网

Java 字符串中的搜索建议

Java 字符串中的搜索建议,java,search-suggestion,Java,Search Suggestion,我有一个文本文件,其中包含: mariam amr sara john jessy salma Mkkkaokaooorlll 用户输入要搜索的单词:例如:maram 如您所见,它不存在于我的文本文件中。。我想提出一些建议,类似于maram是mariam这个词 我使用了最长的公共子序列,但它给出了mariam和mkkkaoorlll,因为它们都包含最长的公共子序列“mar” 我只想强迫你选择玛丽安 有什么想法吗 提前谢谢 /** ** Java Program to implement Lon

我有一个文本文件,其中包含:
mariam amr sara john jessy salma Mkkkaokaooorlll

用户输入要搜索的单词:例如:
maram

如您所见,它不存在于我的文本文件中。。我想提出一些建议,类似于maram是mariam这个词

我使用了最长的公共子序列,但它给出了
mariam
mkkkaoorlll
,因为它们都包含最长的公共子序列“mar”

我只想强迫你选择玛丽安 有什么想法吗

提前谢谢

/**
 ** Java Program to implement Longest Common Subsequence Algorithm
 **/

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

   /** Class  LongestCommonSubsequence **/
    public class  LongestCommonSubsequence
    {    
   /** function lcs **/
    public String lcs(String str1, String str2)
    {
    int l1 = str1.length();
    int l2 = str2.length();

    int[][] arr = new int[l1 + 1][l2 + 1];

    for (int i = l1 - 1; i >= 0; i--)
    {
        for (int j = l2 - 1; j >= 0; j--)
        {
            if (str1.charAt(i) == str2.charAt(j))
                arr[i][j] = arr[i + 1][j + 1] + 1;
            else 
                arr[i][j] = Math.max(arr[i + 1][j], arr[i][j + 1]);
        }
    }

    int i = 0, j = 0;
    StringBuffer sb = new StringBuffer();
    while (i < l1 && j < l2) 
    {
        if (str1.charAt(i) == str2.charAt(j)) 
        {
            sb.append(str1.charAt(i));
            i++;
            j++;
        }
        else if (arr[i + 1][j] >= arr[i][j + 1]) 
            i++;
        else
            j++;
    }


    return sb.toString(); 
   //read text file, if a word contains sb.toString() , print it


}

/** Main Function **/
public static void main(String[] args) throws IOException
{    
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Longest Common Subsequence Algorithm Test\n");

    System.out.println("\nEnter string 1");
    String str1 = br.readLine();

    System.out.println("\nEnter string 2");
    String str2 = br.readLine();

    LongestCommonSubsequence obj = new LongestCommonSubsequence(); 
    String result = obj.lcs(str1, str2);

    System.out.println("\nLongest Common Subsequence : "+ result);
}
/**
**实现最长公共子序列算法的Java程序
**/
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.io.IOException;
/**类最长公共子序列**/
公共类最长公共子序列
{    
/**功能lcs**/
公共字符串lcs(字符串str1、字符串str2)
{
int l1=str1.length();
int l2=str2.length();
int[]arr=新int[l1+1][l2+1];
对于(int i=l1-1;i>=0;i--)
{
对于(int j=l2-1;j>=0;j--)
{
if(str1.字符(i)=str2.字符(j))
arr[i][j]=arr[i+1][j+1]+1;
其他的
arr[i][j]=Math.max(arr[i+1][j],arr[i][j+1]);
}
}
int i=0,j=0;
StringBuffer sb=新的StringBuffer();
而(i=arr[i][j+1])
i++;
其他的
j++;
}
使某人返回字符串();
//读取文本文件,如果某个单词包含sb.toString(),请打印它
}
/**主要功能**/
公共静态void main(字符串[]args)引发IOException
{    
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“最长公共子序列算法测试\n”);
System.out.println(“\n输入字符串1”);
字符串str1=br.readLine();
System.out.println(“\n输入字符串2”);
字符串str2=br.readLine();
LongestCommonSubsequence obj=新的LongestCommonSubsequence();
字符串结果=对象lcs(str1、str2);
System.out.println(“\n最常用子序列:“+结果”);
}

}

有几种类似于此的模糊匹配技术-Apache Commons提供了一些优秀的工具,用于比较两个字符串之间的相似程度。请查看javadoc以了解计算方法

对于Levenshtein距离,分数越低,字符串越相似:

StringUtils.getLevenshteinDistance("frog", "fog") == 1
StringUtils.getLevenshteinDistance("fly", "ant") == 3

你也可以考虑为每个字符串计算这个值,这将允许你在说话时判断字符串的声音有多么相似,即使它们不一定拼写相同。


回到你的问题-使用这些工具,如果用户的搜索词在文本文件中任何字符串的某个阈值内,你可以提出建议。

我认为这是因为只有
mariam
mkkk
m
开头。我打赌你的算法会一个接一个地检查乞丐的尸体。请给我们看地图code@Toumach这是因为两者都有最长的公共子序列mar,我将添加代码我无法回答,但我将向上投票,以便ppl看到