Search in sources :

Example 1 with ParseEntryResult

use of org.omegat.core.data.ParseEntry.ParseEntryResult in project omegat by omegat-org.

the class Aligner method parseFile.

/**
 * Parse the specified file and return the contents as a pair of lists:
 * <ul>
 * <li>Key: A list of IDs for the parsed text units
 * <li>Value: A list of parsed text units
 * </ul>
 *
 * @param file
 *            Path to input file
 * @return Pair of lists
 * @throws Exception
 *             If parsing fails
 */
private Entry<List<String>, List<String>> parseFile(String file) throws Exception {
    final List<String> ids = new ArrayList<>();
    final List<String> rawSegs = new ArrayList<>();
    Core.getFilterMaster().loadFile(file, new FilterContext(srcLang, trgLang, true).setRemoveAllTags(removeTags), new IParseCallback() {

        @Override
        public void linkPrevNextSegments() {
        }

        @Override
        public void addEntry(String id, String source, String translation, boolean isFuzzy, String comment, IFilter filter) {
            process(source, id);
        }

        @Override
        public void addEntry(String id, String source, String translation, boolean isFuzzy, String comment, String path, IFilter filter, List<ProtectedPart> protectedParts) {
            process(source, id != null ? id : path != null ? path : null);
        }

        @Override
        public void addEntryWithProperties(String id, String source, String translation, boolean isFuzzy, String[] props, String path, IFilter filter, List<ProtectedPart> protectedParts) {
            process(source, id != null ? id : path != null ? path : null);
        }

        private void process(String text, String id) {
            boolean removeSpaces = Core.getFilterMaster().getConfig().isRemoveSpacesNonseg();
            text = StringUtil.normalizeUnicode(ParseEntry.stripSomeChars(text, new ParseEntryResult(), removeTags, removeSpaces));
            if (!text.trim().isEmpty()) {
                if (id != null) {
                    ids.add(id);
                }
                rawSegs.add(text);
            }
        }
    });
    return new AbstractMap.SimpleImmutableEntry<>(ids, rawSegs);
}
Also used : IParseCallback(org.omegat.filters2.IParseCallback) ProtectedPart(org.omegat.core.data.ProtectedPart) ArrayList(java.util.ArrayList) ParseEntryResult(org.omegat.core.data.ParseEntry.ParseEntryResult) IFilter(org.omegat.filters2.IFilter) FilterContext(org.omegat.filters2.FilterContext)

Aggregations

ArrayList (java.util.ArrayList)1 ParseEntryResult (org.omegat.core.data.ParseEntry.ParseEntryResult)1 ProtectedPart (org.omegat.core.data.ProtectedPart)1 FilterContext (org.omegat.filters2.FilterContext)1 IFilter (org.omegat.filters2.IFilter)1 IParseCallback (org.omegat.filters2.IParseCallback)1