ERLEDIGT
NEIN
NEIN
ANTWORTEN
2
2
ZUGRIFFE
335
335
EMPFEHLEN
-
18.09.08 23:10 #1
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.885
- Blog-Einträge
- 29
Just 4 Fun:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
package de.tutorials; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; /** * @author Tom * */ public class FuzzySearch { /** * @param args */ public static void main(String[] args) throws Exception { Database db = new Database(); db.load("c:/temp/presidents.txt"); System.out.println(db.query("George Bush")); System.out.println(db.query("JFK")); System.out.println(db.query("oooo")); } static class Database { List<String> index = new ArrayList<String>(); void load(String filePath) throws Exception { Scanner scanner = new Scanner(new File(filePath)); while (scanner.hasNextLine()) { index(scanner.nextLine()); } scanner.close(); } Iterable<String> query(String query) { Map<String, List<MatchPart>> matches = new HashMap<String, List<MatchPart>>(); char[] queryChars = query.replace(" ","").toCharArray(); for (String item : index) { char[] itemChars = item.toCharArray(); int itemIndex = 0; int queryIndex = 0; int alreadyMatchedQueryCharsCount = 0; List<MatchPart> matchParts = new ArrayList<MatchPart>(); while (itemIndex < itemChars.length && queryIndex < queryChars.length) { int currentQueryMatchStart = itemIndex; boolean matched = false; while (itemIndex < itemChars.length && queryIndex < queryChars.length && queryChars[queryIndex] == itemChars[itemIndex]) { queryIndex++; itemIndex++; alreadyMatchedQueryCharsCount++; matched = true; } if (matched) { matchParts.add(new MatchPart(currentQueryMatchStart, itemIndex + 1 // currentQueryMatchEnd )); } if (alreadyMatchedQueryCharsCount == queryChars.length) { matches.put(item, matchParts); // Full match break; } itemIndex++; } } return renderMatches(matches); } Iterable<String> renderMatches(Map<String, List<MatchPart>> matches) { List<String> renderedMatches = new ArrayList<String>(); for (String match : matches.keySet()) { StringBuilder renderedMatch = new StringBuilder(match); int extension = 0; for (MatchPart matchPart : matches.get(match)) { renderedMatch.insert(matchPart.start + extension, '<'); renderedMatch.insert(matchPart.end + extension, '>'); extension += 2; } renderedMatches.add(renderedMatch.toString()); } return renderedMatches; } void index(String item) { index.add(item); } static class MatchPart { int start; int end; public MatchPart(int start, int end) { this.start = start; this.end = end; } } } }
Ausgabe:
Code :1 2 3
[<George> H. <Bush>, <George> W. <Bush>] [<J>ohn <F>. <K>ennedy] [W<oo>dr<o>w Wils<o>n, The<o>d<o>re R<oo>sevelt]
Java rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Hmm, das sieht ganz schön kompliziert aus
-
22.09.08 14:13 #3
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.885
- Blog-Einträge
- 29
Hallo,
sieht vielleicht so aus, ist aber ganz einfach...
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
Ähnliche Themen
-
[Quiz#15] Thomas Darimont (Java Genetischer Algorithmus)
Von Thomas Darimont im Forum ArchivAntworten: 1Letzter Beitrag: 11.04.10, 18:58 -
[Quiz #13] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 10.01.10, 23:28 -
[Quiz #9] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 21.07.09, 23:35 -
[QUIZ#7] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 1Letzter Beitrag: 08.12.08, 09:45 -
[QUIZ #2] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 28.09.08, 14:08







Login





