Permalink
Browse files

Implement source locale filter for glossary search

  • Loading branch information...
1 parent c7be055 commit 01f8045a09c880153cdba5b975094ead447b6e7c @aeng aeng committed Mar 15, 2012
View
58 server/zanata-model/src/main/java/org/zanata/hibernate/search/LocaleFilter.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
+ * @author tags. See the copyright.txt file in the distribution for a full
+ * listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.zanata.hibernate.search;
+
+import java.io.IOException;
+
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermDocs;
+import org.apache.lucene.search.DocIdSet;
+import org.apache.lucene.search.Filter;
+import org.apache.lucene.util.OpenBitSet;
+import org.jboss.seam.log.Log;
+import org.jboss.seam.log.Logging;
+import org.zanata.common.LocaleId;
+
+public class LocaleFilter extends Filter
+{
+ private static final long serialVersionUID = 1L;
+ private static final Log log = Logging.getLog(LocaleFilter.class);
+ private LocaleId locale;
+
+ public LocaleFilter(LocaleId locale)
+ {
+ this.locale = locale;
+ }
+
+ @Override
+ public DocIdSet getDocIdSet(IndexReader reader) throws IOException
+ {
+ OpenBitSet bitSet = new OpenBitSet(reader.maxDoc());
+ if (log.isDebugEnabled())
+ log.debug("getDocIdSet for {0}", locale.toString());
+ Term term = new Term("locale", locale.toString());
+ TermDocs termDocs = reader.termDocs(term);
+ while (termDocs.next())
+ bitSet.set(termDocs.doc());
+ return bitSet;
+ }
+}
View
60 server/zanata-model/src/main/java/org/zanata/hibernate/search/LocaleFilterFactory.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
+ * @author tags. See the copyright.txt file in the distribution for a full
+ * listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.zanata.hibernate.search;
+
+import org.apache.lucene.search.Filter;
+import org.hibernate.search.annotations.Factory;
+import org.hibernate.search.annotations.Key;
+import org.hibernate.search.filter.FilterKey;
+import org.hibernate.search.filter.StandardFilterKey;
+import org.zanata.common.LocaleId;
+
+public class LocaleFilterFactory
+{
+
+ @Factory
+ public Filter getFilter()
+ {
+ LocaleFilter filter = new LocaleFilter(this.locale);
+ return filter;
+ }
+
+ private LocaleId locale;
+
+ public LocaleId getLocale()
+ {
+ return locale;
+ }
+
+ public void setLocale(LocaleId locale)
+ {
+ this.locale = locale;
+ }
+
+ @Key
+ public FilterKey getKey()
+ {
+ StandardFilterKey key = new StandardFilterKey();
+ key.addParameter(locale);
+ return key;
+ }
+
+}
View
14 server/zanata-model/src/main/java/org/zanata/model/HGlossaryTerm.java
@@ -44,18 +44,18 @@
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.validator.NotNull;
-import org.zanata.hibernate.search.IdFilterFactory;
+import org.zanata.hibernate.search.LocaleFilterFactory;
import org.zanata.hibernate.search.LocaleIdBridge;
/**
- *
+ *
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
- *
+ *
**/
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Indexed
-@FullTextFilterDef(name = "glossaryFilter", impl = IdFilterFactory.class, cache = FilterCacheModeType.INSTANCE_ONLY)
+@FullTextFilterDef(name = "glossaryLocaleFilter", impl = LocaleFilterFactory.class, cache = FilterCacheModeType.INSTANCE_ONLY)
public class HGlossaryTerm extends ModelEntityBase
{
private String content;
@@ -127,13 +127,12 @@ public HLocale getLocale()
{
return locale;
}
-
+
public void setLocale(HLocale locale)
{
this.locale = locale;
}
-
@Override
public String toString()
{
@@ -193,6 +192,3 @@ else if (!locale.equals(other.locale))
return true;
}
}
-
-
-
View
6 server/zanata-war/src/main/java/org/zanata/dao/GlossaryDAO.java
@@ -129,7 +129,7 @@ public HGlossaryEntry getEntryBySrcLocaleAndContent(LocaleId localeid, String co
return query.list();
}
- public List<Object[]> getSearchResult(String searchText, SearchType searchType, List<Long> termIds, final int maxResult) throws ParseException
+ public List<Object[]> getSearchResult(String searchText, SearchType searchType, LocaleId srcLocale, final int maxResult) throws ParseException
{
String queryText;
switch (searchType)
@@ -151,12 +151,10 @@ public HGlossaryEntry getEntryBySrcLocaleAndContent(LocaleId localeid, String co
throw new RuntimeException("Unknown query type: " + searchType);
}
- // textQuery.append + " AND localeId: $srcLocale"
-
QueryParser parser = new QueryParser(Version.LUCENE_29, "content", new StandardAnalyzer(Version.LUCENE_29));
org.apache.lucene.search.Query textQuery = parser.parse(queryText);
FullTextQuery ftQuery = entityManager.createFullTextQuery(textQuery, HGlossaryTerm.class);
- ftQuery.enableFullTextFilter("glossaryFilter").setParameter("ids", termIds);
+ ftQuery.enableFullTextFilter("glossaryLocaleFilter").setParameter("locale", srcLocale);
ftQuery.setProjection(FullTextQuery.SCORE, FullTextQuery.THIS);
@SuppressWarnings("unchecked")
List<Object[]> matches = ftQuery.setMaxResults(maxResult).getResultList();
View
24 server/zanata-war/src/main/java/org/zanata/webtrans/client/presenter/GlossaryPresenter.java
@@ -24,10 +24,15 @@
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;
+import org.zanata.common.LocaleId;
import org.zanata.webtrans.client.events.InsertStringInEditorEvent;
import org.zanata.webtrans.client.events.TransUnitSelectionEvent;
import org.zanata.webtrans.client.events.TransUnitSelectionHandler;
+import org.zanata.webtrans.client.history.History;
+import org.zanata.webtrans.client.history.HistoryToken;
import org.zanata.webtrans.client.rpc.CachingDispatchAsync;
+import org.zanata.webtrans.shared.model.DocumentId;
+import org.zanata.webtrans.shared.model.DocumentInfo;
import org.zanata.webtrans.shared.model.TransUnit;
import org.zanata.webtrans.shared.model.TranslationMemoryGlossaryItem;
import org.zanata.webtrans.shared.model.WorkspaceContext;
@@ -58,10 +63,14 @@
private final WorkspaceContext workspaceContext;
private final CachingDispatchAsync dispatcher;
private final GlossaryDetailsPresenter glossaryDetailsPresenter;
+ private final DocumentListPresenter docListPresenter;
+ private final History history;
private GetGlossary submittedRequest = null;
private GetGlossary lastRequest = null;
+
private ListDataProvider<TranslationMemoryGlossaryItem> dataProvider;
+
public interface Display extends WidgetDisplay
{
HasClickHandlers getSearchButton();
@@ -84,12 +93,14 @@
}
@Inject
- public GlossaryPresenter(Display display, EventBus eventBus, CachingDispatchAsync dispatcher, GlossaryDetailsPresenter glossaryDetailsPresenter, WorkspaceContext workspaceContext)
+ public GlossaryPresenter(Display display, EventBus eventBus, CachingDispatchAsync dispatcher, GlossaryDetailsPresenter glossaryDetailsPresenter, DocumentListPresenter docListPresenter, History history, WorkspaceContext workspaceContext)
{
super(display, eventBus);
this.dispatcher = dispatcher;
this.workspaceContext = workspaceContext;
this.glossaryDetailsPresenter = glossaryDetailsPresenter;
+ this.docListPresenter = docListPresenter;
+ this.history = history;
dataProvider = new ListDataProvider<TranslationMemoryGlossaryItem>();
display.setDataProvider(dataProvider);
}
@@ -138,7 +149,16 @@ public void update(int index, TranslationMemoryGlossaryItem object, ImageResourc
private void createGlossaryRequest(final String query, GetGlossary.SearchType searchType)
{
display.startProcessing();
- final GetGlossary action = new GetGlossary(query, workspaceContext.getWorkspaceId().getLocaleId(), searchType);
+
+ HistoryToken token = HistoryToken.fromTokenString(history.getToken());
+ DocumentId docId = docListPresenter.getDocumentId(token.getDocumentPath());
+ DocumentInfo docInfo = docListPresenter.getDocumentInfo(docId);
+ LocaleId srcLocale = LocaleId.EN_US;
+ if (docInfo != null)
+ {
+ srcLocale = docInfo.getSourceLocale();
+ }
+ final GetGlossary action = new GetGlossary(query, workspaceContext.getWorkspaceId().getLocaleId(), srcLocale, searchType);
scheduleGlossaryRequest(action);
}
View
2 ...r/zanata-war/src/main/java/org/zanata/webtrans/client/presenter/TranslationPresenter.java
@@ -379,7 +379,7 @@ private void setSouthPanelExpanded(boolean expanded)
if (tu != null)
{
transMemoryPresenter.createTMRequestForTransUnit(tu);
- // glossaryPresenter.createGlossaryRequestForTransUnit(tu);
+ glossaryPresenter.createGlossaryRequestForTransUnit(tu);
}
}
else
View
2 server/zanata-war/src/main/java/org/zanata/webtrans/server/rpc/GetDocumentListHandler.java
@@ -58,7 +58,7 @@ public GetDocumentListResult execute(GetDocumentList action, ExecutionContext co
{
DocumentId docId = new DocumentId(hDoc.getId());
TranslationStats stats = documentDAO.getStatistics(hDoc.getId(), localeId);
- DocumentInfo doc = new DocumentInfo(docId, hDoc.getName(), hDoc.getPath(), stats);
+ DocumentInfo doc = new DocumentInfo(docId, hDoc.getName(), hDoc.getPath(), hDoc.getLocale().getLocaleId(), stats);
docs.add(doc);
}
}
View
54 server/zanata-war/src/main/java/org/zanata/webtrans/server/rpc/GetGlossaryHandler.java
@@ -69,40 +69,6 @@
@In
private GlossaryDAO glossaryDAO;
- /**
- * Filtered term ids First filter: Entries that contains target locale Second
- * filter: Source term in filtered entries
- *
- * @param entries
- */
- private List<Long> getFilteredSrcTermIds(List<HGlossaryEntry> entries)
- {
- List<Long> termIds = new ArrayList<Long>();
- for (HGlossaryEntry entry : entries)
- {
- HGlossaryTerm term = entry.getGlossaryTerms().get(entry.getSrcLocale());
- if (term != null)
- {
- termIds.add(term.getId());
- }
- }
- return termIds;
- }
-
- private HGlossaryTerm getTargetTerm(List<HGlossaryEntry> entries, Long id, HLocale locale)
- {
- HGlossaryTerm targetTerm = null;
- for (HGlossaryEntry entry : entries)
- {
- if (entry.getId() == id)
- {
- return entry.getGlossaryTerms().get(locale);
- }
- }
- return targetTerm;
-
- }
-
@Override
public GetGlossaryResult execute(GetGlossary action, ExecutionContext context) throws ActionException
{
@@ -120,9 +86,7 @@ public GetGlossaryResult execute(GetGlossary action, ExecutionContext context) t
try
{
List<HGlossaryEntry> entries = glossaryDAO.getEntriesByLocaleId(localeID);
- List<Long> termIds = getFilteredSrcTermIds(entries);
-
- List<Object[]> matches = glossaryDAO.getSearchResult(searchText, searchType, termIds, MAX_RESULTS);
+ List<Object[]> matches = glossaryDAO.getSearchResult(searchText, searchType, action.getSrcLocaleId(), MAX_RESULTS);
Map<GlossaryKey, TranslationMemoryGlossaryItem> matchesMap = new LinkedHashMap<GlossaryKey, TranslationMemoryGlossaryItem>();
for (Object[] match : matches)
@@ -135,7 +99,21 @@ public GetGlossaryResult execute(GetGlossary action, ExecutionContext context) t
}
String srcTermContent = glossaryTerm.getContent();
- HGlossaryTerm targetTerm = getTargetTerm(entries, glossaryTerm.getGlossaryEntry().getId(), hLocale);
+
+ HGlossaryTerm targetTerm = null;
+ for (HGlossaryEntry entry : entries)
+ {
+ if (entry.getId() == glossaryTerm.getGlossaryEntry().getId())
+ {
+ targetTerm = entry.getGlossaryTerms().get(hLocale);
+ }
+ }
+
+ if (targetTerm == null)
+ {
+ continue;
+ }
+
String targetTermContent = targetTerm.getContent();
int percent = (int) (100 * LevenshteinUtil.getSimilarity(searchText, srcTermContent));
View
13 server/zanata-war/src/main/java/org/zanata/webtrans/shared/model/DocumentInfo.java
@@ -2,6 +2,7 @@
import java.io.Serializable;
+import org.zanata.common.LocaleId;
import org.zanata.common.TranslationStats;
public class DocumentInfo implements HasIdentifier<DocumentId>, Serializable
@@ -10,6 +11,7 @@
private DocumentId id;
private String name;
private String path;
+ private LocaleId sourceLocale;
private TranslationStats stats;
// for GWT
@@ -18,12 +20,13 @@ private DocumentInfo()
{
}
- public DocumentInfo(DocumentId id, String name, String path, TranslationStats stats)
+ public DocumentInfo(DocumentId id, String name, String path, LocaleId sourceLocale, TranslationStats stats)
{
this.id = id;
this.name = name;
this.path = path;
this.stats = stats;
+ this.sourceLocale = sourceLocale;
}
public DocumentId getId()
@@ -41,14 +44,16 @@ public String getPath()
return path;
}
- /**
- * @return the stats
- */
public TranslationStats getStats()
{
return stats;
}
+ public LocaleId getSourceLocale()
+ {
+ return sourceLocale;
+ }
+
@Override
public String toString()
{
View
9 server/zanata-war/src/main/java/org/zanata/webtrans/shared/rpc/GetGlossary.java
@@ -11,17 +11,19 @@
private LocaleId localeId;
private String query;
private SearchType searchType;
+ private LocaleId srcLocaleId;
@SuppressWarnings("unused")
private GetGlossary()
{
}
- public GetGlossary(String query, LocaleId localeId, SearchType searchType)
+ public GetGlossary(String query, LocaleId localeId, LocaleId srcLocaleId, SearchType searchType)
{
this.query = query;
this.localeId = localeId;
this.searchType = searchType;
+ this.srcLocaleId = srcLocaleId;
}
@Override
@@ -50,6 +52,11 @@ public String getQuery()
return query;
}
+ public LocaleId getSrcLocaleId()
+ {
+ return srcLocaleId;
+ }
+
@Override
public int hashCode()
{
View
3 server/zanata-war/src/test/java/org/zanata/webtrans/client/presenter/AppPresenterTest.java
@@ -8,6 +8,7 @@
import org.easymock.Capture;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import org.zanata.common.LocaleId;
import org.zanata.common.TransUnitCount;
import org.zanata.common.TransUnitWords;
import org.zanata.common.TranslationStats;
@@ -477,7 +478,7 @@ private void buildTestDocumentInfo()
TransUnitCount unitCount = new TransUnitCount(1, 2, 3);
TransUnitWords wordCount = new TransUnitWords(4, 5, 6);
testDocStats = new TranslationStats(unitCount, wordCount);
- testDocInfo = new DocumentInfo(testDocId, TEST_DOCUMENT_NAME, TEST_DOCUMENT_PATH, testDocStats);
+ testDocInfo = new DocumentInfo(testDocId, TEST_DOCUMENT_NAME, TEST_DOCUMENT_PATH, LocaleId.EN_US, testDocStats);
}
/**
View
12 ...ata-war/src/test/java/org/zanata/webtrans/client/presenter/DocumentListPresenterTest.java
@@ -406,7 +406,7 @@ public void documentSelectUpdatesHistoryToken()
verifyAllMocks();
// simulate document click on second document
- DocumentInfo docInfo = new DocumentInfo(new DocumentId(2222L), "doc122", "second/path/", new TranslationStats());
+ DocumentInfo docInfo = new DocumentInfo(new DocumentId(2222L), "doc122", "second/path/", LocaleId.EN_US, new TranslationStats());
capturedDocumentSelectionHandler.getValue().onSelection(new SelectionEvent<DocumentInfo>(docInfo)
{
});
@@ -599,10 +599,10 @@ public void getDocumentInfo()
setupAndBindDocListPresenter();
DocumentInfo docInfo = dlp.getDocumentInfo(new DocumentId(1111L));
- assertThat(docInfo, is(equalTo(new DocumentInfo(new DocumentId(1111L), "doc111", "first/path/", new TranslationStats()))));
+ assertThat(docInfo, is(equalTo(new DocumentInfo(new DocumentId(1111L), "doc111", "first/path/", LocaleId.EN_US, new TranslationStats()))));
docInfo = dlp.getDocumentInfo(new DocumentId(3333L));
- assertThat(docInfo, is(equalTo(new DocumentInfo(new DocumentId(3333L), "doc123", "third/path/", new TranslationStats()))));
+ assertThat(docInfo, is(equalTo(new DocumentInfo(new DocumentId(3333L), "doc123", "third/path/", LocaleId.EN_US, new TranslationStats()))));
}
private void setupAndBindDocListPresenter()
@@ -622,13 +622,13 @@ private void setupAndBindDocListPresenter()
TransUnitCount unitCount = new TransUnitCount(1, 2, 3);
TransUnitWords wordCount = new TransUnitWords(4, 5, 6);
- DocumentInfo docInfo = new DocumentInfo(new DocumentId(1111L), "matches", "no/filter", new TranslationStats(unitCount, wordCount));
+ DocumentInfo docInfo = new DocumentInfo(new DocumentId(1111L), "matches", "no/filter", LocaleId.EN_US, new TranslationStats(unitCount, wordCount));
docList.add(docInfo);
- docInfo = new DocumentInfo(new DocumentId(2222L), "filter", "match/exact/", new TranslationStats(unitCount, wordCount));
+ docInfo = new DocumentInfo(new DocumentId(2222L), "filter", "match/exact/", LocaleId.EN_US, new TranslationStats(unitCount, wordCount));
docList.add(docInfo);
- docInfo = new DocumentInfo(new DocumentId(3333L), "filter", "does/not/match/exact/", new TranslationStats(unitCount, wordCount));
+ docInfo = new DocumentInfo(new DocumentId(3333L), "filter", "does/not/match/exact/", LocaleId.EN_US, new TranslationStats(unitCount, wordCount));
docList.add(docInfo);
return docList;
View
19 server/zanata-war/src/test/java/org/zanata/webtrans/client/rpc/DummyGetDocsListCommand.java
@@ -6,6 +6,7 @@
import java.util.ArrayList;
import org.zanata.common.ContentState;
+import org.zanata.common.LocaleId;
import org.zanata.common.TransUnitCount;
import org.zanata.common.TransUnitWords;
import org.zanata.common.TranslationStats;
@@ -44,15 +45,15 @@ public void execute()
private ArrayList<DocumentInfo> generateTransUnitSampleData()
{
ArrayList<DocumentInfo> names = new ArrayList<DocumentInfo>();
- names.add(new DocumentInfo(new DocumentId(0), "name0", "", newStats(0)));
- names.add(new DocumentInfo(new DocumentId(1), "path1name1", "path/1", newStats(1)));
- names.add(new DocumentInfo(new DocumentId(2), "path1name2", "path/1", newStats(2)));
- names.add(new DocumentInfo(new DocumentId(3), "path2name1", "path/2", newStats(3)));
- names.add(new DocumentInfo(new DocumentId(4), "path2name2", "path/2", newStats(4)));
- names.add(new DocumentInfo(new DocumentId(5), "name2", "", newStats(5)));
- names.add(new DocumentInfo(new DocumentId(6), "name1", "", newStats(6)));
- names.add(new DocumentInfo(new DocumentId(7), "long name, a name which is really quite long, too wide to be displayed without scrolling (in most cases)", "", newStats(7)));
- names.add(new DocumentInfo(new DocumentId(8), "another long name, a name which is really quite long, too wide to be displayed without scrolling (in most cases)", "long path, a path which is, again, really quite long, and also too wide to be displayed without scrolling (in most cases)", newStats(8)));
+ names.add(new DocumentInfo(new DocumentId(0), "name0", "", LocaleId.EN_US, newStats(0)));
+ names.add(new DocumentInfo(new DocumentId(1), "path1name1", "path/1", LocaleId.EN_US, newStats(1)));
+ names.add(new DocumentInfo(new DocumentId(2), "path1name2", "path/1", LocaleId.EN_US, newStats(2)));
+ names.add(new DocumentInfo(new DocumentId(3), "path2name1", "path/2", LocaleId.EN_US, newStats(3)));
+ names.add(new DocumentInfo(new DocumentId(4), "path2name2", "path/2", LocaleId.EN_US, newStats(4)));
+ names.add(new DocumentInfo(new DocumentId(5), "name2", "", LocaleId.EN_US, newStats(5)));
+ names.add(new DocumentInfo(new DocumentId(6), "name1", "", LocaleId.EN_US, newStats(6)));
+ names.add(new DocumentInfo(new DocumentId(7), "long name, a name which is really quite long, too wide to be displayed without scrolling (in most cases)", "", LocaleId.EN_US, newStats(7)));
+ names.add(new DocumentInfo(new DocumentId(8), "another long name, a name which is really quite long, too wide to be displayed without scrolling (in most cases)", "long path, a path which is, again, really quite long, and also too wide to be displayed without scrolling (in most cases)", LocaleId.EN_US, newStats(8)));
for (int n = 10; n < 9; n++)
{
// two digit numbers, to make sorting happier

0 comments on commit 01f8045

Please sign in to comment.