2016-05-13 3 views
0

私はメインストリングを持っています。文字列から一致する単語を見つける方法とパーセント値?

文字列main = "こんにちは?私は大丈夫ですか?";

、その後、いくつかの文字列..

文字列の文字列1 = "HI必要が何であるかを人................ 方法へ ......................................................................................... ....... とは、 のみ変更が必要です。................................ ......... 細かい 状況............... ... タスクを実行する................................. ... ........ 私はいいえ............... はあなたですか大丈夫ですか?

文字列string2のを行う方法= "................... 私は(約100の単語でもよいです)わからない 必要.........何か私はといいますか...................... 細かい 私はam ..........大丈夫.. ";

文字列はstring3 = "再びいくつかの文字列..............."(約100語であってもよいです)。

文字列string4 = "再びいくつかの文字列..............."(約100語であってもよいです)。

はすべて、私が何をすべきか今

7ワードメイン文字列にがある..

とあなたが見ることができるように(約100の単語でもよいです)これらの7つの単語がstring1に存在します.4つの単語がstring2 ..に存在し、続行します..

これで、string1のパーセント値はstring2の100%.. になりました。パーセント値は57.xx %..とように...だから、

、私はプログラムでこれらのパーセンテージ値を取得したい。..

私は、

String perc; 

String[] q1 = str1.split(" "); 
String[] q2 = main.split(" "); 

for (String temp1: q1) { 
    for (String temp2: q2) { 
     if(temp1.equals(temp2)) { 
      // Some code here 
     } 
    } 
} 

今、私はどこから始めれば分からない、これまで試してみましたか?

答えて

1

ここでそれを行うことができる方法:

String main = "hi how are you? i am fine."; 
// Extract all the words whose length is > 1 and remove duplicates 
Set<String> mainWords = new HashSet<>(); 
for (String s : main.split("\\W")) { 
    if (s.length() > 1) { 
     mainWords.add(s); 
    } 
} 
String string1 = "hi people what is the need................ how to ................... do the needful.................................. and you can only need the change......................................... in the fine situation................................................. to do the task................... i am alright............... are you okay?"; 

Set<String> mainWordsToFind = new HashSet<>(mainWords); 
// Iterate over all the words and remove the word from the list to find 
for (String word : string1.split("\\W")) { 
    if (word.length() > 1) { 
     mainWordsToFind.remove(word); 
    } 
} 

// Print the percent of word found 
System.out.println((double) (mainWords.size() - mainWordsToFind.size())/mainWords.size()); 

出力:

1.0 
+0

は申し訳ありませんが試してみてください。 。 –

+0

いいえ私はJavaを使用しています。7.80 –

+0

これは動作しています..しかし私は1つの質問があります。つまり、メインの文字列に7つの単語があり、そのすべての単語がstring1に存在するので、%valueは100%でなければなりません。しかし、私は** 0.025157232704402517 **を取得しました**( totalFound/totalWords)* 100 **しかし、値は** 2.5157232704402517 **です。これは望ましい出力ではないと思います。そうでなければ、あなたのコードは正常に動作しています。ありがとう。 –

0

は、私はあなたの答えで出力を得るために管理することができませんでした。この

String string1 = "hi people what is the need how to do the needful and you can only need the changein the fine situation to do the task i am alright are you okay?"; 


    String string2 = "how to do i don't know the need what i am supposed to do fine i am okay.."; 

    int count = 0; 
    String[] array = string1.split(" "); 
    ArrayList<String> processedWords = new ArrayList<>(); 
    for(String str : array){ 

     if (string2.contains(str) && !processedWords.contains(str) && str.length() > 0){ 
       System.out.println(str); 

      processedWords.add(str); 
      count++; 
     } 

    } 
    System.out.println("Total words: " +array.length); 
    System.out.println("Count: " +count); 

    System.out.println("Percent value: " +((float)count/array.length)*100); 
+0

str1、str2、str3、......、strNなどのすべての文字列について同じ答えが得られます。文字列mainとのすべての文字列を比較していたので、 –

+0

いくつかの実装で出力を得ました.. –

関連する問題