2016-10-18 167 views
1

所以我正在做這個過去的樣本期末考試,其中問題要求從文件讀取輸入,然後將它們處理成文字。 句子的結尾用任何以三個字符之一結尾的單詞來標記。 ? !Java處理來自文件的輸入

我能夠爲此寫一個代碼,但我只能用將它們拆分成句子使用掃描儀類並使用use.Delimiter。我想將它們處理成單詞,看看在上面的句子分隔符中是否有單詞結束,然後我將停止在句子類中添加單詞。 任何幫助將不勝感激,因爲我正在自己學習這一點,這就是我想出的。我的代碼在這裏。

File file = new File("finalq4.txt"); 
    Scanner scanner = new Scanner(file); 
    scanner.useDelimiter("[.?!]"); 
    while(scanner.hasNext()){ 
     sentCount++; 
     line = scanner.next(); 
     line = line.replaceAll("\\r?\\n", " "); 
     line = line.trim(); 
     StringTokenizer tokenizer = new StringTokenizer(line, " "); 
     wordsCount += tokenizer.countTokens(); 
     sentences.add(new Sentence(line,wordsCount)); 
     for(int i = 0; i < line.replaceAll(",|\\s+|'|-","").length(); i++){ 
      currentChar = line.charAt(i); 
      if (Character.isDigit(currentChar)) { 
      }else{ 
       lettersCount++; 
      } 
     } 
    } 

我在此代碼正在做的是,我分裂投入使用分隔符方法的句子,然後計算的話,整個文件的信件,並存儲在一個句子類的句子。

如果我想分解成單詞,我怎麼能做到這一點,而不使用掃描儀類。

從一些,我要處理的文件輸入的是在這裏

文字下面是基於密碼的維基百科頁面上!

密碼學是隱藏信息的實踐和研究。在現代,密碼學被認爲是數學和計算機科學的分支,並且與信息論,計算機安全和工程學緊密相關。加密技術用於技術領域的應用領域:先進的社會;例子包括ATM卡,計算機 密碼和電子商務安全性,這都依賴於密碼.....

我能在這個問題上進一步闡述,如果它需要解釋。

我希望能夠做的是不斷向單詞類添加單詞,並在單詞在上面的句子分隔符之一結束時停止。然後讀另一個詞,並繼續添加這些詞,直到我擊中另一個分隔符。

+0

掃描儀很不錯。 。你也可以按行讀取文件行 –

+0

使用'String.split'怎麼樣? –

+0

是的掃描儀是好的,它也很容易,而不是很多的編碼。 @ΦXocę웃Пepeúpaツ – Saad

回答

0

好了,所以我一直在通過多種技術解決這個問題,辦法之一是上面。但是我能夠用另一種方法解決這個問題,而不涉及使用Scanner類。這一個更準確,它給了我確切的輸出,而在上面,我只有幾個字和字母。

try { 
     input = new BufferedReader(new FileReader("file.txt")); 
     strLine = input.readLine(); 
     while(strLine!= null){ 

      String[] tokens = strLine.split("\\s+"); 
      for (int i = 0; i < tokens.length; i++) { 
       if(strLine.isEmpty()){ 
        continue; 
       } 
       String s = tokens[i]; 
       wordsJoin += tokens[i] + " "; 

       wordCount += i; 
       int len = s.length(); 
       String charString = s.replaceAll("[^a-zA-Z ]", ""); 
       for(int k =0; k<charString.length(); k++){ 
        currentChar = charString.charAt(k); 
        if(Character.isLetter(currentChar)){ 
         lettersCount++; 
        } 
       } 
       if (s.charAt(len - 1) == '.' || s.charAt(len - 1) == '?' || s.charAt(len - 1) == '!') { 
        sentences.add(new Sentence(wordsJoin, wordCount)); 
        sentCount++; 
        numOfWords += countWords(wordsJoin); 
        wordsJoin = ""; 
        wordCount = 0; 
       } 
      } 
      strLine = input.readLine(); 
     } 

這可能是任何人都做了同樣的問題有用的或只是需要如何從一個文本文件數的字母,單詞和句子的想法。

1

下面的代碼片段應制定

public static void main(String[] args) throws FileNotFoundException { 
    File file = new File("final.txt"); 
    Scanner scanner = new Scanner(file); 
    scanner.useDelimiter("[.?!]"); 
    int sentCount; 
    List<Sentence> sentences = new ArrayList<Sentence>(); 
    while (scanner.hasNext()) { 
     String line = scanner.next(); 
     if (!line.equals("")) { /// for the ... in the end 
      int wordsCount = 0; 
      String[] wordsOfLine = line.split(" "); 
      for (int i = 0; i < wordsOfLine.length; i++) { 
       wordsCount++; 
      } 
      Sentence sentence = new Sentence(line, wordsCount); 
      sentences.add(sentence); 
     } 
    } 
} 



public class Sentence { 
    String line = ""; 
    int wordsCount = 0; 
    public Sentence(String line, int wordsCount) { 
     this.line = line; 
     this.wordsCount=wordsCount; 
} 
+0

如果我正在關注你的程序,那麼這行就會得到一個句子,所以它和我的程序基本相同。我想將它分解成單詞,然後將每個單詞添加到句子課程中,並在結束時停止。 – Saad

+0

我不認爲你的程序正在這樣做。 – Saad

1

您可以使用一個緩衝的讀者閱讀文件的每一行。然後用split方法將每一行分割成一個句子,最後讓這些單詞用相同的方法分割句子。最後,它會是這個樣子:

BufferedReader br; 
try{ 
    br = new BufferedReader(new File(fileName)); 
}catch (IOException e) {e.printStackTrace();} 
StringBuilder sb = new StringBuilder(); 
String line; 
while((line = br.readLine()) != null){ 
    sb.append(line); 
} 
String[] sentences = sb.toString().split("\\."); 
for(String sentence:sentences){ 
    String word = sentence.split(" "); 
    //Add word to sentence... 
} 
try{ 
    br.close(); 
}catch(IOException e){ 
    e.printStackTrace(); 
}