Esempio n. 1
0
func CalcDiffOfSourceLine(a, b string, mx int) int {
	if a == b {
		return 0
	} // if

	delT, insT := LineToTokens(a), LineToTokens(b)
	delK, insK := isKeywords(delT), isKeywords(insT)

	diff := ed.EditDistanceF(len(delT), len(insT), func(iA, iB int) int {
		if delT[iA] == insT[iB] {
			return 0
		} // if
		return 50
	}, func(iA int) int {
		if delK[iA] {
			return 2
		}
		return 1
	}, func(iB int) int {
		if insK[iB] {
			return 2
		}

		return 1
	})

	return diff * mx / (len(delT) + len(insT))
}
Esempio n. 2
0
func (f *fragment) calcDiff(that diffFragment) int {
	switch g := that.(type) {
	case *fragment:
		if f == nil {
			if g == nil {
				return 0
			} else {
				return f.Weight() + g.Weight()
			} // else
		} // if
		if g == nil {
			return f.Weight() + g.Weight()
		} // if

		switch f.Type() {
		case df_STAR:
			if g.Type() == df_STAR {
				return f.Parts[0].calcDiff(g.Parts[0])
			} // if

			return f.Parts[0].calcDiff(g) + 50
		}

		if g.Type() == df_STAR {
			return f.calcDiff(g.Parts[0]) + 50
		} // if

		if f.Type() != g.Type() {
			return f.Weight() + g.Weight()
		} // if

		switch f.Type() {
		case df_FUNC:
			res := int(0)
			for i := 0; i < 4; i++ {
				res += f.Parts[i].calcDiff(g.Parts[i])
			} // for i

			res += int(math.Sqrt(float64(f.Parts[4].calcDiff(g.Parts[4]))/100.) * 100)

			return res
		}

		return ed.EditDistanceF(len(f.Parts), len(g.Parts), func(iA, iB int) int {
			return f.Parts[iA].calcDiff(g.Parts[iB]) * 3 / 2
		}, func(iA int) int {
			return f.Parts[iA].Weight()
		}, func(iB int) int {
			return g.Parts[iB].Weight()
		})
	}
	return f.Weight() + that.Weight()
}