2017-01-08 17 views
0

このコードでは、特定の単語を含む文章を抽出できます。問題は、私がそれを何度かコピーしなければならない別の言葉に基づいていくつかの文章を抽出したい場合です。いくつかの言葉でこれを行う方法はありますか?おそらくそれに配列を与える?特定の単語を含む文字列を抽出する

String o = "Trying to extract this string. And also the one next to it.";  
String[] sent = o.split("\\."); 
List<String> output = new ArrayList<String>(); 
for (String sentence : sent) { 
    if (sentence.contains("this")) { 
     output.add(sentence); 
    } 
}  
System.out.println(">>output=" + output); 
+1

あなたのコードは、いくつかの問題があります。メソッド 'substring'は2回現れ、入れ子になっています。それを修正しようとしてください。また、「うわー」はその意味では知られていない。私は申し訳ありませんが、あなたがしようとしているものは得られません...文字列 'have"をスペースで区切って '{" have "}"とし、配列を使用していません最後に – torkleyy

+0

ごめんなさい。私はコードを投稿しました... –

+0

あなたは複数の単語を持つことができ、その単語の1つが文に存在する場合、文章を抽出する必要がありますか? – torkleyy

答えて

0

あなたはこれを試すことができます。

String o = "Trying to extract this string. And also the one next to it."; 
String[] sent = o.split("\\."); 
List<String> keyList = new ArrayList<String>(); 
keyList.add("this"); 
keyList.add("these"); 
keyList.add("that"); 

List<String> output = new ArrayList<String>(); 

for (String sentence : sent) { 
    for (String key : keyList) { 
     if (sentence.contains(key)) { 
      output.add(sentence); 
      break; 
     } 
    } 
} 
System.out.println(">>output=" + output); 
0
String sentence = "First String. Second Int. Third String. Fourth Array. Fifth Double. Sixth Boolean. Seventh String"; 
List<String> output = new ArrayList<String>(); 

for(String each: sentence.split("\\.")){ 
    if(inKeyword(each)) output.add(each); 
} 

System.out.println(output); 

ヘルパー機能:

public static Boolean inKeyword(String currentSentence){ 
    String[] keyword = {"int", "double"}; 

    for(String each: keyword){ 
     if(currentSentence.toLowerCase().contains(each)) return true; 
    } 

    return false; 
} 
0

あなたは呼ばfilterのためにフィルタリングする単語のリストや文章の配列を持っている場合Collections.disjointを使用して、その文の単語がフィルタリングする単語と重複しないかどうかを比較することができる。悲しいことに、"However"のフィルタリングを行い、文章に"However,"が含まれていると、これは機能しません。 (文章や単語に分割)のストリームで

Collection<String> filter = /**/; 
String[] sentences = /**/; 
List<String> result = new ArrayList(); 
for(String sentence : sentences) { 
    Collection<String> words = Arrays.asList(sentence.split(" ")); 
    // If they do not not overlap, they overlap 
    if (!Collections.disjoint(words, filter)) { 
     result.add(sentence); 
    }   
} 
0

次のように

String o = "Trying to extract this string. And also the one next to it."; 
    Set<String> words = new HashSet<>(Arrays.asList("this", "also")); 

    List<String> output = Arrays.stream(o.split("\\.")).filter(
      sentence -> Arrays.stream(sentence.split("\\s")).anyMatch(
        word -> words.contains(word) 
      ) 
    ).collect(Collectors.toList()); 

    System.out.println(">>output=" + output); 
0

あなたはString.matchesを使用することができます。

String sentence = ...; 
if (sentence.matches(".*(you|can|use).*")) { // Or: 
if (sentence.matches(".*\\b(you|can|use)\\b.*")) { // With word boundaries 

if (sentence.matches("(?i).*(you|can|use).*")) { // Case insensitive ("You") 

のJava 8では、以下のバリエーションが行う可能性があります:

String pattern = ".*(you|can|use).*"; 

String pattern = new StringJoiner("|", ".*(", ").*) 
    .add("you") 
    .add("can") 
    .add("use") 
    .toString(); 
// Or a stream on the words with a joining collector 

Arrays.stream(o.split("\\.\\s*")) 
    filter(sentence -> sentence.matches(pattern)) 
    forEach(System.out::println); 
関連する問題