001 // This file is part of AceWiki. 002 // Copyright 2008-2012, AceWiki developers. 003 // 004 // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU 005 // Lesser General Public License as published by the Free Software Foundation, either version 3 of 006 // the License, or (at your option) any later version. 007 // 008 // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 010 // Lesser General Public License for more details. 011 // 012 // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If 013 // not, see http://www.gnu.org/licenses/. 014 015 package ch.uzh.ifi.attempto.acewiki.core; 016 017 import java.util.ArrayList; 018 import java.util.List; 019 import java.util.Map; 020 import java.util.TreeMap; 021 022 /** 023 * This class is the default implementation of a word index. 024 * 025 * @author Tobias Kuhn 026 */ 027 //TODO Different ontology elements should be allowed to have overlapping word forms. 028 public class DefaultWordIndex implements WordIndex { 029 030 private Map<String, OntologyElement> wordIndex = new TreeMap<String, OntologyElement>(); 031 private List<OntologyElement> elements = new ArrayList<OntologyElement>(); 032 033 public void elementAdded(OntologyElement element) { 034 if (elements.contains(element)) { 035 throw new RuntimeException("Registration failed: Already registered."); 036 } 037 elements.add(element); 038 for (String word : element.getWords()) { 039 if (word == null) continue; 040 041 if (getElement(word) == null) { 042 wordIndex.put(word, element); 043 } else if (getElement(word) != element) { 044 throw new RuntimeException( 045 "Registration failed: The word '" + word + "' is already used." 046 ); 047 } 048 } 049 } 050 051 public void elementRemoved(OntologyElement element) { 052 elements.remove(element); 053 for (String word : element.getWords()) { 054 if (word == null) continue; 055 wordIndex.remove(word); 056 } 057 } 058 059 public void elementBeforeChange(OntologyElement element) { 060 for (String word : element.getWords()) { 061 if (word != null) { 062 wordIndex.remove(word); 063 } 064 } 065 } 066 067 public void elementAfterChange(OntologyElement element) { 068 for (String word : element.getWords()) { 069 if (word != null) { 070 if (getElement(word) == null) { 071 wordIndex.put(word, element); 072 } else if (getElement(word) != element) { 073 throw new RuntimeException( 074 "Word update failed: The word '" + word + "' is already used." 075 ); 076 } 077 } 078 } 079 } 080 081 public OntologyElement getElement(String word) { 082 return wordIndex.get(word); 083 } 084 085 public List<OntologyElement> searchForElements(String searchText) { 086 List<OntologyElement> searchResult = new ArrayList<OntologyElement>(); 087 String s = searchText.toLowerCase().replace('_', ' '); 088 for (OntologyElement e : elements) { 089 for (String w : e.getWords()) { 090 if (w == null) continue; 091 if (w.toLowerCase().replace('_', ' ').contains(s)) { 092 searchResult.add(e); 093 break; 094 } 095 } 096 } 097 LanguageUtils.sortOntologyElements(searchResult); 098 return searchResult; 099 } 100 101 }