Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Implement source locale filter for glossary search
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Mar 15, 2012
1 parent c7be055 commit 01f8045
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,13 +127,12 @@ public HLocale getLocale()
{
return locale;
}

public void setLocale(HLocale locale)
{
this.locale = locale;
}


@Override
public String toString()
{
Expand Down Expand Up @@ -193,6 +192,3 @@ else if (!locale.equals(other.locale))
return true;
}
}



Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public List<HGlossaryTerm> findByIdList(List<Long> idList)
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)
Expand All @@ -151,12 +151,10 @@ public List<Object[]> getSearchResult(String searchText, SearchType searchType,
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,10 +63,14 @@ public class GlossaryPresenter extends WidgetPresenter<GlossaryPresenter.Display
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();
Expand All @@ -84,12 +93,14 @@ public interface Display extends WidgetDisplay
}

@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);
}
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private void setSouthPanelExpanded(boolean expanded)
if (tu != null)
{
transMemoryPresenter.createTMRequestForTransUnit(tu);
// glossaryPresenter.createGlossaryRequestForTransUnit(tu);
glossaryPresenter.createGlossaryRequestForTransUnit(tu);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,40 +69,6 @@ public class GetGlossaryHandler extends AbstractActionHandler<GetGlossary, GetGl
@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
{
Expand All @@ -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)
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -10,6 +11,7 @@ public class DocumentInfo implements HasIdentifier<DocumentId>, Serializable
private DocumentId id;
private String name;
private String path;
private LocaleId sourceLocale;
private TranslationStats stats;

// for GWT
Expand All @@ -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()
Expand All @@ -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()
{
Expand Down
Loading

0 comments on commit 01f8045

Please sign in to comment.