Используйте шаблон соответствия для подсчета слов в строке - PullRequest
0 голосов
/ 28 октября 2018

Я хочу написать метод в java, который получает входную строку и перечисляет слова (без учета регистра), которые встречаются более одного раза, с указанием их числа.

например:

input>> "I have two cars in my garage and my dad has one car in his garage"

он должен выдать следующий вывод:

output>> my -- repeated 2 times
         in -- repeated 2 times
         ...

вот мой код

public class classduplicate {
    private static final String REGEX = "\\b([A-Z]+)\\s+\\1\\b*";
   private static final String INPUT = "Cat cat cat cattie cat";
    public static void main(String[] args) {
      Pattern p = Pattern.compile(REGEX);
      Matcher m = p.matcher(INPUT);   // get a matcher object
      int count = 0;

      while(m.find()) {
         count++;
                 System.out.println(m.find());


      }
      System.out.println("Match number "+count);
    }
}

Ответы [ 2 ]

0 голосов
/ 28 октября 2018

Вы можете найти повторяющиеся слова, как показано ниже:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package duplicatewords;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *
 * @author sami
 */
public class DuplicateWords {

    private static final String INPUT = "Cat cat cat cattie cat";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        List<String> wordsWithCase = DuplicateWords(INPUT);
        List<String> wordsWithoutCase = DuplicateWordsDespiteOfCase(INPUT);
        CountDuplicateWords(INPUT, wordsWithCase);
        CountDuplicateWords(INPUT, wordsWithoutCase);
    }

    /**
     * Find the duplicate words with regards of upper and lower case
     * @param inputValue Input String
     * @return duplicateWords List of the words which are duplicated in the string.
     */
    private static List<String> DuplicateWords(String inputValue) {
        String[] breakWords = inputValue.split("\\s+");
        List<String> duplicateWords = new ArrayList<>();
        for (String word : breakWords) {
            if (!duplicateWords.contains(word)) {
                duplicateWords.add(word);
            }
        }
        return duplicateWords;
    }

    /**
     * Find the duplicate words despite of upper and lower case
     * @param inputValue Input String
     * @return duplicateWords List of the words which are duplicated in the string.
     */
    private static List<String> DuplicateWordsDespiteOfCase(String inputValue) {
        inputValue = inputValue.toLowerCase();
        String[] breakWords = inputValue.split("\\s+");
        List<String> duplicateWords = new ArrayList<>();
        for (String word : breakWords) {
            if (!duplicateWords.contains(word)) {
                duplicateWords.add(word);
            }
        }
        return duplicateWords;
    }

    /**
     * Found the Iteration of the the duplicated words in the string
     * @param inputValue Input String
     * @param duplicatedWords List of the duplicated words
     */
    private static void CountDuplicateWords(String inputValue, List<String> duplicatedWords) {
        int i;
        Pattern pattern;
        Matcher matcher;
        System.out.println("Duplicate Words With respect of Upper and Lower Case: " + duplicatedWords);
        for (String value : duplicatedWords) {
            i = 0;
            pattern = Pattern.compile(value);
            matcher = pattern.matcher(inputValue);
            while (matcher.find()) {
                i++;
            }
            System.out.println(i);
        }
    }
}

DuplicateWords метод получить все слова, которые повторяются в строке, в верхнем и нижнем регистре, метод DuplicateWordsDespiteOfCase getвсе слова, которые повторяются в строке, несмотря на прописные и строчные буквы (что вы упомянули в своем вопросе).Как только повторяющиеся слова затем CountDuplicateWords проверяет их нахождение в строке.

Вы можете удалить методы DuplicateWords, если не хотите их использовать.Это только для справки.

0 голосов
/ 28 октября 2018

Я не думаю, что вы можете решить эту проблему с помощью регулярных выражений.Это решение с использованием набора:

    String str = " I have two cars   in my garage and my dad has one   car in his garage ";
    System.out.println(str);

    String low = str.trim().toLowerCase();

    String[] words = low.split("\\s+");

    Set<String> setOfWords = new HashSet<String>(Arrays.asList(words));

    low = " " + str.toLowerCase() + " ";
    low = low.replaceAll("\\s", "  ");

    for (String s : setOfWords) {
        String without = low.replaceAll(" " + s + " ", "");
        int counter = (low.length() - without.length()) / (s.length() + 2);
        if (counter > 1)
            System.out.println(s + " repeated " + counter + " times.");
    }

он напечатает

 I have two cars   in my garage and my dad has one   car in his garage 
in repeated 2 times.
garage repeated 2 times.
my repeated 2 times.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...