diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f7a479..85f17d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.1.2] - 2022-03-01 +### Fixed +- Fix a bug where a single line would cause a panic + ## [0.1.1] - 2022-02-09 ### Fixed - Don't consider empty files to have any lines @@ -39,7 +43,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - First functional version -[Unreleased]: https://github.com/SmartBear/lhdiff/compare/v0.1.1...HEAD +[Unreleased]: https://github.com/SmartBear/lhdiff/compare/v0.1.2...HEAD +[0.1.2]: https://github.com/SmartBear/lhdiff/compare/v0.1.1...v0.1.2 [0.1.1]: https://github.com/SmartBear/lhdiff/compare/v0.1.0...v0.1.1 [0.1.0]: https://github.com/SmartBear/lhdiff/compare/v0.0.5...v0.1.0 [0.0.5]: https://github.com/SmartBear/lhdiff/compare/v0.0.4...v0.0.5 diff --git a/lhdiff_test.go b/lhdiff_test.go index 231c703..ae44fd1 100644 --- a/lhdiff_test.go +++ b/lhdiff_test.go @@ -88,6 +88,33 @@ four` // 3,3 } +func ExampleLhdiff_oneDifferentLineWithNewLine() { + left := "a\n" + right := "b\n" + mappings, err := Lhdiff(left, right, 4, true) + printErr(err) + err = PrintMappings(mappings) + printErr(err) + + // Output: + // 1,_ + // 2,2 + // _,1 +} + +func ExampleLhdiff_oneModifiedLineWithNewLine() { + left := "a b c d\n" + right := "a b c d e\n" + mappings, err := Lhdiff(left, right, 4, true) + printErr(err) + err = PrintMappings(mappings) + printErr(err) + + // Output: + // 1,1 + // 2,2 +} + func ExampleLhdiff_withEmptyLeft() { right := `one two diff --git a/tfidf_cosine_similarity.go b/tfidf_cosine_similarity.go index 094a35e..c4f9988 100644 --- a/tfidf_cosine_similarity.go +++ b/tfidf_cosine_similarity.go @@ -20,6 +20,9 @@ func TfIdfCosineSimilarity(docA string, docB string) float64 { ) allTokens = append(allTokens, tokensA...) allTokens = append(allTokens, tokensB...) + if len(allTokens) == 0 { + return 1 + } var documentFrequency = map[string]int{} for _, token := range allTokens { if documentFrequency[token] == 0 {