次のようなテキスト ファイルがあるとします: http://www.gutenberg.org/files/17921/17921-8.txt
テキスト ファイルから単語を抽出するための優れたアルゴリズムまたはオープン ソース コードを持っている人はいますか? 特殊文字を避け、「それは」などを維持しながら、すべての単語を取得する方法...
私はJavaで働いています。ありがとう
次のようなテキスト ファイルがあるとします: http://www.gutenberg.org/files/17921/17921-8.txt
テキスト ファイルから単語を抽出するための優れたアルゴリズムまたはオープン ソース コードを持っている人はいますか? 特殊文字を避け、「それは」などを維持しながら、すべての単語を取得する方法...
私はJavaで働いています。ありがとう
これは、正規表現の適切な仕事のように思えます。開始方法がわからない場合に備えて、アイデアを提供する Java コードを次に示します。
String input = "Input text, with words, punctuation, etc. Well, it's rather short.";
Pattern p = Pattern.compile("[\\w']+");
Matcher m = p.matcher(input);
while ( m.find() ) {
System.out.println(input.substring(m.start(), m.end()));
}
パターン[\w']+
は、すべての単語文字とアポストロフィに複数回一致します。例の文字列は単語ごとに出力されます。詳細については、Java パターン クラスのドキュメントを参照してください。
問題に対する適切なアプローチは次のとおりです。この関数は、テキストを入力として受け取り、指定されたテキスト内のすべての単語の配列を返します。
private ArrayList<String> get_Words(String SInput){
StringBuilder stringBuffer = new StringBuilder(SInput);
ArrayList<String> all_Words_List = new ArrayList<String>();
String SWord = "";
for(int i=0; i<stringBuffer.length(); i++){
Character charAt = stringBuffer.charAt(i);
if(Character.isAlphabetic(charAt) || Character.isDigit(charAt)){
SWord = SWord + charAt;
}
else{
if(!SWord.isEmpty()) all_Words_List.add(new String(SWord));
SWord = "";
}
}
return all_Words_List;
}
擬似コードは次のようになります。
create words, a list of words, by splitting the input by whitespace
for every word, strip out whitespace and punctuation on the left and the right
Python コードは次のようになります。
words = input.split()
words = [word.strip(PUNCTUATION) for word in words]
どこ
PUNCTUATION = ",. \n\t\\\"'][#*:"
または削除したい他の文字。
Java には String クラスに同等の関数があると思います: String .split() 。
リンクで提供したテキストに対してこのコードを実行した結果の出力:
>>> print words[:100]
['Project', "Gutenberg's", 'Manual', 'of', 'Surgery', 'by', 'Alexis',
'Thomson', 'and', 'Alexander', 'Miles', 'This', 'eBook', 'is', 'for',
'the', 'use', 'of', 'anyone', 'anywhere', 'at', 'no', 'cost', 'and',
'with', 'almost', 'no', 'restrictions', 'whatsoever', 'You', 'may',
'copy', 'it', 'give', 'it', 'away', 'or', 're-use', 'it', 'under',
... etc etc.
基本的には合わせたい
([A-Za-z])+('([A-Za-z])*)?
右?
作成したパターンを使用して正規表現を試し、そのパターンが見つかった回数をカウントすることができます。