예제 #1
0
파일: shingle.go 프로젝트: ekanite/bleve
func (s *ShingleFilter) shingleCurrentRingState(ring *ring.Ring, itemsInRing int) analysis.TokenStream {
	rv := make(analysis.TokenStream, 0)
	for shingleN := s.min; shingleN <= s.max; shingleN++ {
		// if there are enough items in the ring
		// to produce a shingle of this size
		if itemsInRing >= shingleN {
			thisShingleRing := ring.Move(-(shingleN - 1))
			shingledBytes := make([]byte, 0)
			pos := 0
			start := -1
			end := 0
			for i := 0; i < shingleN; i++ {
				if i != 0 {
					shingledBytes = append(shingledBytes, []byte(s.tokenSeparator)...)
				}
				curr := thisShingleRing.Value.(*analysis.Token)
				if pos == 0 && curr.Position != 0 {
					pos = curr.Position
				}
				if start == -1 && curr.Start != -1 {
					start = curr.Start
				}
				if curr.End != -1 {
					end = curr.End
				}
				shingledBytes = append(shingledBytes, curr.Term...)
				thisShingleRing = thisShingleRing.Next()
			}
			token := analysis.Token{
				Type: analysis.Shingle,
				Term: shingledBytes,
			}
			if pos != 0 {
				token.Position = pos
			}
			if start != -1 {
				token.Start = start
			}
			if end != -1 {
				token.End = end
			}
			rv = append(rv, &token)
		}
	}
	return rv
}
예제 #2
0
파일: boundary.go 프로젝트: drewwells/bleve
func (t *UnicodeWordBoundaryTokenizer) Tokenize(input []byte) analysis.TokenStream {
	rv := make(analysis.TokenStream, 0)

	if len(input) < 1 {
		return rv
	}

	// works
	var myUnsafePointer = unsafe.Pointer(&(input[0]))
	var myCCharPointer *C.char = (*C.char)(myUnsafePointer)

	var inlen C.int32_t = C.int32_t(len(input))
	var buflen C.int32_t = C.int32_t(2*len(input) + 1) // worse case each byte becomes 2
	var stringToExamine []C.UChar = make([]C.UChar, buflen)
	var myUnsafePointerToExamine = unsafe.Pointer(&(stringToExamine[0]))
	var myUCharPointer *C.UChar = (*C.UChar)(myUnsafePointerToExamine)
	C.u_uastrncpy(myUCharPointer, myCCharPointer, inlen)

	var err C.UErrorCode = C.U_ZERO_ERROR
	bi := C.ubrk_open(C.UBRK_WORD, t.locale, myUCharPointer, -1, &err)

	if err > C.U_ZERO_ERROR {
		return rv
	}

	defer C.ubrk_close(bi)

	position := 0
	var prev C.int32_t
	p := C.ubrk_first(bi)
	for p != C.UBRK_DONE {

		q := C.ubrk_getRuleStatus(bi)

		// convert boundaries back to utf8 positions
		var nilCString *C.char
		var indexA C.int32_t

		C.u_strToUTF8(nilCString, 0, &indexA, myUCharPointer, prev, &err)
		if err > C.U_ZERO_ERROR && err != C.U_BUFFER_OVERFLOW_ERROR {
			return rv
		} else {
			err = C.U_ZERO_ERROR
		}

		var indexB C.int32_t
		C.u_strToUTF8(nilCString, 0, &indexB, myUCharPointer, p, &err)
		if err > C.U_ZERO_ERROR && err != C.U_BUFFER_OVERFLOW_ERROR {
			return rv
		} else {
			err = C.U_ZERO_ERROR
		}

		if q != 0 {
			position += 1
			token := analysis.Token{
				Start:    int(indexA),
				End:      int(indexB),
				Term:     input[indexA:indexB],
				Position: position,
				Type:     analysis.AlphaNumeric,
			}
			if q == 100 {
				token.Type = analysis.Numeric
			}
			if q == 400 {
				token.Type = analysis.Ideographic
			}
			rv = append(rv, &token)
		}
		prev = p
		p = C.ubrk_next(bi)
	}

	return rv
}