Voici ma mise en œuvre de recherche de moins de 50 ms.
Vous devez d'abord charger le fichier et le garder en mémoire.
Vous pouvez le charger comme vous voulez, mais si vous l'avez chargé en gros morceaux, ce sera plus facile.
Mon entrée a été le byte into python book (téléchargé la seule version du fichier HTML) et le Java language specification (téléchargé le code html et créer un fichier unique de toutes les pages html)
Pour créer la liste dans un grand fichier je ce même programme (voir le code commenté).
Une fois que j'ai un gros fichier avec environ 300k mots, je courais le programme avec cette sortie:
C:\Users\oreyes\langs\java\search>dir singlelineInput.txt
El volumen de la unidad C no tiene etiqueta.
El número de serie del volumen es: 22A8-203B
Directorio de C:\Users\oreyes\langs\java\search
04/03/2011 09:37 p.m. 3,898,345 singlelineInput.txt
1 archivos 3,898,345 bytes
C:\Users\oreyes\langs\java\search>javac WordSearch.java
C:\Users\oreyes\langs\java\search>java WordSearch singlelineInput.txt "great"
Loaded 377381 words in 2844 ms
true
in 31 ms
C:\Users\oreyes\langs\java\search>java WordSearch singlelineInput.txt "great"
Loaded 377381 words in 2812 ms
true
in 31 ms
C:\Users\oreyes\langs\java\search>java WordSearch singlelineInput.txt "awesome"
Loaded 377381 words in 2813 ms
false
in 47 ms
C:\Users\oreyes\langs\java\search>gvim singlelineInput.txt
C:\Users\oreyes\langs\java\search>java WordSearch singlelineInput.txt "during"
Loaded 377381 words in 2813 ms
true
in 15 ms
C:\Users\oreyes\langs\java\search>java WordSearch singlelineInput.txt "specification"
Loaded 377381 words in 2875 ms
true
in 47 ms
C:\Users\oreyes\langs\java\search>java WordSearch singlelineInput.txt "<href"
Loaded 377381 words in 2844 ms
false
in 47 ms
C:\Users\oreyes\langs\java\search>java WordSearch singlelineInput.txt "<br>"
Loaded 377381 words in 2829 ms
true
in 15 ms
toujours moins de 50 ms.
Voici le code:
import java.io.*;
import java.util.*;
class WordSearch {
String inputFile;
List<String> words;
public WordSearch(String file) {
inputFile = file;
}
public void initialize() throws IOException {
long start = System.currentTimeMillis();
File file = new File(inputFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream((int) file.length());
FileInputStream in = new FileInputStream(file);
copyLarge(in, baos, (int)file.length());
Scanner scanner = new Scanner(new ByteArrayInputStream( baos.toByteArray()));
words = new LinkedList<String>();
while(scanner.hasNextLine()) {
String l = scanner.nextLine().trim();
//for(String s : l.split("\\s+")){
//System.out.println(s);
words.add(l.toLowerCase());
//}
}
Collections.sort(words);
for(String s : words) {
//System.out.println(s);
}
System.out.println("Loaded " + words.size() + " words in "+ (System.currentTimeMillis() - start) + " ms" );
}
public boolean contains(String aWord) {
return words.contains(aWord.toLowerCase());
}
// taken from: http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file/326413#326413
public static long copyLarge(InputStream input, OutputStream output, int size)
throws IOException {
byte[] buffer = new byte[size];// something biggie
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
public static void main(String ... args) throws IOException {
WordSearch ws = new WordSearch(args[0]);
ws.initialize();
long start = System.currentTimeMillis();
System.out.println(ws.contains(args[1]));
System.out.println("in "+ (System.currentTimeMillis() - start) +" ms ");
}
}
La partie la plus difficile était d'obtenir une entrée de l'échantillon: P
Les espaces sont mieux placés sur tous les éditeurs (y compris la zone de texte magique de SO) pour les indentations par rapport aux tabulations. –
combien de mots distincts y a-t-il? –
Où pouvons-nous avoir une longue liste de mots? Je parviens à simuler 15k et je cours sous un ms – OscarRyz