Created
June 27, 2014 05:40
-
-
Save defp/4e7e4ef45077c289fcd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"log" | |
"os" | |
"path/filepath" | |
"regexp" | |
"strings" | |
) | |
func main() { | |
inFileName, outFileName, err := filenamesFromCmd() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
if inFileName == "" || outFileName == "" { | |
fmt.Println("inFileName or outFileName is null") | |
os.Exit(1) | |
} | |
inFile, outFile := os.Stdin, os.Stdout | |
if inFile, err = os.Open(inFileName); err != nil { | |
log.Fatal(err) | |
} | |
defer inFile.Close() | |
if outFile, err = os.OpenFile(outFileName, os.O_WRONLY, 0644); err != nil { | |
log.Fatal(err) | |
} | |
defer outFile.Close() | |
if err = americanise(inFile, outFile); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func filenamesFromCmd() (inFilename, outFileName string, err error) { | |
inFilename = "" | |
outFileName = "" | |
argsLen := len(os.Args) | |
if argsLen > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help") { | |
err = fmt.Errorf("usage: %s [<]infile.txt [>]outfile.txt", | |
filepath.Base(os.Args[0])) | |
return "", "", err | |
} | |
if argsLen > 1 { | |
inFilename = os.Args[1] | |
} | |
if argsLen > 2 { | |
outFileName = os.Args[2] | |
} else { | |
outFileName = inFilename | |
} | |
return inFilename, outFileName, nil | |
} | |
func americanise(inFile io.Reader, outFile io.Writer) (err error) { | |
reader := bufio.NewReader(inFile) | |
writer := bufio.NewWriter(outFile) | |
var replacer func(string) string | |
if replacer, err = makeReplacer("british-american.txt"); err != nil { | |
return err | |
} | |
wordRegexp := regexp.MustCompile("[A-Za-z]+") | |
eof := false | |
for !eof { | |
var line string | |
line, err = reader.ReadString('\n') | |
if err == io.EOF { | |
err = nil | |
eof = true | |
} else if err != nil { | |
return err | |
} | |
line = wordRegexp.ReplaceAllStringFunc(line, replacer) | |
fmt.Println(line) | |
if _, err = writer.WriteString(line); err != nil { | |
return err | |
} | |
} | |
err = writer.Flush() | |
return err | |
} | |
func makeReplacer(filename string) (func(string) string, error) { | |
rawBytes, err := ioutil.ReadFile(filename) | |
if err != nil { | |
return nil, err | |
} | |
text := string(rawBytes) | |
lines := strings.Split(text, "\n") | |
usOfBritish := make(map[string]string) | |
for _, line := range lines { | |
fileds := strings.Fields(line) | |
if len(fileds) == 2 { | |
usOfBritish[fileds[0]] = fileds[1] | |
} | |
} | |
replacer := func(word string) string { | |
if usWord, isFound := usOfBritish[word]; isFound { | |
return usWord | |
} | |
return word | |
} | |
return replacer, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment