Search in sources :

Example 81 with Span

use of edu.uci.ics.textdb.api.span.Span in project textdb by TextDB.

the class NlpSplitOperator method computeSentenceList.

private List<Span> computeSentenceList(Tuple inputTuple) {
    String inputText = inputTuple.<IField>getField(predicate.getInputAttributeName()).getValue().toString();
    Reader reader = new StringReader(inputText);
    DocumentPreprocessor documentPreprocessor = new DocumentPreprocessor(reader);
    List<Span> sentenceList = new ArrayList<Span>();
    int start = 0;
    int end = 0;
    String key = PropertyNameConstants.NLP_SPLIT_KEY;
    String attributeName = predicate.getInputAttributeName();
    for (List<HasWord> sentence : documentPreprocessor) {
        String sentenceText = Sentence.listToString(sentence);
        //Make span
        end = start + sentenceText.length();
        Span span = new Span(attributeName, start, end, key, sentenceText);
        sentenceList.add(span);
        start = end + 1;
    }
    return sentenceList;
}
Also used : HasWord(edu.stanford.nlp.ling.HasWord) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) IField(edu.uci.ics.textdb.api.field.IField) DocumentPreprocessor(edu.stanford.nlp.process.DocumentPreprocessor) Span(edu.uci.ics.textdb.api.span.Span)

Example 82 with Span

use of edu.uci.ics.textdb.api.span.Span in project textdb by TextDB.

the class DictionaryMatcherSourceOperator method computeMatchingResult.

/*
     * Match the key against the Tuple. if there's no match, returns the
     * original Tuple object, if there's a match, return a new Tuple
     * with span list added
     */
private Tuple computeMatchingResult(String key, Tuple sourceTuple) throws TextDBException {
    List<String> attributeNames = predicate.getAttributeNames();
    List<Span> matchingResults = new ArrayList<>();
    for (String attributeName : attributeNames) {
        String fieldValue = sourceTuple.getField(attributeName).getValue().toString();
        AttributeType attributeType = inputSchema.getAttribute(attributeName).getAttributeType();
        // fieldValue exactly
        if (attributeType != AttributeType.TEXT) {
            if (fieldValue.equals(key)) {
                matchingResults.add(new Span(attributeName, 0, fieldValue.length(), key, fieldValue));
            }
        } else // if attribute type is TEXT, then key can match a substring of
        // fieldValue
        {
            String regex = key.toLowerCase();
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(fieldValue.toLowerCase());
            while (matcher.find()) {
                int start = matcher.start();
                int end = matcher.end();
                matchingResults.add(new Span(attributeName, start, end, key, fieldValue.substring(start, end)));
            }
        }
    }
    advanceDictionaryCursor();
    if (matchingResults.size() == 0) {
        return null;
    }
    ListField<Span> spanListField = sourceTuple.getField(predicate.getSpanListName());
    List<Span> spanList = spanListField.getValue();
    spanList.addAll(matchingResults);
    return sourceTuple;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AttributeType(edu.uci.ics.textdb.api.schema.AttributeType) ArrayList(java.util.ArrayList) Span(edu.uci.ics.textdb.api.span.Span)

Example 83 with Span

use of edu.uci.ics.textdb.api.span.Span in project textdb by TextDB.

the class SpanTupleTest method createSpanListField.

private IField createSpanListField() {
    List<Span> list = new ArrayList<Span>();
    // The key value will be:
    // For RegexMatcher : "n.*k"
    // For NamedEntityMatcher : LOCATION
    // For DictionaryMatcher: "new york" - For DictionaryMatcher the key and
    // value are same
    // For KeyWordMatcher: "new york" - the value can be "new" or "york"
    Span span1 = new Span("description", 18, 26, "LOCATION", "new york");
    Span span2 = new Span("description", 52, 63, "LOCATION", "los angeles");
    list.add(span1);
    list.add(span2);
    IField spanListField = new ListField<Span>(list);
    return spanListField;
}
Also used : ArrayList(java.util.ArrayList) ListField(edu.uci.ics.textdb.api.field.ListField) IField(edu.uci.ics.textdb.api.field.IField) Span(edu.uci.ics.textdb.api.span.Span)

Example 84 with Span

use of edu.uci.ics.textdb.api.span.Span in project textdb by TextDB.

the class DictionaryMatcherTest method testSingleWordQueryInStringFieldUsingScan.

/**
     * Scenario: verifies GetNextTuple of DictionaryMatcher and single word
     * queries in String Field using SCANOPERATOR
     */
@Test
public void testSingleWordQueryInStringFieldUsingScan() throws Exception {
    ArrayList<String> names = new ArrayList<String>(Arrays.asList("bruce"));
    Dictionary dictionary = new Dictionary(names);
    // create a data tuple first
    List<Span> list = new ArrayList<Span>();
    Span span = new Span("firstName", 0, 5, "bruce", "bruce");
    list.add(span);
    Attribute[] schemaAttributes = new Attribute[TestConstants.ATTRIBUTES_PEOPLE.length + 1];
    for (int count = 0; count < schemaAttributes.length - 1; count++) {
        schemaAttributes[count] = TestConstants.ATTRIBUTES_PEOPLE[count];
    }
    schemaAttributes[schemaAttributes.length - 1] = RESULTS_ATTRIBUTE;
    IField[] fields1 = { new StringField("bruce"), new StringField("john Lee"), new IntegerField(46), new DoubleField(5.50), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-14-1970")), new TextField("Tall Angry"), new ListField<Span>(list) };
    Tuple tuple1 = new Tuple(new Schema(schemaAttributes), fields1);
    List<Tuple> expectedResults = new ArrayList<Tuple>();
    expectedResults.add(tuple1);
    List<String> attributeNames = Arrays.asList(TestConstants.FIRST_NAME, TestConstants.LAST_NAME, TestConstants.DESCRIPTION);
    List<Tuple> returnedResults = DictionaryMatcherTestHelper.getQueryResults(PEOPLE_TABLE, dictionary, attributeNames, KeywordMatchingType.SUBSTRING_SCANBASED);
    boolean contains = TestUtils.equals(expectedResults, returnedResults);
    Assert.assertTrue(contains);
}
Also used : Dictionary(edu.uci.ics.textdb.exp.dictionarymatcher.Dictionary) Attribute(edu.uci.ics.textdb.api.schema.Attribute) Schema(edu.uci.ics.textdb.api.schema.Schema) ArrayList(java.util.ArrayList) IntegerField(edu.uci.ics.textdb.api.field.IntegerField) IField(edu.uci.ics.textdb.api.field.IField) Span(edu.uci.ics.textdb.api.span.Span) StringField(edu.uci.ics.textdb.api.field.StringField) TextField(edu.uci.ics.textdb.api.field.TextField) DateField(edu.uci.ics.textdb.api.field.DateField) SimpleDateFormat(java.text.SimpleDateFormat) DoubleField(edu.uci.ics.textdb.api.field.DoubleField) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) Test(org.junit.Test)

Example 85 with Span

use of edu.uci.ics.textdb.api.span.Span in project textdb by TextDB.

the class FuzzyTokenMatcherTest method TestFuzzyTokenMatcherWithLimitOffset.

@Test
public void TestFuzzyTokenMatcherWithLimitOffset() throws Exception {
    String query = "Twelve Angry Men";
    // The ratio of tokens that need to be matched
    double threshold = 0.5;
    ArrayList<String> attributeNames = new ArrayList<>();
    attributeNames.add(TestConstants.DESCRIPTION);
    Schema schema = Utils.addAttributeToSchema(TestConstants.SCHEMA_PEOPLE, RESULTS_ATTR);
    List<Span> list = new ArrayList<>();
    Span span = new Span(TestConstants.DESCRIPTION, 5, 10, "angry", "Angry", 1);
    list.add(span);
    IField[] fields1 = { new StringField("bruce"), new StringField("john Lee"), new IntegerField(46), new DoubleField(5.50), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-14-1970")), new TextField("Tall Angry"), new ListField<>(list) };
    list = new ArrayList<>();
    span = new Span(TestConstants.DESCRIPTION, 6, 11, "angry", "Angry", 1);
    list.add(span);
    IField[] fields2 = { new StringField("brad lie angelina"), new StringField("pitt"), new IntegerField(44), new DoubleField(6.10), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-12-1972")), new TextField("White Angry"), new ListField<>(list) };
    list = new ArrayList<>();
    span = new Span(TestConstants.DESCRIPTION, 40, 45, "angry", "Angry", 8);
    list.add(span);
    IField[] fields3 = { new StringField("george lin lin"), new StringField("lin clooney"), new IntegerField(43), new DoubleField(6.06), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1973")), new TextField("Lin Clooney is Short and lin clooney is Angry"), new ListField<>(list) };
    list = new ArrayList<>();
    span = new Span(TestConstants.DESCRIPTION, 6, 11, "angry", "angry", 1);
    list.add(span);
    IField[] fields4 = { new StringField("Mary brown"), new StringField("Lake Forest"), new IntegerField(42), new DoubleField(5.99), new DateField(new SimpleDateFormat("MM-dd-yyyy").parse("01-13-1974")), new TextField("Short angry"), new ListField<>(list) };
    Tuple tuple1 = new Tuple(schema, fields1);
    Tuple tuple2 = new Tuple(schema, fields2);
    Tuple tuple3 = new Tuple(schema, fields3);
    Tuple tuple4 = new Tuple(schema, fields4);
    List<Tuple> expectedResultList = new ArrayList<>();
    expectedResultList.add(tuple1);
    expectedResultList.add(tuple2);
    expectedResultList.add(tuple3);
    expectedResultList.add(tuple4);
    List<Tuple> results = FuzzyTokenMatcherTestHelper.getQueryResults(PEOPLE_TABLE, query, threshold, attributeNames, 2, 1);
    Assert.assertEquals(expectedResultList.size(), 4);
    Assert.assertEquals(results.size(), 2);
    Assert.assertTrue(TestUtils.containsAll(expectedResultList, results));
}
Also used : Schema(edu.uci.ics.textdb.api.schema.Schema) ArrayList(java.util.ArrayList) IntegerField(edu.uci.ics.textdb.api.field.IntegerField) IField(edu.uci.ics.textdb.api.field.IField) Span(edu.uci.ics.textdb.api.span.Span) StringField(edu.uci.ics.textdb.api.field.StringField) TextField(edu.uci.ics.textdb.api.field.TextField) DateField(edu.uci.ics.textdb.api.field.DateField) SimpleDateFormat(java.text.SimpleDateFormat) DoubleField(edu.uci.ics.textdb.api.field.DoubleField) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) Test(org.junit.Test)

Aggregations

Span (edu.uci.ics.textdb.api.span.Span)112 ArrayList (java.util.ArrayList)97 Schema (edu.uci.ics.textdb.api.schema.Schema)88 IField (edu.uci.ics.textdb.api.field.IField)86 Tuple (edu.uci.ics.textdb.api.tuple.Tuple)80 TextField (edu.uci.ics.textdb.api.field.TextField)71 Attribute (edu.uci.ics.textdb.api.schema.Attribute)71 Test (org.junit.Test)71 IntegerField (edu.uci.ics.textdb.api.field.IntegerField)60 StringField (edu.uci.ics.textdb.api.field.StringField)58 DoubleField (edu.uci.ics.textdb.api.field.DoubleField)49 DateField (edu.uci.ics.textdb.api.field.DateField)46 SimpleDateFormat (java.text.SimpleDateFormat)46 Dictionary (edu.uci.ics.textdb.exp.dictionarymatcher.Dictionary)25 ListField (edu.uci.ics.textdb.api.field.ListField)18 KeywordMatcherSourceOperator (edu.uci.ics.textdb.exp.keywordmatcher.KeywordMatcherSourceOperator)10 AttributeType (edu.uci.ics.textdb.api.schema.AttributeType)9 JoinDistancePredicate (edu.uci.ics.textdb.exp.join.JoinDistancePredicate)9 DataFlowException (edu.uci.ics.textdb.api.exception.DataFlowException)7 SchemaConstants (edu.uci.ics.textdb.api.constants.SchemaConstants)5