Commit f0444a5d authored by Adrian-Ionut Tanase's avatar Adrian-Ionut Tanase

added diffutils as dependency

parent 102d1d14
Pipeline #2 failed with stages
......@@ -157,6 +157,12 @@
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>com.googlecode.java-diff-utils</groupId>
<artifactId>diffutils</artifactId>
<version>1.4.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<resources>
......
......@@ -18,7 +18,7 @@ package com.readerbench.coreservices.data;
import com.readerbench.coreservices.keywordmining.Keyword;
import com.readerbench.coreservices.semanticmodels.SimilarityType;
import com.readerbench.coreservices.data.cscl.Utterance;
import com.readerbench.coreservices.data.diff.difflib.DiffRow;
import difflib.DiffRow;
import com.readerbench.coreservices.semanticmodels.SemanticModel;
import com.readerbench.datasourceprovider.pojo.Lang;
import org.slf4j.LoggerFactory;
......
......@@ -11,13 +11,6 @@ import com.readerbench.coreservices.data.AnalysisElement;
import com.readerbench.coreservices.data.Block;
import com.readerbench.coreservices.data.Sentence;
import com.readerbench.coreservices.data.Word;
import com.readerbench.coreservices.data.diff.difflib.Chunk;
import com.readerbench.coreservices.data.diff.difflib.DeleteDelta;
import com.readerbench.coreservices.data.diff.difflib.Delta;
import com.readerbench.coreservices.data.diff.difflib.DiffRow;
import com.readerbench.coreservices.data.diff.difflib.DiffUtils;
import com.readerbench.coreservices.data.diff.difflib.InsertDelta;
import com.readerbench.coreservices.data.diff.difflib.Patch;
import com.readerbench.coreservices.data.discourse.SemanticCohesion;
import com.readerbench.coreservices.data.document.Document;
import com.readerbench.coreservices.data.lexicalchains.LexicalChain;
......@@ -28,6 +21,14 @@ import com.readerbench.coreservices.semanticmodels.SimilarityType;
import com.readerbench.coreservices.semanticmodels.wordnet.OntologySupport;
import com.readerbench.datasourceprovider.pojo.Lang;
import difflib.Chunk;
import difflib.DeleteDelta;
import difflib.Delta;
import difflib.DiffRow;
import difflib.DiffUtils;
import difflib.InsertDelta;
import difflib.Patch;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
......
/*
Copyright 2010 Dmitry Naumenko (dm.naumenko@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.readerbench.coreservices.data.diff.difflib;
import java.util.List;
/**
* Describes the change-delta between original and revised texts.
*
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
* @param T The type of the compared elements in the 'lines'.
*/
public class ChangeDelta<T> extends Delta<T> {
/**
* Creates a change delta with the two given chunks.
* @param original The original chunk. Must not be {@code null}.
* @param revised The original chunk. Must not be {@code null}.
*/
public ChangeDelta(Chunk<T> original, Chunk<T>revised) {
super(original, revised);
}
/**
* {@inheritDoc}
*
* @throws PatchFailedException
*/
@Override
public void applyTo(List<T> target) throws PatchFailedException {
verify(target);
int position = getOriginal().getPosition();
int size = getOriginal().size();
for (int i = 0; i < size; i++) {
target.remove(position);
}
int i = 0;
for (T line : getRevised().getLines()) {
target.add(position + i, line);
i++;
}
}
/**
* {@inheritDoc}
*/
@Override
public void restore(List<T> target) {
int position = getRevised().getPosition();
int size = getRevised().size();
for (int i = 0; i < size; i++) {
target.remove(position);
}
int i = 0;
for (T line : getOriginal().getLines()) {
target.add(position + i, line);
i++;
}
}
/**
* {@inheritDoc}
*/
public void verify(List<T> target) throws PatchFailedException {
getOriginal().verify(target);
if (getOriginal().getPosition() > target.size()) {
throw new PatchFailedException("Incorrect patch for delta: "
+ "delta original position > target size");
}
}
@Override
public String toString() {
return "[ChangeDelta, position: " + getOriginal().getPosition() + ", lines: "
+ getOriginal().getLines() + " to " + getRevised().getLines() + "]";
}
@Override
public TYPE getType() {
return Delta.TYPE.CHANGE;
}
}
/*
Copyright 2010 Dmitry Naumenko (dm.naumenko@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.readerbench.coreservices.data.diff.difflib;
import java.util.Arrays;
import java.util.List;
/**
* Holds the information about the part of text involved in the diff process
*
* <p>
* Text is represented as <code>Object[]</code> because the diff engine is
* capable of handling more than plain ascci. In fact, arrays or lists of any
* type that implements {@link java.lang.Object#hashCode hashCode()} and
* {@link java.lang.Object#equals equals()} correctly can be subject to
* differencing using this library.
* </p>
*
* @author <a href="dm.naumenko@gmail.com>Dmitry Naumenko</a>
* @param T The type of the compared elements in the 'lines'.
*/
public class Chunk<T> {
private int position;
private List<T> lines;
/**
* Creates a chunk and saves a copy of affected lines
*
* @param position
* the start position
* @param lines
* the affected lines
*/
public Chunk(int position, List<T> lines) {
this.position = position;
this.lines = lines;
}
/**
* Creates a chunk and saves a copy of affected lines
*
* @param position
* the start position
* @param lines
* the affected lines
*/
public Chunk(int position, T[] lines) {
this.position = position;
this.lines = Arrays.asList(lines);
}
/**
* Verifies that this chunk's saved text matches the corresponding text in
* the given sequence.
*
* @param target
* the sequence to verify against.
*/
public void verify(List<T> target) throws PatchFailedException {
if (last() > target.size()) {
throw new PatchFailedException("Incorrect Chunk: the position of chunk > target size");
}
for (int i = 0; i < size(); i++) {
if (!target.get(position + i).equals(lines.get(i))) {
throw new PatchFailedException(
"Incorrect Chunk: the chunk content doesn't match the target");
}
}
}
/**
* @return the start position of chunk in the text
*/
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public void setLines(List<T> lines) {
this.lines = lines;
}
/**
* @return the affected lines
*/
public List<T> getLines() {
return lines;
}
public int size() {
return lines.size();
}
/**
* Returns the index of the last line of the chunk.
*/
public int last() {
return getPosition() + size() - 1;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((lines == null) ? 0 : lines.hashCode());
result = prime * result + position;
result = prime * result + size();
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Chunk<T> other = (Chunk) obj;
if (lines == null) {
if (other.lines != null)
return false;
} else if (!lines.equals(other.lines))
return false;
if (position != other.position)
return false;
return true;
}
@Override
public String toString() {
return "[position: " + position + ", size: " + size() + ", lines: " + lines + "]";
}
}
/*
Copyright 2010 Dmitry Naumenko (dm.naumenko@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.readerbench.coreservices.data.diff.difflib;
import java.util.List;
/**
* Describes the delete-delta between original and revised texts.
*
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
* @param T The type of the compared elements in the 'lines'.
*/
public class DeleteDelta<T> extends Delta<T> {
/**
* Creates a change delta with the two given chunks.
*
* @param original
* The original chunk. Must not be {@code null}.
* @param revised
* The original chunk. Must not be {@code null}.
*/
public DeleteDelta(Chunk<T> original, Chunk<T> revised) {
super(original, revised);
}
/**
* {@inheritDoc}
*
* @throws PatchFailedException
*/
@Override
public void applyTo(List<T> target) throws PatchFailedException {
verify(target);
int position = getOriginal().getPosition();
int size = getOriginal().size();
for (int i = 0; i < size; i++) {
target.remove(position);
}
}
/**
* {@inheritDoc}
*/
@Override
public void restore(List<T> target) {
int position = this.getRevised().getPosition();
List<T> lines = this.getOriginal().getLines();
for (int i = 0; i < lines.size(); i++) {
target.add(position + i, lines.get(i));
}
}
@Override
public TYPE getType() {
return Delta.TYPE.DELETE;
}
@Override
public void verify(List<T> target) throws PatchFailedException {
getOriginal().verify(target);
}
@Override
public String toString() {
return "[DeleteDelta, position: " + getOriginal().getPosition() + ", lines: "
+ getOriginal().getLines() + "]";
}
}
/*
Copyright 2010 Dmitry Naumenko (dm.naumenko@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.readerbench.coreservices.data.diff.difflib;
import java.util.*;
/**
* Describes the delta between original and revised texts.
*
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
* @param T The type of the compared elements in the 'lines'.
*/
public abstract class Delta<T> {
/** The original chunk. */
private Chunk<T> original;
/** The revised chunk. */
private Chunk<T> revised;
/**
* Specifies the type of the delta.
*
*/
public enum TYPE {
/** A change in the original. */
CHANGE,
/** A delete from the original. */
DELETE,
/** An insert into the original. */
INSERT
}
/**
* Construct the delta for original and revised chunks
*
* @param original Chunk describing the original text. Must not be {@code null}.
* @param revised Chunk describing the revised text. Must not be {@code null}.
*/
public Delta(Chunk<T> original, Chunk<T> revised) {
if (original == null) {
throw new IllegalArgumentException("original must not be null");
}
if (revised == null) {
throw new IllegalArgumentException("revised must not be null");
}
this.original = original;
this.revised = revised;
}
/**
* Verifies that this delta can be used to patch the given text.
*
* @param target the text to patch.
* @throws PatchFailedException if the patch cannot be applied.
*/
public abstract void verify(List<T> target) throws PatchFailedException;
/**
* Applies this delta as the patch for a given target
*
* @param target the given target
* @throws PatchFailedException
*/
public abstract void applyTo(List<T> target) throws PatchFailedException;
/**
* Cancel this delta for a given revised text. The action is opposite to
* patch.
*
* @param target the given revised text
*/
public abstract void restore(List<T> target);
/**
* Returns the type of delta
* @return the type enum
*/
public abstract TYPE getType();
/**
* @return The Chunk describing the original text.
*/
public Chunk<T> getOriginal() {
return original;
}
/**
* @param original The Chunk describing the original text to set.
*/
public void setOriginal(Chunk<T> original) {
this.original = original;
}
/**
* @return The Chunk describing the revised text.
*/
public Chunk<T> getRevised() {
return revised;
}
/**
* @param revised The Chunk describing the revised text to set.
*/
public void setRevised(Chunk<T> revised) {
this.revised = revised;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((original == null) ? 0 : original.hashCode());
result = prime * result + ((revised == null) ? 0 : revised.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Delta<T> other = (Delta) obj;
if (original == null) {
if (other.original != null)
return false;
} else if (!original.equals(other.original))
return false;
if (revised == null) {
if (other.revised != null)
return false;
} else if (!revised.equals(other.revised))
return false;
return true;
}
}
package com.readerbench.coreservices.data.diff.difflib;
import java.io.Serializable;
import java.util.Comparator;
/**
* @author mksenzov
* @param T The type of the compared elements in the 'lines'.
*/
public class DeltaComparator implements Comparator<Delta<?>>, Serializable {
private static final long serialVersionUID = 1L;
public static final Comparator<Delta<?>> INSTANCE = new DeltaComparator();
private DeltaComparator() {
}
public int compare(final Delta<?> a, final Delta<?> b) {
final int posA = a.getOriginal().getPosition();
final int posB = b.getOriginal().getPosition();
if (posA > posB) {
return 1;
} else if (posA < posB) {
return -1;
}
return 0;
}
}
\ No newline at end of file
/*
Copyright 2010 Dmitry Naumenko (dm.naumenko@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.readerbench.coreservices.data.diff.difflib;
import java.util.*;
/**
* The general interface for computing diffs between two lists of elements of type T.
*
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
* @param T The type of the compared elements in the 'lines'.
*/
public interface DiffAlgorithm<T> {
/**
* Computes the difference between the original sequence and the revised
* sequence and returns it as a {@link Patch} object.
*
* @param original The original sequence. Must not be {@code null}.
* @param revised The revised sequence. Must not be {@code null}.
* @return The patch representing the diff of the given sequences. Never {@code null}.
*/
public Patch<T> diff(T[] original, T[] revised);
/**
* Computes the difference between the original sequence and the revised
* sequence and returns it as a {@link Patch} object.
*
* @param original The original sequence. Must not be {@code null}.
* @param revised The revised sequence. Must not be {@code null}.
* @return The patch representing the diff of the given sequences. Never {@code null}.
*/
public Patch<T> diff(List<T> original, List<T> revised);
}
/*
Copyright 2010 Dmitry Naumenko (dm.naumenko@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.readerbench.coreservices.data.diff.difflib;
/**
* Base class for all exceptions emanating from this package.
*
* @author <a href="mailto:juanco@suigeneris.org">Juanco Anez</a>
*/
public class DiffException extends Exception {
private static final long serialVersionUID = 1L;
public DiffException() {
}
public DiffException(String msg) {
super(msg);
}
}
<
/*
Copyright 2010 Dmitry Naumenko (dm.naumenko@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and