2013-10-04 2 views
1

Question 1:Comment compter combien de fois un mot-clé apparaît dans une taxe en java?

Je suis en train de compter la fréquence d'un mot-clé, mon code fonctionne sauf qu'il compte également ces mots qui contiennent aussi le mot-clé (par exemple, si je recherche « compte », des mots comme " compte "sera également compté.) Est-ce que quelqu'un sait comment résoudre ce problème?

Question 2:

Je veux aussi compter le nombre de mots uniques dans un texte (ce qui signifie que je compte le mot répété qu'une seule fois). Je ne sais pas comment y parvenir non plus. Mon code me donne seulement le nombre total de mots.

Voici mon code:

import java.util.Scanner; 


public class Text_minining { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 

    //Prompt the user for the search word 
    System.out.print("enter a search word: "); 
    //Get the user's search word input 
    Scanner keywordScanner = new Scanner(System.in); 
    String keyword = keywordScanner.nextLine(); 
    keyword = keyword.toLowerCase(); 

    //Prompt the user for the text 
    System.out.println("Enter a string of words (words separated by single spaces or tabs): "); 
    //Get the user's string input 
    Scanner userInputScanner = new Scanner(System.in); 
    String userInput = userInputScanner.nextLine(); 
    userInput = userInput.toLowerCase(); 

    int keywordCount = 0, wordCount = 0; 
    int lastIndex = 0; 

    while(lastIndex != -1){ 
     lastIndex = userInput.indexOf(keyword,lastIndex); 
     if(lastIndex != -1){ 
      keywordCount ++; 
      lastIndex = keyword.length() + lastIndex; 
     } 
    } 

    boolean wasSpace=true; 
    for (int i = 0; i < userInput.length(); i++) 
    { 
     if (userInput.charAt(i) == ' ') { 
      wasSpace=true; 
     } 
     else{ 
      if(wasSpace == true) wordCount++; 
      wasSpace = false; 
      } 
    } 

    //Print the results to the screen 
    System.out.println("-------"); 
    System.out.println("Good, \"" + keyword + "\"appears in the text and the word count is " + keywordCount); 
    System.out.println("The total number of unique words in the text is " + wordCount); 


    System.exit(0); 
} 
    } 
+0

[Cette réponse] (http://stackoverflow.com/a/19108700/2024761) n'est pas exactement ce que vous cherchez, mais cela pourrait vous aider. Vérifiez-le. – SudoRahul

Répondre

2

Première: userInput.split(keyword).length - 1 fera l'affaire. Notre utilisation regex.

Deuxième:

Set<String> uniqueWords = new HashSet<String>(); 
for (String word : userInput.split(" ")) { 
    uniqueWords.add(word); 
} 
System.out.println("Unique words count " + uniqueWords.size()); 
1

Il suffit d'utiliser split méthode de chaîne.

String words[] = userInput.split(keyword);

puis vérifier et compter le mot-clé ...

for (String w : words) { 
    // do check 
} 
1

D'accord. Utilisez Fractionner pour créer le tableau et vous pouvez utiliser

(new HashSet(Arrays.asList(yourArray))).size(); 

pour trouver la

1

Je vous suggère cette approche:

  • Éclate une chaîne par des espaces blancs userInput: userInput.split("\\s+"). Vous obtiendrez un tableau. Voir String.split()
  • Pour la question 1: parcourez le tableau en comparant chaque chaîne avec votre mot-clé . Voir String.equals() et String.equalsIgnoreCase().
  • Pour la question 2: ajoutez le tableau à Set. Comme il ne peut contenir aucun objet en double, sa taille vous donnera la réponse.
Questions connexes