//BuildTimeNodes will insert time nodes if a valid response from date service is received for its place
//This function won't replace any existing tokens. If conflict between existing node and time node come,
// the time node will be skipped with priority given to the existing node.
BuildTimeNodes will insert time nodes if a valid response from date service is received for its place
This function won't replace any existing tokens. If conflict between existing node and time node come,
the time node will be skipped with priority given to the existing node.
// BuildTimeNodes will insert time nodes if a valid response from date service is received for its place // This function won ' t replace any existing tokens . If conflict between existing node and time node come // the time node will be skipped with priority given to the existing node .
func BuildTimeNodes(toks []Token, ch chan datetime.Results) []Token {
	result := &datetime.Results{}
	select {
	case res := <-ch:
		result = &res
	case <-time.After(3 * time.Second):
		break
	}
	if !result.IsValid() {
		return toks
	}
	validResults := []datetime.Response{}
	for _, resp := range result.Res {
		if resp.IsValid() {
			validResults = append(validResults, resp)
		}
	}
	sort.Sort(timeResults(validResults))
	i, j := 0, 0
	for i < len(validResults) {
		result := validResults[i]
		startIndex := result.Start
		endIndex := result.End
		if endIndex < toks[j].Pos {
			tok := Token{
				Pos:  startIndex,
				Word: []rune(result.Body),
				Nodes: []Node{
					&TimeNode{
						UID:   "T" + strconv.Itoa(i),
						Word:  []rune(result.Value.Value),
						Value: result.Value,
					},
				},
			}
			toks = append(toks[:j], append([]Token{tok}, toks[j:]...)...)
			i++
		} else if startIndex <= toks[j].Pos {
			i++
			j++
		} else if startIndex > toks[j].Pos {
			j++
		}
		if j >= len(toks) {
			timeNodes := []Token{}
			for i < len(validResults) {
				result = validResults[i]
				startIndex = result.Start
				timeNodes = append(timeNodes, Token{
					Pos:  startIndex,
					Word: []rune(result.Body),
					Nodes: []Node{
						&TimeNode{
							UID:   "T" + strconv.Itoa(i),
							Word:  []rune(result.Body),
							Value: result.Value,
						},
					},
				})
				i++
			}
			toks = append(toks, timeNodes...)
		}
	}
	return toks
}
func BuildTimeNodes ( toks [ ] Token , ch chan datetime . Results ) [ ] Token { result := & datetime . Results { } select { case res := <- ch : result = & res case <- time . After ( 3 * time . Second ) : break } if ! result . IsValid ( ) { return toks } validResults := [ ] datetime . Response { } for _ , resp := range result . Res { if resp . IsValid ( ) { validResults = append ( validResults , resp ) } } sort . Sort ( timeResults ( validResults ) ) i , j := 0 , 0 for i < len ( validResults ) { result := validResults [ i ] startIndex := result . Start endIndex := result . End if endIndex < toks [ j ] . Pos { tok := Token { Pos : startIndex , Word : [ ] rune ( result . Body ) , Nodes : [ ] Node { & TimeNode { UID : " " + strconv . Itoa ( i ) , Word : [ ] rune ( result . Value . Value ) , Value : result . Value , } , } , } toks = append ( toks [ : j ] , append ( [ ] Token { tok } , toks [ j : ] ... ) ... ) i ++ } else if startIndex <= toks [ j ] . Pos { i ++ j ++ } else if startIndex > toks [ j ] . Pos { j ++ } if j >= len ( toks ) { timeNodes := [ ] Token { } for i < len ( validResults ) { result = validResults [ i ] startIndex = result . Start timeNodes = append ( timeNodes , Token { Pos : startIndex , Word : [ ] rune ( result . Body ) , Nodes : [ ] Node { & TimeNode { UID : " " + strconv . Itoa ( i ) , Word : [ ] rune ( result . Body ) , Value : result . Value , } , } , } ) i ++ } toks = append ( toks , timeNodes ... ) } } return toks }
BuildTimeNodes will insert time nodes if a valid response from date service is received for its place
This function won't replace any existing tokens.
BuildTimeNodes will insert time nodes if a valid response from date service is received for its place This function won ' t replace any existing tokens .
/*
	 * Then we will wait for the channel to dump results
	 * We will check whether the results are valid or not
	 * Then we will process it
	 */ //wait for the channel to dump response //checking whether the results are valid or not //processing the result //first we will sort the valid results according to the start and end position //then we will insert the tokens for time nodes in between the tokens //if the end index is < the token's position, then we can //insert the result as a time node to the tokens //if the time result has intersected a known node //then priority is for the known know node, so we skip and move ahead //we haven't reached the relevant position in the token //we have exhausted the token list now we will append //the remianing time results to the tokens list