1
- package main
1
+ package kmp
2
2
3
3
import (
4
4
"fmt"
@@ -10,53 +10,65 @@ import (
10
10
// User defined.
11
11
// Set to true to read input from two command line arguments
12
12
// Set to false to read input from two files "pattern.txt" and "text.txt"
13
- const commandLineInput bool = false
13
+ const isTakingInputFromCommandLine bool = true
14
+ const notFoundPosition int = - 1
15
+
16
+ type result struct {
17
+ resultPosition int
18
+ numberOfComparison int
19
+ }
14
20
15
21
// Implementation of Knuth-Morris-Pratt algorithm (Prefix based approach).
16
22
// Requires either a two command line arguments separated by a single space,
17
23
// or two files in the same folder: "pattern.txt" containing the string to
18
24
// be searched for, "text.txt" containing the text to be searched in.
19
25
func main () {
20
- if commandLineInput == true { // case of command line input
26
+ var text string
27
+ var word string
28
+
29
+ if isTakingInputFromCommandLine { // case of command line input
21
30
args := os .Args
22
31
if len (args ) <= 2 {
23
32
log .Fatal ("Not enough arguments. Two string arguments separated by spaces are required!" )
24
33
}
25
- pattern : = args [1 ]
26
- s : = args [2 ]
34
+ word = args [1 ]
35
+ text = args [2 ]
27
36
for i := 3 ; i < len (args ); i ++ {
28
- s = s + " " + args [i ]
37
+ text = text + " " + args [i ]
29
38
}
30
- if len (args [1 ]) > len (s ) {
31
- log .Fatal ("Pattern is longer than text!" )
32
- }
33
- fmt .Printf ("\n Running: Knuth-Morris-Pratt algorithm.\n \n " )
34
- fmt .Printf ("Search word (%d chars long): %q.\n " , len (args [1 ]), pattern )
35
- fmt .Printf ("Text (%d chars long): %q.\n \n " , len (s ), s )
36
- knp (s , pattern )
37
- } else if commandLineInput == false { // case of file input
38
- patFile , err := ioutil .ReadFile ("pattern.txt" )
39
+ } else { // case of file input
40
+ patFile , err := ioutil .ReadFile ("../pattern.txt" )
39
41
if err != nil {
40
42
log .Fatal (err )
41
43
}
42
- textFile , err := ioutil .ReadFile ("text.txt" )
44
+ textFile , err := ioutil .ReadFile ("../ text.txt" )
43
45
if err != nil {
44
46
log .Fatal (err )
45
47
}
46
- if len (patFile ) > len (textFile ) {
47
- log .Fatal ("Pattern is longer than text!" )
48
- }
49
- fmt .Printf ("\n Running: Knuth-Morris-Pratt algorithm.\n \n " )
50
- fmt .Printf ("Search word (%d chars long): %q.\n " , len (patFile ), patFile )
51
- fmt .Printf ("Text (%d chars long): %q.\n \n " , len (textFile ), textFile )
52
- knp (string (textFile ), string (patFile ))
48
+ text = string (textFile )
49
+ word = string (patFile )
50
+ }
51
+
52
+ if len (word ) > len (text ) {
53
+ log .Fatal ("Pattern is longer than text!" )
54
+ }
55
+ fmt .Printf ("\n Running: Knuth-Morris-Pratt algorithm.\n \n " )
56
+ fmt .Printf ("Search word (%d chars long): %q.\n " , len (word ), word )
57
+ fmt .Printf ("Text (%d chars long): %q.\n \n " , len (text ), text )
58
+
59
+ r := kmp (text , word )
60
+ if r .resultPosition == notFoundPosition {
61
+ fmt .Printf ("\n \n Word was not found.\n %d comparisons were done." , r .numberOfComparison )
62
+ } else {
63
+ fmt .Printf ("\n \n Word %q was found at position %d in %q. \n %d comparisons were done." , word ,
64
+ r .resultPosition , text , r .numberOfComparison )
53
65
}
54
66
}
55
67
56
- // Function knp performing the Knuth-Morris-Pratt algorithm.
68
+ // Function kmp performing the Knuth-Morris-Pratt algorithm.
57
69
// Prints whether the word/pattern was found and on what position in the text or not.
58
70
// m - current match in text, i - current character in w, c - amount of comparisons.
59
- func knp (text , word string ) {
71
+ func kmp (text string , word string ) result {
60
72
m , i , c := 0 , 0 , 0
61
73
t := kmp_table (word )
62
74
for m + i < len (text ) {
@@ -65,8 +77,9 @@ func knp(text, word string) {
65
77
if word [i ] == text [m + i ] {
66
78
fmt .Printf (" - match" )
67
79
if i == len (word )- 1 {
68
- fmt .Printf ("\n \n Word %q was found at position %d in %q. \n %d comparisons were done." , word , m , text , c )
69
- return
80
+ return result {
81
+ m , c ,
82
+ }
70
83
}
71
84
i ++
72
85
} else {
@@ -78,8 +91,9 @@ func knp(text, word string) {
78
91
}
79
92
}
80
93
}
81
- fmt .Printf ("\n \n Word was not found.\n %d comparisons were done." , c )
82
- return
94
+ return result {notFoundPosition ,
95
+ c ,
96
+ }
83
97
}
84
98
85
99
// Table building alghoritm.
0 commit comments