<commit_before>package main

import (
	"fmt"
	"github.com/russross/blackfriday"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
)

const (
	HELP = `md2xml [-h] file.md
Transform a given Markdown file into XML.
-h        To print this help page.
-x        Print intermediate XHTML output.
file.md   The markdown file to convert.
Note: this program calls xsltproc that must have been installed.`
	STYLESHEET_ARTICLE = `<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:output method="xml" encoding="UTF-8"/>
  <xsl:param name="id">ID</xsl:param>
  <xsl:param name="date">DATE</xsl:param>
  <xsl:param name="title">TITLE</xsl:param>

  <xsl:template match="/xhtml">
    <xsl:text disable-output-escaping="yes">
    <!DOCTYPE blog PUBLIC "-//CAFEBABE//DTD blog 1.0//EN"
                             "../dtd/article.dtd">
    </xsl:text>

      <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
      <xsl:attribute name="date"><xsl:value-of select="$date"/></xsl:attribute>
      <title><xsl:value-of select="$title"/></title>
      <xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="h1">
    <sect level="1"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h2">
    <sect level="2"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h3">
    <sect level="3"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h4">
    <sect level="4"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h5">
    <sect level="5"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h6">
    <sect level="6"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="p[count(text())=0 and count(code)=1]">
    <source><xsl:apply-templates select="code"/></source>
  </xsl:template>

  <xsl:template match="p[count(text())=1 and count(img)=1]">
    <xsl:apply-templates select="img"/>
  </xsl:template>

  <xsl:template match="img">

  </xsl:template>

  <xsl:template match="p">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="ul">
    <list><xsl:apply-templates/></list>
  </xsl:template>

  <xsl:template match="ol">
    <enum><xsl:apply-templates/></enum>
  </xsl:template>

  <xsl:template match="li">
    <item><xsl:apply-templates/></item>
  </xsl:template>

  <xsl:template match="table">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="th">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="tr">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="td">
    <co><xsl:apply-templates/></co>
  </xsl:template>

  <xsl:template match="pre">
    <source><xsl:apply-templates/></source>
  </xsl:template>

  <xsl:template match="em">
    <term><xsl:apply-templates/></term>
  </xsl:template>

  <xsl:template match="strong">
    <imp><xsl:apply-templates/></imp>
  </xsl:template>

  <xsl:template match="a">
    <link url="{@href}"><xsl:apply-templates/></link>
  </xsl:template>

</xsl:stylesheet>`
	STYLESHEET_BLOG = `<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:output method="xml" encoding="UTF-8"/>
  <xsl:param name="id">ID</xsl:param>
  <xsl:param name="date">DATE</xsl:param>
  <xsl:param name="title">TITLE</xsl:param>

  <xsl:template match="/xhtml">
    <xsl:text disable-output-escaping="yes">
    <!DOCTYPE blog PUBLIC "-//CAFEBABE//DTD blog 1.0//EN"
                             "../dtd/blog.dtd">
    </xsl:text>
    <blog>
      <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
      <xsl:attribute name="date"><xsl:value-of select="$date"/></xsl:attribute>
      <title><xsl:value-of select="$title"/></title>
      <xsl:apply-templates/>
    </blog>
  </xsl:template>

  <xsl:template match="p[count(text())=0 and count(code)=1]">
    <source><xsl:apply-templates select="code"/></source>
  </xsl:template>

  <xsl:template match="p[count(text())=1 and count(img)=1]">
    <xsl:apply-templates select="img"/>
  </xsl:template>

  <xsl:template match="img">

  </xsl:template>

  <xsl:template match="p">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="ul">
    <list><xsl:apply-templates/></list>
  </xsl:template>

  <xsl:template match="ol">
    <enum><xsl:apply-templates/></enum>
  </xsl:template>

  <xsl:template match="li">
    <item><xsl:apply-templates/></item>
  </xsl:template>

  <xsl:template match="table">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="th">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="tr">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="td">
    <co><xsl:apply-templates/></co>
  </xsl:template>

  <xsl:template match="pre">
    <source><xsl:apply-templates/></source>
  </xsl:template>

  <xsl:template match="em">
    <term><xsl:apply-templates/></term>
  </xsl:template>

  <xsl:template match="strong">
    <imp><xsl:apply-templates/></imp>
  </xsl:template>

  <xsl:template match="a">
    <link url="{@href}"><xsl:apply-templates/></link>
  </xsl:template>

</xsl:stylesheet>`
	XHTML_HEADER = "<xhtml>\n"
	XHTML_FOOTER = "\n</xhtml>"
)

func processXsl(xmlFile string, data map[string]string, article bool) []byte {
	xslFile, err := ioutil.TempFile("/tmp", "md2xsl-")
	if err != nil {
		panic(err)
	}
	stylesheet := STYLESHEET_ARTICLE
	if !article {
		stylesheet = STYLESHEET_BLOG
	}
	err = ioutil.WriteFile(xslFile.Name(), []byte(stylesheet), 0755)
	if err != nil {
		panic(err)
	}
	defer os.Remove(xslFile.Name())
	params := make([]string, 0, 2+3*len(data))
	for name, value := range data {
		params = append(params, "--stringparam")
		params = append(params, name)
		params = append(params, value)
	}
	params = append(params, xslFile.Name())
	params = append(params, xmlFile)
	command := exec.Command("xsltproc", params...)
	result, err := command.CombinedOutput()
	if err != nil {
		panic(err)
	}
	return result
}

func markdown2xhtml(markdown string) []byte {
	xhtml := blackfriday.MarkdownBasic([]byte(markdown))
	return []byte(XHTML_HEADER + string(xhtml) + XHTML_FOOTER)
}

func markdownData(text string) (map[string]string, string) {
	data := make(map[string]string)
	lines := strings.Split(text, "\n")
	var limit int
	for index, line := range lines {
		if strings.HasPrefix(line, "% ") && strings.Index(line, ":") >= 0 {
			name := strings.TrimSpace(line[2:strings.Index(line, ":")])
			value := strings.TrimSpace(line[strings.Index(line, ":")+1 : len(line)])
			data[name] = value
		} else {
			limit = index
			break
		}
	}
	return data, strings.Join(lines[limit:len(lines)], "\n")
}

func escapeXml(source string) string {
	source = strings.Replace(source, "&", "&", -1)
	source = strings.Replace(source, "<", "<", -1)
	return source
}

func processFile(filename string, printXhtml bool, article bool) string {
	source, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(err)
	}
	data, markdown := markdownData(string(source))
	markdown = escapeXml(markdown)
	xhtml := markdown2xhtml(markdown)
	if printXhtml {
		return string(xhtml)
	}
	xmlFile, err := ioutil.TempFile("/tmp", "md2xml-")
	if err != nil {
		panic(err)
	}
	defer os.Remove(xmlFile.Name())
	ioutil.WriteFile(xmlFile.Name(), xhtml, 0755)
	result := processXsl(xmlFile.Name(), data, article)
	return string(result)
}

func main() {
	xhtml := false
	article := false
	if len(os.Args) < 2 {
		fmt.Println(HELP)
		os.Exit(1)
	}
	for _, arg := range os.Args[1:] {
		if arg == "-h" || os.Args[1] == "--help" {
			fmt.Println(HELP)
			os.Exit(0)
		} else if arg == "-x" || arg == "--xhtml" {
			xhtml = true
		} else if arg == "-a" || arg == "--article" {
			article = true
		} else {
			fmt.Println(processFile(arg, xhtml, article))
		}
	}
}
<commit_msg>Fixed article<commit_after>package main

import (
	"fmt"
	"github.com/russross/blackfriday"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
)

const (
	HELP = `md2xml [-h] file.md
Transform a given Markdown file into XML.
-h        To print this help page.
-x        Print intermediate XHTML output.
file.md   The markdown file to convert.
Note: this program calls xsltproc that must have been installed.`
	STYLESHEET_ARTICLE = `<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:output method="xml" encoding="UTF-8"/>
  <xsl:param name="id">ID</xsl:param>
  <xsl:param name="date">DATE</xsl:param>
  <xsl:param name="title">TITLE</xsl:param>
  <xsl:param name="author">AUTHOR</xsl:param>
  <xsl:param name="email">EMAIL</xsl:param>

  <xsl:template match="/xhtml">
    <xsl:text disable-output-escaping="yes">
    <!DOCTYPE article PUBLIC "-//CAFEBABE//DTD blog 1.0//EN"
                             "../dtd/article.dtd">
    </xsl:text>

      <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
      <xsl:attribute name="date"><xsl:value-of select="$date"/></xsl:attribute>
      <xsl:attribute name="author"><xsl:value-of select="$author"/></xsl:attribute>
      <xsl:attribute name="email"><xsl:value-of select="$email"/></xsl:attribute>
      <title><xsl:value-of select="$title"/></title>
      <text>
       <xsl:apply-templates/>
      </text>

  </xsl:template>

  <xsl:template match="h1">
    <sect level="1"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h2">
    <sect level="2"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h3">
    <sect level="3"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h4">
    <sect level="4"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h5">
    <sect level="5"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="h6">
    <sect level="6"><title><xsl:value-of select="."/></title></sect>
  </xsl:template>

  <xsl:template match="p[count(text())=0 and count(code)=1]">
    <source><xsl:apply-templates select="code"/></source>
  </xsl:template>

  <xsl:template match="p[count(text())=1 and count(img)=1]">
    <xsl:apply-templates select="img"/>
  </xsl:template>

  <xsl:template match="img">

  </xsl:template>

  <xsl:template match="p">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="ul">
    <list><xsl:apply-templates/></list>
  </xsl:template>

  <xsl:template match="ol">
    <enum><xsl:apply-templates/></enum>
  </xsl:template>

  <xsl:template match="li">
    <item><xsl:apply-templates/></item>
  </xsl:template>

  <xsl:template match="table">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="th">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="tr">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="td">
    <co><xsl:apply-templates/></co>
  </xsl:template>

  <xsl:template match="pre">
    <source><xsl:apply-templates/></source>
  </xsl:template>

  <xsl:template match="em">
    <term><xsl:apply-templates/></term>
  </xsl:template>

  <xsl:template match="strong">
    <imp><xsl:apply-templates/></imp>
  </xsl:template>

  <xsl:template match="a">
    <link url="{@href}"><xsl:apply-templates/></link>
  </xsl:template>

</xsl:stylesheet>`
	STYLESHEET_BLOG = `<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:output method="xml" encoding="UTF-8"/>
  <xsl:param name="id">ID</xsl:param>
  <xsl:param name="date">DATE</xsl:param>
  <xsl:param name="title">TITLE</xsl:param>

  <xsl:template match="/xhtml">
    <xsl:text disable-output-escaping="yes">
    <!DOCTYPE blog PUBLIC "-//CAFEBABE//DTD blog 1.0//EN"
                             "../dtd/blog.dtd">
    </xsl:text>
    <blog>
      <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
      <xsl:attribute name="date"><xsl:value-of select="$date"/></xsl:attribute>
      <title><xsl:value-of select="$title"/></title>
      <xsl:apply-templates/>
    </blog>
  </xsl:template>

  <xsl:template match="p[count(text())=0 and count(code)=1]">
    <source><xsl:apply-templates select="code"/></source>
  </xsl:template>

  <xsl:template match="p[count(text())=1 and count(img)=1]">
    <xsl:apply-templates select="img"/>
  </xsl:template>

  <xsl:template match="img">

  </xsl:template>

  <xsl:template match="p">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="ul">
    <list><xsl:apply-templates/></list>
  </xsl:template>

  <xsl:template match="ol">
    <enum><xsl:apply-templates/></enum>
  </xsl:template>

  <xsl:template match="li">
    <item><xsl:apply-templates/></item>
  </xsl:template>

  <xsl:template match="table">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="th">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="tr">

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="td">
    <co><xsl:apply-templates/></co>
  </xsl:template>

  <xsl:template match="pre">
    <source><xsl:apply-templates/></source>
  </xsl:template>

  <xsl:template match="em">
    <term><xsl:apply-templates/></term>
  </xsl:template>

  <xsl:template match="strong">
    <imp><xsl:apply-templates/></imp>
  </xsl:template>

  <xsl:template match="a">
    <link url="{@href}"><xsl:apply-templates/></link>
  </xsl:template>

</xsl:stylesheet>`
	XHTML_HEADER = "<xhtml>\n"
	XHTML_FOOTER = "\n</xhtml>"
)

func processXsl(xmlFile string, data map[string]string, article bool) []byte {
	xslFile, err := ioutil.TempFile("/tmp", "md2xsl-")
	if err != nil {
		panic(err)
	}
	stylesheet := STYLESHEET_ARTICLE
	if !article {
		stylesheet = STYLESHEET_BLOG
	}
	err = ioutil.WriteFile(xslFile.Name(), []byte(stylesheet), 0755)
	if err != nil {
		panic(err)
	}
	defer os.Remove(xslFile.Name())
	params := make([]string, 0, 2+3*len(data))
	for name, value := range data {
		params = append(params, "--stringparam")
		params = append(params, name)
		params = append(params, value)
	}
	params = append(params, xslFile.Name())
	params = append(params, xmlFile)
	command := exec.Command("xsltproc", params...)
	result, err := command.CombinedOutput()
	if err != nil {
		panic(err)
	}
	return result
}

func markdown2xhtml(markdown string) []byte {
	xhtml := blackfriday.MarkdownBasic([]byte(markdown))
	return []byte(XHTML_HEADER + string(xhtml) + XHTML_FOOTER)
}

func markdownData(text string) (map[string]string, string) {
	data := make(map[string]string)
	lines := strings.Split(text, "\n")
	var limit int
	for index, line := range lines {
		if strings.HasPrefix(line, "% ") && strings.Index(line, ":") >= 0 {
			name := strings.TrimSpace(line[2:strings.Index(line, ":")])
			value := strings.TrimSpace(line[strings.Index(line, ":")+1 : len(line)])
			data[name] = value
		} else {
			limit = index
			break
		}
	}
	return data, strings.Join(lines[limit:len(lines)], "\n")
}

func escapeXml(source string) string {
	source = strings.Replace(source, "&", "&", -1)
	source = strings.Replace(source, "<", "<", -1)
	return source
}

func processFile(filename string, printXhtml bool, article bool) string {
	source, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(err)
	}
	data, markdown := markdownData(string(source))
	markdown = escapeXml(markdown)
	xhtml := markdown2xhtml(markdown)
	if printXhtml {
		return string(xhtml)
	}
	xmlFile, err := ioutil.TempFile("/tmp", "md2xml-")
	if err != nil {
		panic(err)
	}
	defer os.Remove(xmlFile.Name())
	ioutil.WriteFile(xmlFile.Name(), xhtml, 0755)
	result := processXsl(xmlFile.Name(), data, article)
	return string(result)
}

func main() {
	xhtml := false
	article := false
	if len(os.Args) < 2 {
		fmt.Println(HELP)
		os.Exit(1)
	}
	for _, arg := range os.Args[1:] {
		if arg == "-h" || os.Args[1] == "--help" {
			fmt.Println(HELP)
			os.Exit(0)
		} else if arg == "-x" || arg == "--xhtml" {
			xhtml = true
		} else if arg == "-a" || arg == "--article" {
			article = true
		} else {
			fmt.Println(processFile(arg, xhtml, article))
		}
	}
}
<|endoftext|>


<commit_before>package plugins

import (
	"github.com/InnovaCo/serve/manifest"
	"fmt"
)

func init() {
	manifest.PluginRegestry.Add("db.clone", DBClone{})
}

type DBClone struct{}

func (p DBClone) Run(data manifest.Manifest) error {
	if data.GetBool("purge") {
		return p.Drop(data)
	} else {
		return p.Clone(data)
	}

}

func (p DBClone) Clone(data manifest.Manifest) error {
	err := runSshCmd(
		data.GetString("server"),
		data.GetString("ssh-user"),
		fmt.Sprintf("sudo -EHu postgres createdb -O postgres %s && pg_dump %s | psql %s", data.GetString("to"), data.GetString("from"), data.GetString("to")),
	)
	if err != nil {
		// ToDo analize db exist
		return err
	}
	return nil
}

func (p DBClone) Drop(data manifest.Manifest) error {
	return runSshCmd(
		data.GetString("server"),
		data.GetString("ssh-user"),
		fmt.Sprintf("sudo -EHu postgres dropdb %s", data.GetString("to")),
	)
}
<commit_msg>fix cmd, add owner<commit_after>package plugins

import (
	"github.com/InnovaCo/serve/manifest"
	"fmt"
)

func init() {
	manifest.PluginRegestry.Add("db.clone", DBClone{})
}

type DBClone struct{}

func (p DBClone) Run(data manifest.Manifest) error {
	if data.GetBool("purge") {
		return p.Drop(data)
	} else {
		return p.Clone(data)
	}

}

func (p DBClone) Clone(data manifest.Manifest) error {
	f := data.GetString("from")
	t := data.GetString("to")

	err := runSshCmd(
		data.GetString("server"),
		data.GetString("ssh-user"),
		fmt.Sprintf("sudo -EHu postgres createdb -O "+
			"`psql postgres -c \"SELECT d.datname, pg_catalog.pg_get_userbyid(d.datdba) FROM pg_catalog.pg_database d "+
			"WHERE d.datname='%s' ORDER BY 1;\" | grep %s | awk '{print $3}'` %s && pg_dump %s | psql %s", f, f, t, f, t),
	)
	if err != nil {
		// ToDo analize db exist
		return err
	}
	return nil
}

func (p DBClone) Drop(data manifest.Manifest) error {
	return runSshCmd(
		data.GetString("server"),
		data.GetString("ssh-user"),
		fmt.Sprintf("sudo -EHu postgres dropdb %s", data.GetString("to")),
	)
}
<|endoftext|>


<commit_before>package main

import (
    "database/sql"
    "encoding/json"
    "fmt"
    "image"
    "image/color"
    "image/draw"
    "image/png"
    "io"
    "io/ioutil"
    "log"
    "os"
    "os/exec"
    re "regexp"
    "sort"
    "strconv"
    s "strings"
    "sync"
    "time"
    "jfb/svgxml"
    "github.com/golang/freetype"
    _ "github.com/lib/pq"
)

type Config struct {
    Colours     map[string]string                       `json:"colours"`
    DefaultFont string                                  `json:"annotation_font_default"`
    Maps        map[string]map[string]map[string]string `json:"maps"`
}

type DbConfig struct {
    Server      map[string]string       `json:"db_server"`
    Creds       map[string]string       `json:"db_creds"`
    Schema      map[string]string       `json:"db_schema"`
}

// set up integer array sorting
type IntArray []int
func (list IntArray) Len() int          { return len(list) }
func (list IntArray) Swap(a, b int)     { list[a], list[b] = list[b], list[a] }
func (list IntArray) Less(a, b int) bool { return list[a] < list[b] }

// suck in count data
func db_data() (map[string]int, map[string]int) {
    var dbconfig        DbConfig

    jsoncfg, err := ioutil.ReadFile("dbconfig.json")
    if err != nil {
        panic(err)
    }

    err = json.Unmarshal(jsoncfg, &dbconfig)
    if err != nil {
        fmt.Fprintf(os.Stderr, "DB config unmarshal: ")
        panic(err)
    }

    state_counts :=     make(map[string]int)
    county_counts :=    make(map[string]int)

    dbh, err := sql.Open(dbconfig.Server["dbtype"],
        dbconfig.Server["dbtype"] + "://" + dbconfig.Creds["username"] + ":" +
        dbconfig.Creds["password"] + "@" + dbconfig.Server["dbhost"] + "/" +
        dbconfig.Server["dbname"] + dbconfig.Server["dbopts"])
    if err != nil {
        log.Fatal(err)
    }

    query :=
            "select " +
                dbconfig.Schema["state_column"] + ", " +
                dbconfig.Schema["county_column"] + ", " +
                dbconfig.Schema["tally_column"] +
            "from " +
                dbconfig.Schema["tables"] + " " +
            dbconfig.Schema["where"] + " " +
            dbconfig.Schema["group_by"]
    rows, err := dbh.Query(query)
    // fmt.Println(query)
    if err != nil {
        log.Fatal(err)
    }

    defer rows.Close()
    for rows.Next() {
        var state, county string
        var count int
        if err := rows.Scan(&state, &county, &count); err != nil {
            log.Fatal(err)
        }
        state_counts[state] += count
        state_county_key := s.Replace(state + " " + county, " ", "_", -1)
        county_counts[state_county_key] = count
    }
    if err := rows.Err(); err != nil {
        log.Fatal(err)
    }

    return state_counts, county_counts

}

func colour_svgdata(mapsvg_obj *svgxml.SVG, data map[string]int, re_fill *re.Regexp, colours map[string]string, mincount []int) (string, []string) {

    var errors []string

    for id, count := range data {
        for _, mc := range mincount {
            if count >= mc {
                element := svgxml.FindPathById(mapsvg_obj, id)
                if element != nil {
                    element.Style = string(re_fill.ReplaceAll([]byte(element.Style), []byte("${1}" + colours[strconv.Itoa(mc)])))
                } else {
                    errors = append(errors, "'" + id + "' not found")
                }
            }
        }
    }
    return string(svgxml.SVG2XML(mapsvg_obj, true)), errors
}

func annotate(img *image.RGBA, fontfile string, attrs map[string]string, data map[string]int) {
    fontdata, err := ioutil.ReadFile(fontfile)
    if err != nil {
        log.Fatal(err)
    }
    font, err := freetype.ParseFont(fontdata)
    if err != nil {
        log.Fatal(err)
    }

    ann_x, _ := strconv.ParseInt(attrs["annotation_x"], 10, 64)
    ann_y, _ := strconv.ParseInt(attrs["annotation_y"], 10, 64)

    fontsize, _ := strconv.ParseFloat(attrs["annotation_sz"], 64)
    ft_ctx := freetype.NewContext()
    ft_ctx.SetDPI(72.0)
    ft_ctx.SetFont(font)
    ft_ctx.SetFontSize(fontsize)
    ft_ctx.SetClip(img.Bounds())
    ft_ctx.SetDst(img)
    ft_ctx.SetSrc(image.Black)
    pt := freetype.Pt(int(ann_x), int(ann_y)+int(ft_ctx.PointToFixed(fontsize) >> 6))

    total_hits := 0;
    for _, hits := range data {
        total_hits += hits
    }
    regions := len(data)
    if len(attrs["regions_adjust"]) > 0 {
        adj, _ := strconv.Atoi(attrs["regions_adjust"])
        regions += adj
    }

    annotation := s.Replace(attrs["annotation"], "%t%", strconv.Itoa(total_hits), -1)
    annotation = s.Replace(annotation, "%c%", strconv.Itoa(regions), -1)
    if s.Index(annotation, "%T%") >= 0 {
        annotation = s.Replace(annotation, "%T%", time.Now().Format(attrs["annotation_timefmt"]), -1)
    }
    ann_lines := s.Split(annotation, "\n")
    for _, line := range ann_lines {
        _, err = ft_ctx.DrawString(line, pt)
        if err != nil {
            log.Fatal(err)
        }
        pt.Y += ft_ctx.PointToFixed(fontsize * 1.5)
    }
}

func main() {

    var config  Config
    var wg      sync.WaitGroup

    jsoncfg, err := ioutil.ReadFile("config.json")
    if err != nil {
        panic(err)
    }

    err = json.Unmarshal(jsoncfg, &config)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Config unmarshal: ")
        panic(err)
    }

    // make sorted list of keys (minimum counts) for later comparisons
    mincount := make([]int, len(config.Colours))
    i := 0
    for k, _ := range config.Colours {
        k_i, _ := strconv.ParseInt(k, 0, 64)
        mincount[i] = int(k_i)
        i++
    }

    sort.Sort(IntArray(mincount))

    re_fill, err := re.Compile(`(fill:#)......`)
    if err != nil {
        panic(err)
    }

    re_svgext, err := re.Compile(`\.svg$`)
    if err != nil {
        panic(err)
    }

    state_data, county_data := db_data()

    for maptype, mapset := range config.Maps {
        var data map[string]int

        if maptype == "states" {
            data = state_data
        } else {
            data = county_data
        }

        for infile, attrs := range mapset {
            wg.Add(1)
            // go func(srcfile, dstfile, outsize, maptype string, mapdata map[string]int)
            go func(srcfile string, attrs map[string]string, maptype string, mapdata map[string]int) {

                var county_data_new map[string]int

                defer wg.Done()
                defer os.Stderr.Close()

                mapsvg, err := ioutil.ReadFile(srcfile)
                if err != nil {
                    fmt.Fprintf(os.Stderr, "can't read '" + srcfile + "': " + err.Error())
                    return
                }

                mapsvg_obj := svgxml.XML2SVG(mapsvg)
                if mapsvg_obj == nil {
                    fmt.Fprintf(os.Stderr, "can't create SVG object from " + srcfile)
                    return
                }

                if maptype == "counties" {
                    // This block has the effect of pruning county data for
                    // *states* that don't appear in the given map. This is so
                    // that counties in states outside the map don't cause
                    // error messages and counties in the map that have a
                    // different (incorrect)( name in the data do generate
                    // errors.

                    var map_state_list []string

                    county_data_new = make(map[string]int)

                    // first, make a list of all states in the map using
                    // state_data as the source of state names
                    for _, g := range mapsvg_obj.G {
                        for state, _ := range state_data {
                            if s.Index(g.Id, state + "_") == 0 {
                                map_state_list = append(map_state_list, state + "_")
                            }
                        }
                    }

                    // next, search county names in data for states found in
                    // the map and copy only county data entries for those
                    // found states
                    for state_county, sc_count := range mapdata {
                        found_state := false
                        for _, state_ := range map_state_list {
                            if s.Index(state_county, state_) == 0 {
                                found_state = true
                                break
                            }
                        }
                        if found_state == true {
                            county_data_new[state_county] = sc_count
                        }
                    }

                    // replace the function-local dataset with the pruned data
                    mapdata = county_data_new
                }

                svg_coloured, errlist := colour_svgdata(mapsvg_obj, mapdata, re_fill, config.Colours, mincount)
                if len(errlist) > 0 {
                    for _, errmsg := range errlist {
                        fmt.Fprintf(os.Stderr, "%s: %s\n", srcfile, errmsg)
                    }
                }

                ret := re_svgext.Find([]byte(attrs["outfile"]))
                if ret == nil {
                    // going to call ImageMagick's 'convert' because I can't find
                    // a damn SVG package that can write to a non-SVG image and I
                    // don't have the chops to write one.
                    cmd := exec.Command("convert", "svg:-", "-resize", attrs["outsize"], "png:-")
                    convert_stdin, err := cmd.StdinPipe()
                    if err != nil {
                        log.Fatal(err)
                    }
                    go func() {
                        defer convert_stdin.Close()
                        io.WriteString(convert_stdin, svg_coloured)
                    }()

                    // grab PNG data and cram it into an RGBA image
                    png_data, err := cmd.Output()
                    if err != nil {
                        log.Fatal(err)
                    }
                    png_reader := s.NewReader(string(png_data))
                    img, _, err := image.Decode(png_reader)
                    b := img.Bounds()
                    img_rgba := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
                    draw.Draw(img_rgba, img_rgba.Bounds(), img, b.Min, draw.Src)

                    fontdata_l, err := ioutil.ReadFile("/usr/local/share/fonts/bitstream-vera/Vera.ttf")
                    if err != nil {
                        log.Fatal(err)
                    }
                    font_l, err := freetype.ParseFont(fontdata_l)
                    if err != nil {
                        log.Fatal(err)
                    }
                    fontsize_l := 12.0
                    ft_ctx := freetype.NewContext()
                    ft_ctx.SetDPI(72.0)
                    ft_ctx.SetFont(font_l)
                    ft_ctx.SetFontSize(fontsize_l)
                    ft_ctx.SetClip(b)
                    ft_ctx.SetDst(img_rgba)
                    ft_ctx.SetSrc(image.Black)

                    legend_elem_width := 48
                    legend_elem_height := 16
                    legend_height := len(config.Colours) * (legend_elem_height + 1) - 1
                    box_x := b.Dx() - legend_elem_width
                    box_y := b.Dy() - legend_height
                    for i, mc := range mincount {
                        c_red, _ := strconv.ParseUint(config.Colours[strconv.Itoa(mc)][0:2], 16, 8)
                        c_green, _ := strconv.ParseUint(config.Colours[strconv.Itoa(mc)][2:4], 16, 8)
                        c_blue, _ := strconv.ParseUint(config.Colours[strconv.Itoa(mc)][4:6], 16, 8)
                        fill := color.RGBA{uint8(c_red), uint8(c_green), uint8(c_blue), 255}
                        draw.Draw(img_rgba, image.Rect(box_x, box_y, box_x + legend_elem_width, box_y + legend_elem_height),
                                    &image.Uniform{fill}, image.ZP, draw.Src)
                        box_y += legend_elem_height + 1

                        label := strconv.Itoa(mc)
                        if i == len(mincount) - 1 {
                            label = label + "+"
                        } else if mincount[i+1] != (mc + 1) {
                            label = label + "-" + strconv.Itoa(mincount[i+1] - 1)
                        }
                        fpt := freetype.Pt(box_x + 4, box_y - legend_elem_height + int(ft_ctx.PointToFixed(fontsize_l) >> 6))
                        _, err = ft_ctx.DrawString(label, fpt)
                        if err != nil {
                            log.Fatal(err)
                        }
                    }

                    fontfile := config.DefaultFont
                    if len(attrs["annotation_font_override"]) > 0 {
                        fontfile = attrs["annotation_font_override"]
                    }

                    annotate(img_rgba, fontfile, attrs, mapdata)

                    outfile_handle, err := os.Create(attrs["outfile"])
                    if err != nil {
                        log.Fatal(err)
                    }
                    if err := png.Encode(outfile_handle, img_rgba); err != nil {
                        outfile_handle.Close()
                        log.Fatal(err)
                    }
                } else {
                    // just going back to an SVG file
                    err := ioutil.WriteFile(attrs["outfile"], []byte(svg_coloured), 0666)
                    if err != nil {
                        fmt.Fprintf(os.Stderr, "can't write to '" + attrs["outfile"] + "': " + err.Error())
                        return
                    }
                }

            }(infile, attrs, maptype, data)
        }
    }

    wg.Wait()

}

// vim:ts=4:et:
// ex:ai:sw=4:ts=1000:
<commit_msg>create 'defaults' config section for legends and annotations<commit_after>package main

import (
    "database/sql"
    "encoding/json"
    "fmt"
    "image"
    "image/color"
    "image/draw"
    "image/png"
    "io"
    "io/ioutil"
    "log"
    "os"
    "os/exec"
    re "regexp"
    "sort"
    "strconv"
    s "strings"
    "sync"
    "time"
    "jfb/svgxml"
    "github.com/golang/freetype"
    _ "github.com/lib/pq"
)

type Config struct {
    Colours     map[string]string                       `json:"colours"`
    LADefaults  map[string]string                       `json:"legend_annotation_defaults"`
    Maps        map[string]map[string]map[string]string `json:"maps"`
}

type DbConfig struct {
    Server      map[string]string       `json:"db_server"`
    Creds       map[string]string       `json:"db_creds"`
    Schema      map[string]string       `json:"db_schema"`
}

// set up integer array sorting
type IntArray []int
func (list IntArray) Len() int          { return len(list) }
func (list IntArray) Swap(a, b int)     { list[a], list[b] = list[b], list[a] }
func (list IntArray) Less(a, b int) bool { return list[a] < list[b] }

// suck in count data
func db_data() (map[string]int, map[string]int) {
    var dbconfig        DbConfig

    jsoncfg, err := ioutil.ReadFile("dbconfig.json")
    if err != nil {
        panic(err)
    }

    err = json.Unmarshal(jsoncfg, &dbconfig)
    if err != nil {
        fmt.Fprintf(os.Stderr, "DB config unmarshal: ")
        panic(err)
    }

    state_counts :=     make(map[string]int)
    county_counts :=    make(map[string]int)

    dbh, err := sql.Open(dbconfig.Server["dbtype"],
        dbconfig.Server["dbtype"] + "://" + dbconfig.Creds["username"] + ":" +
        dbconfig.Creds["password"] + "@" + dbconfig.Server["dbhost"] + "/" +
        dbconfig.Server["dbname"] + dbconfig.Server["dbopts"])
    if err != nil {
        log.Fatal(err)
    }

    query :=
            "select " +
                dbconfig.Schema["state_column"] + ", " +
                dbconfig.Schema["county_column"] + ", " +
                dbconfig.Schema["tally_column"] +
            "from " +
                dbconfig.Schema["tables"] + " " +
            dbconfig.Schema["where"] + " " +
            dbconfig.Schema["group_by"]
    rows, err := dbh.Query(query)
    // fmt.Println(query)
    if err != nil {
        log.Fatal(err)
    }

    defer rows.Close()
    for rows.Next() {
        var state, county string
        var count int
        if err := rows.Scan(&state, &county, &count); err != nil {
            log.Fatal(err)
        }
        state_counts[state] += count
        state_county_key := s.Replace(state + " " + county, " ", "_", -1)
        county_counts[state_county_key] = count
    }
    if err := rows.Err(); err != nil {
        log.Fatal(err)
    }

    return state_counts, county_counts

}

func colour_svgdata(mapsvg_obj *svgxml.SVG, data map[string]int, re_fill *re.Regexp, colours map[string]string, mincount []int) (string, []string) {

    var errors []string

    for id, count := range data {
        for _, mc := range mincount {
            if count >= mc {
                element := svgxml.FindPathById(mapsvg_obj, id)
                if element != nil {
                    element.Style = string(re_fill.ReplaceAll([]byte(element.Style), []byte("${1}" + colours[strconv.Itoa(mc)])))
                } else {
                    errors = append(errors, "'" + id + "' not found")
                }
            }
        }
    }
    return string(svgxml.SVG2XML(mapsvg_obj, true)), errors
}

func annotate(img *image.RGBA, legend_anno_dfl, attrs map[string]string, data map[string]int) {

    ann_x_str       := legend_anno_dfl["annotation_x"]
    ann_y_str       := legend_anno_dfl["annotation_y"]
    ann_timefmt     := legend_anno_dfl["annotation_timefmt"]
    ann_fontfile    := legend_anno_dfl["annotation_fontfile"]
    ann_fontsz_str  := legend_anno_dfl["annotation_sz"]
    ann_str         := legend_anno_dfl["annotation_str"]

    if len(attrs["annotation_x"]) > 0 {
        ann_x_str = attrs["annotation_x"]
    }
    if len(attrs["annotation_y"]) > 0 {
        ann_y_str = attrs["annotation_y"]
    }
    ann_x, err := strconv.Atoi(ann_x_str)
    ann_y, err := strconv.Atoi(ann_y_str)

    if len(attrs["annotation_fontfile"]) > 0 {
        ann_fontfile = attrs["annotation_fontfile"]
    }

    if len(attrs["annotation_fontsize"]) > 0 {
        ann_fontsz_str = attrs["annotation_sz"]
    }
    fontsize, _ := strconv.ParseFloat(ann_fontsz_str, 64)

    if len(attrs["annotation_timefmt"]) > 0 {
        ann_timefmt = attrs["annotation_timefmt"]
    }

    if len(attrs["annotation_str"]) > 0 {
        ann_str = attrs["annotation_str"]
    }

    ann_fontdata, err := ioutil.ReadFile(ann_fontfile)
    if err != nil {
        fmt.Fprintf(os.Stderr, "can't read from font file '%s': %s\n", ann_fontfile, err.Error())
        os.Exit(1)
    }
    font, err := freetype.ParseFont(ann_fontdata)
    if err != nil {
        log.Fatal(err)
    }

    ft_ctx := freetype.NewContext()
    ft_ctx.SetDPI(72.0)
    ft_ctx.SetFont(font)
    ft_ctx.SetFontSize(fontsize)
    ft_ctx.SetClip(img.Bounds())
    ft_ctx.SetDst(img)
    ft_ctx.SetSrc(image.Black)
    pt := freetype.Pt(int(ann_x), int(ann_y)+int(ft_ctx.PointToFixed(fontsize) >> 6))

    total_hits := 0;
    for _, hits := range data {
        total_hits += hits
    }
    regions := len(data)
    if len(attrs["regions_adjust"]) > 0 {
        adj, _ := strconv.Atoi(attrs["regions_adjust"])
        regions += adj
    }

    annotation := s.Replace(ann_str, "%t%", strconv.Itoa(total_hits), -1)
    annotation = s.Replace(annotation, "%c%", strconv.Itoa(regions), -1)
    if s.Index(annotation, "%T%") >= 0 {
        annotation = s.Replace(annotation, "%T%", time.Now().Format(ann_timefmt), -1)
    }
    ann_lines := s.Split(annotation, "\n")
    for _, line := range ann_lines {
        _, err = ft_ctx.DrawString(line, pt)
        if err != nil {
            log.Fatal(err)
        }
        pt.Y += ft_ctx.PointToFixed(fontsize * 1.5)
    }
}

func main() {

    var config  Config
    var wg      sync.WaitGroup

    jsoncfg, err := ioutil.ReadFile("config.json")
    if err != nil {
        panic(err)
    }

    err = json.Unmarshal(jsoncfg, &config)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Config unmarshal: ")
        panic(err)
    }

    // make sorted list of keys (minimum counts) for later comparisons
    mincount := make([]int, len(config.Colours))
    i := 0
    for k, _ := range config.Colours {
        k_i, _ := strconv.ParseInt(k, 0, 64)
        mincount[i] = int(k_i)
        i++
    }

    sort.Sort(IntArray(mincount))

    re_fill, err := re.Compile(`(fill:#)......`)
    if err != nil {
        panic(err)
    }

    re_svgext, err := re.Compile(`\.svg$`)
    if err != nil {
        panic(err)
    }

    state_data, county_data := db_data()

    for maptype, mapset := range config.Maps {
        var data map[string]int

        if maptype == "states" {
            data = state_data
        } else {
            data = county_data
        }

        for infile, attrs := range mapset {
            wg.Add(1)
            // go func(srcfile, dstfile, outsize, maptype string, mapdata map[string]int)
            go func(srcfile string, attrs map[string]string, maptype string, mapdata map[string]int) {

                var county_data_new map[string]int

                defer wg.Done()
                defer os.Stderr.Close()

                mapsvg, err := ioutil.ReadFile(srcfile)
                if err != nil {
                    fmt.Fprintf(os.Stderr, "can't read '" + srcfile + "': " + err.Error())
                    return
                }

                mapsvg_obj := svgxml.XML2SVG(mapsvg)
                if mapsvg_obj == nil {
                    fmt.Fprintf(os.Stderr, "can't create SVG object from " + srcfile)
                    return
                }

                if maptype == "counties" {
                    // This block has the effect of pruning county data for
                    // *states* that don't appear in the given map. This is so
                    // that counties in states outside the map don't cause
                    // error messages and counties in the map that have a
                    // different (incorrect)( name in the data do generate
                    // errors.

                    var map_state_list []string

                    county_data_new = make(map[string]int)

                    // first, make a list of all states in the map using
                    // state_data as the source of state names
                    for _, g := range mapsvg_obj.G {
                        for state, _ := range state_data {
                            if s.Index(g.Id, state + "_") == 0 {
                                map_state_list = append(map_state_list, state + "_")
                            }
                        }
                    }

                    // next, search county names in data for states found in
                    // the map and copy only county data entries for those
                    // found states
                    for state_county, sc_count := range mapdata {
                        found_state := false
                        for _, state_ := range map_state_list {
                            if s.Index(state_county, state_) == 0 {
                                found_state = true
                                break
                            }
                        }
                        if found_state == true {
                            county_data_new[state_county] = sc_count
                        }
                    }

                    // replace the function-local dataset with the pruned data
                    mapdata = county_data_new
                }

                svg_coloured, errlist := colour_svgdata(mapsvg_obj, mapdata, re_fill, config.Colours, mincount)
                if len(errlist) > 0 {
                    for _, errmsg := range errlist {
                        fmt.Fprintf(os.Stderr, "%s: %s\n", srcfile, errmsg)
                    }
                }

                ret := re_svgext.Find([]byte(attrs["outfile"]))
                if ret == nil {
                    // going to call ImageMagick's 'convert' because I can't find
                    // a damn SVG package that can write to a non-SVG image and I
                    // don't have the chops to write one.
                    cmd := exec.Command("convert", "svg:-", "-resize", attrs["outsize"], "png:-")
                    convert_stdin, err := cmd.StdinPipe()
                    if err != nil {
                        log.Fatal(err)
                    }
                    go func() {
                        defer convert_stdin.Close()
                        io.WriteString(convert_stdin, svg_coloured)
                    }()

                    // grab PNG data and cram it into an RGBA image
                    png_data, err := cmd.Output()
                    if err != nil {
                        log.Fatal(err)
                    }
                    png_reader := s.NewReader(string(png_data))
                    img, _, err := image.Decode(png_reader)
                    b := img.Bounds()
                    img_rgba := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
                    draw.Draw(img_rgba, img_rgba.Bounds(), img, b.Min, draw.Src)

                    fontdata_l, err := ioutil.ReadFile("/usr/local/share/fonts/bitstream-vera/Vera.ttf")
                    if err != nil {
                        log.Fatal(err)
                    }
                    font_l, err := freetype.ParseFont(fontdata_l)
                    if err != nil {
                        log.Fatal(err)
                    }
                    fontsize_l := 12.0
                    ft_ctx := freetype.NewContext()
                    ft_ctx.SetDPI(72.0)
                    ft_ctx.SetFont(font_l)
                    ft_ctx.SetFontSize(fontsize_l)
                    ft_ctx.SetClip(b)
                    ft_ctx.SetDst(img_rgba)
                    ft_ctx.SetSrc(image.Black)

                    legend_elem_width := 48
                    legend_elem_height := 16
                    legend_height := len(config.Colours) * (legend_elem_height + 1) - 1
                    box_x := b.Dx() - legend_elem_width
                    box_y := b.Dy() - legend_height
                    for i, mc := range mincount {
                        c_red, _ := strconv.ParseUint(config.Colours[strconv.Itoa(mc)][0:2], 16, 8)
                        c_green, _ := strconv.ParseUint(config.Colours[strconv.Itoa(mc)][2:4], 16, 8)
                        c_blue, _ := strconv.ParseUint(config.Colours[strconv.Itoa(mc)][4:6], 16, 8)
                        fill := color.RGBA{uint8(c_red), uint8(c_green), uint8(c_blue), 255}
                        draw.Draw(img_rgba, image.Rect(box_x, box_y, box_x + legend_elem_width, box_y + legend_elem_height),
                                    &image.Uniform{fill}, image.ZP, draw.Src)
                        box_y += legend_elem_height + 1

                        label := strconv.Itoa(mc)
                        if i == len(mincount) - 1 {
                            label = label + "+"
                        } else if mincount[i+1] != (mc + 1) {
                            label = label + "-" + strconv.Itoa(mincount[i+1] - 1)
                        }
                        fpt := freetype.Pt(box_x + 4, box_y - legend_elem_height + int(ft_ctx.PointToFixed(fontsize_l) >> 6))
                        _, err = ft_ctx.DrawString(label, fpt)
                        if err != nil {
                            log.Fatal(err)
                        }
                    }

                    annotate(img_rgba, config.LADefaults, attrs, mapdata)
                    outfile_handle, err := os.Create(attrs["outfile"])
                    if err != nil {
                        fmt.Fprintf(os.Stderr, "can't create '" + attrs["outfile"] + "': " + err.Error())
                        return
                    }
                    if err := png.Encode(outfile_handle, img_rgba); err != nil {
                        outfile_handle.Close()
                        log.Fatal(err)
                    }
                } else {
                    // just going back to an SVG file
                    err := ioutil.WriteFile(attrs["outfile"], []byte(svg_coloured), 0666)
                    if err != nil {
                        fmt.Fprintf(os.Stderr, "can't write to '" + attrs["outfile"] + "': " + err.Error())
                        return
                    }
                }

            }(infile, attrs, maptype, data)
        }
    }

    wg.Wait()

}

// vim:ts=4:et:
// ex:ai:sw=4:ts=1000:
<|endoftext|>


<commit_before>package main

import (
	"github.com/garyburd/redigo/redis"
	"log"
	"regexp"
	"strings"
)

type Markov struct {
	length int
}

func (m Markov) StoreUpdates(updates []Result, connection redis.Conn) {
	for _, update := range updates {
		if update.Message.Text != "" {
			m.Store(update.Message.Text, connection)
		}
	}
}

func (m Markov) Store(text string, c redis.Conn) {
	text = strings.ToLower(text) //todo, no lower case
	splitted := strings.Split(text, " ")

	for index, word := range splitted {
		if index < len(splitted)-1 {
			c.Do("SADD", word, splitted[index+1])
		}
	}

}

func (m Markov) Generate(seed string, connection redis.Conn) string {
	log.Printf("seed: %s\n", seed)

	seed = strings.ToLower(seed)
	splitted := strings.Split(seed, " ")

	key := string(splitted[0])

	s := []string{}

	s = append(s, key)
	for i := 1; i < 20; i++ {

		next, _ := redis.String(connection.Do("SRANDMEMBER", key))

		s = append(s, next)
		key = next

		matched, _ := regexp.MatchString(".*[\\.;!?¿¡]$", next)
		if next == "" || matched {
			break
		}
	}

	text := strings.Join(s, " ")
	log.Printf("Text: %s\n", text)
	return text
}
<commit_msg>skip word in storeUpdates if it is a command<commit_after>package main

import (
	"github.com/garyburd/redigo/redis"
	"log"
	"regexp"
	"strings"
)

type Markov struct {
	length int
}

func (m Markov) StoreUpdates(updates []Result, connection redis.Conn) {
	for _, update := range updates {
		if update.Message.Text != "" {
			m.Store(update.Message.Text, connection)
		}
	}
}

func (m Markov) Store(text string, c redis.Conn) {
	splitted := strings.Split(text, " ")

	// if the first word has '/' character skip the whole string
	if !strings.ContainsAny(splitted[0], "/") {
		for index, word := range splitted {
			if index < len(splitted)-1 {
				c.Do("SADD", word, splitted[index+1])
			}
		}
	}
}

func (m Markov) Generate(seed string, connection redis.Conn) string {
	log.Printf("seed: %s\n", seed)

	seed = strings.ToLower(seed)
	splitted := strings.Split(seed, " ")

	key := string(splitted[0])

	s := []string{}

	for i := 1; i < m.length; i++ {
		s = append(s, key)

		next, _ := redis.String(connection.Do("SRANDMEMBER", key))
		key = next

		matched, _ := regexp.MatchString(".*[\\.;!?¿¡]$", next)
		if next == "" || matched {
			break
		}
	}

	text := strings.Join(s, " ")
	log.Printf("Text: %s\n", text)
	return text
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/kataras/iris"
	log "github.com/Sirupsen/logrus"
	"gopkg.in/mgo.v2"
)

type AppsAPI struct {
	*iris.Context
	proxyRegister *ProxyRegister
}

// GET /apps
func (u AppsAPI) Get() {
	repo := u.getRepository()
	data, err := repo.FindAll()

	if err != nil {
		log.Errorf(err.Error())
		u.JSON(iris.StatusInternalServerError, err.Error())
	}

	u.JSON(iris.StatusOK, data)
}

// GET /apps/:param1 which its value passed to the id argument
func (u AppsAPI) GetBy(id string) {
	repo := u.getRepository()
	data, err := repo.FindByID(id)

	if err != nil {
		log.Errorf(err.Error())
		u.JSON(iris.StatusInternalServerError, err.Error())
	}

	u.JSON(iris.StatusOK, data)
}

// PUT /apps/:id
func (u AppsAPI) PutBy(id string) {
	repo := u.getRepository()
	apiSpec, err := repo.FindByID(id)

	if err != nil {
		log.Errorf(err.Error())
		u.JSON(iris.StatusInternalServerError, err.Error())
	}

	err = u.ReadJSON(&apiSpec)

	if err != nil {
		log.Errorf("Error when reading json: %s", err.Error())
	}

	repo.Add(&apiSpec)

	u.JSON(iris.StatusCreated, apiSpec)
}

// POST /apps
func (u AppsAPI) Post() {
	repo := u.getRepository()
	apiSpec := &APIDefinition{}
	err := u.ReadJSON(apiSpec)

	if err != nil {
		log.Errorf("Error when reading json: %s", err.Error())
	}

	repo.Add(apiSpec)

	u.JSON(iris.StatusCreated, apiSpec)
}

// DELETE /apps/:param1
func (u AppsAPI) DeleteBy(id string) {
	repo := u.getRepository()
	err := repo.Remove(id)

	if err != nil {
		log.Errorf(err.Error())
		u.JSON(iris.StatusInternalServerError, err.Error())
	}

	u.Response.SetStatusCode(iris.StatusNoContent)
}

// GetRepository gets the repository for the handlers
func (u AppsAPI) getRepository() *MongoAPISpecRepository {
	db := u.Context.Get("db").(*mgo.Database)
	repo, err := NewMongoAppRepository(db)

	if err != nil {
		log.Panic(err)
	}

	return repo
}
<commit_msg>Removed proxy from handler<commit_after>package main

import (
	"github.com/kataras/iris"
	log "github.com/Sirupsen/logrus"
	"gopkg.in/mgo.v2"
)

type AppsAPI struct {
	*iris.Context
}

// GET /apps
func (u AppsAPI) Get() {
	repo := u.getRepository()
	data, err := repo.FindAll()

	if err != nil {
		log.Errorf(err.Error())
		u.JSON(iris.StatusInternalServerError, err.Error())
	}

	u.JSON(iris.StatusOK, data)
}

// GET /apps/:param1 which its value passed to the id argument
func (u AppsAPI) GetBy(id string) {
	repo := u.getRepository()
	data, err := repo.FindByID(id)

	if err != nil {
		log.Errorf(err.Error())
		u.JSON(iris.StatusInternalServerError, err.Error())
	}

	u.JSON(iris.StatusOK, data)
}

// PUT /apps/:id
func (u AppsAPI) PutBy(id string) {
	repo := u.getRepository()
	apiSpec, err := repo.FindByID(id)

	if err != nil {
		log.Errorf(err.Error())
		u.JSON(iris.StatusInternalServerError, err.Error())
	}

	err = u.ReadJSON(&apiSpec)

	if err != nil {
		log.Errorf("Error when reading json: %s", err.Error())
	}

	repo.Add(&apiSpec)

	u.JSON(iris.StatusCreated, apiSpec)
}

// POST /apps
func (u AppsAPI) Post() {
	repo := u.getRepository()
	apiSpec := &APIDefinition{}
	err := u.ReadJSON(apiSpec)

	if err != nil {
		log.Errorf("Error when reading json: %s", err.Error())
	}

	repo.Add(apiSpec)

	u.JSON(iris.StatusCreated, apiSpec)
}

// DELETE /apps/:param1
func (u AppsAPI) DeleteBy(id string) {
	repo := u.getRepository()
	err := repo.Remove(id)

	if err != nil {
		log.Errorf(err.Error())
		u.JSON(iris.StatusInternalServerError, err.Error())
	}

	u.Response.SetStatusCode(iris.StatusNoContent)
}

// GetRepository gets the repository for the handlers
func (u AppsAPI) getRepository() *MongoAPISpecRepository {
	db := u.Context.Get("db").(*mgo.Database)
	repo, err := NewMongoAppRepository(db)

	if err != nil {
		log.Panic(err)
	}

	return repo
}
<|endoftext|>


<commit_before>package numgo

//Matrix and vector multiplication implementations
// - Dot Product
// - Cross Product
// - Tensor Product

func (a *Array64) DotProd(b *Array64) *Array64 {
	switch {
	case a.valRith(b, "DotProd"):
		return a
	case len(a.shape) == 1:
		return &Array64{
			shape:   []uint64{1},
			strides: []uint64{1, 1},
			data:    []float64{dotProd(a.data, b.data)},
			err:     nil,
			debug:   "",
			stack:   "",
		}
	}
	return a
}
<commit_msg>DotProd method docs<commit_after>package numgo

//Matrix and vector multiplication implementations
// - Dot Product
// - Cross Product
// - Tensor Product

// DotProd calculates the dot (scalar) product of two vectors.
// NOTE: Only implemented on 1-D arrays, and other sizes are NOOP
func (a *Array64) DotProd(b *Array64) *Array64 {
	switch {
	case a.valRith(b, "DotProd"):
		return a
	case len(a.shape) == 1:
		return &Array64{
			shape:   []uint64{1},
			strides: []uint64{1, 1},
			data:    []float64{dotProd(a.data, b.data)},
			err:     nil,
			debug:   "",
			stack:   "",
		}
	}
	return a
}
<|endoftext|>


<commit_before>/*
Package "matrix" provides types and operations for matrix manipulation.
*/
package matrix

type Matrix interface {
	// Return the shape of matrix, which consists of the "rows" and the "columns".
	Shape() (rows, columns int)

	// Return the "rows" of matrix.
	Rows() (rows int)

	// Return the "columns" of matrix.
	Columns() (columns int)

	// Create and return an iterator for all elements.
	All() Elements

	// Create and return an iterator for non-zero elements.
	NonZeros() Elements

	// Get an element of matrix speficied with "row" and "column".
	Get(row, column int) (element float64)
}

// Check whether a matrix "m" is square or not.
func IsSquare(m Matrix) bool {
	return m.Rows() == m.Columns()
}

// Check whether a matrix "m" is zero-matrix or not.
func IsZeros(m Matrix) bool {
	// TODO: Implement.
	return false
}
<commit_msg>Zero matrix should not have any non-zero element.<commit_after>/*
Package "matrix" provides types and operations for matrix manipulation.
*/
package matrix

type Matrix interface {
	// Return the shape of matrix, which consists of the "rows" and the "columns".
	Shape() (rows, columns int)

	// Return the "rows" of matrix.
	Rows() (rows int)

	// Return the "columns" of matrix.
	Columns() (columns int)

	// Create and return an iterator for all elements.
	All() Elements

	// Create and return an iterator for non-zero elements.
	NonZeros() Elements

	// Get an element of matrix speficied with "row" and "column".
	Get(row, column int) (element float64)
}

// Check whether a matrix "m" is square or not.
func IsSquare(m Matrix) bool {
	return m.Rows() == m.Columns()
}

// Check whether a matrix "m" is zero-matrix or not.
func IsZeros(m Matrix) bool {
	return !m.NonZeros().HasNext()
}
<|endoftext|>


<commit_before>// Package matrix provides functions for simple linear algebra
package matrix

import (
	"bytes"
	"fmt"
	"math"
	"math/rand"
	"strconv"
	"strings"
)

var (
	ErrIncompatibleSizes    = &MatrixError{"Incompatible sizes of matricies"}
	ErrInsertionOutOfBounds = &MatrixError{"The matrix you are trying to insert to hasn't got that row or column."}
	ErrOutOfBounds          = &MatrixError{"The element you are trying to access is out of bounds."}
)

// Describes error during calculations
type MatrixError struct {
	ErrorString string
}

// Return the Error's string
func (err *MatrixError) Error() string { return err.ErrorString }

// Matrix construct that holds all the information about a matrix
type Matrix struct {
	rows, cols int
	values     []float64
}

// A function that will apply an abitary transformation to an element in the matrix
type ApplyFunc func(index int, value float64) float64

// Creates a new Matrix and initializes all values to 0
func Zeros(rows, cols int) *Matrix {
	A := new(Matrix)

	A.rows = rows
	A.cols = cols
	A.values = make([]float64, rows*cols)

	return A
}

// Creates a new Matrix and initializes all values to 1
func Ones(rows, cols int) *Matrix {
	A := Zeros(rows, cols)
	A.AddNum(1)

	return A
}

// Creates a new identity matrix
func Eye(size int) *Matrix {
	A := Zeros(size, size)

	for i := 1; i <= size; i++ {
		A.Set(i, i, 1)
	}

	return A
}

// Constructs a new matrix with random values in range [0;1)
func Rand(rows, cols int) *Matrix {
	A := Zeros(rows, cols)

	for i := range A.values {
		A.values[i] = rand.Float64()
	}

	return A
}

// Constructs a new Matrix from a Matlab style representation
//
//	A := matrix.FromMatlab("[1 2 3; 4 5 6]")
func FromMatlab(str string) *Matrix {
	rows := strings.Split(str, ";")

	for i, row := range rows {
		rows[i] = strings.Replace(row, ",", " ", -1)
	}

	nRows := len(rows)
	nColumns := len(strings.Fields(rows[0]))

	A := Zeros(nRows, nColumns)

	for i, row := range rows {
		row = strings.Trim(row, "[] ")
		strNums := strings.Fields(row)

		for j, num := range strNums {
			n, _ := strconv.ParseFloat(num, 64)
			A.Set(i+1, j+1, n)
		}
	}

	return A
}

// Return a Matlab representation of the matrix
func (A *Matrix) ToMatlab() string {
	buffer := new(bytes.Buffer)
	buffer.WriteString("[")

	for i, v := range A.values {
		buffer.WriteString(fmt.Sprintf("%f ", v))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("; ")
		}
	}

	buffer.WriteString("]")

	return buffer.String()
}

// Gives the dimensions of the matrix
func (A *Matrix) Dim() (int, int) {
	return A.rows, A.cols
}

// Return the number of rows in the matrix
func (A *Matrix) Rows() int {
	return A.rows
}

// Return the number of columns in the matrix
func (A *Matrix) Columns() int {
	return A.cols
}

// Returns an array of all the values
func (A *Matrix) Values() []float64 {
	tmp := make([]float64, len(A.values))
	copy(tmp, A.values)
	return tmp
}

// Returns a string representation of the matrix
func (A *Matrix) String() string {
	buffer := new(bytes.Buffer)

	for i, elem := range A.values {
		buffer.WriteString(fmt.Sprintf("%.3f ", elem))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("\n")
		}
	}

	return buffer.String()
}

// Returns an exact copy of the matrix
func (A *Matrix) Copy() *Matrix {
	B := Zeros(A.rows, A.cols)
	copy(B.values, A.values)

	return B
}

func (A *Matrix) Unroll() *Matrix {
	return A.Reshape(A.rows*A.cols, 1)
}

// Reshapes the matrix. Make sure they have the same number of elements
func (A *Matrix) Reshape(rows, cols int) *Matrix {
	B := Zeros(rows, cols)
	copy(B.values, A.values)

	return B
}

// Insert the given rows into the matrix, returning a new matrix.
// Passing 0 as the second argument is like making the
// passed rows the first few, whereas passing Rows() is like appending
// the additional rows to the matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeInsertRows
func (A *Matrix) InsertRows(rows *Matrix, afterRow int) *Matrix {
	B := Zeros(A.rows+rows.rows, A.cols)

	// copy rows before inserted rows
	copy(B.values[0:afterRow*B.cols], A.values[0:afterRow*B.cols])
	// insert new rows
	copy(B.values[afterRow*B.cols:afterRow*B.cols+rows.rows*B.cols], rows.values)
	// copy rows after inserted rows
	copy(B.values[afterRow*B.cols+rows.rows*B.cols:], A.values[afterRow*B.cols:])

	return B
}

// Remove a single row from the matrix.
// The indexing for the rows start with 1 and go upto A.Rows()
func (A *Matrix) RemoveRow(row int) *Matrix {
	B := Zeros(A.rows-1, A.cols)
	copy(B.values, append(append([]float64{}, A.values[:(row-1)*A.cols]...), A.values[(row-1)*A.cols+A.cols:]...))
	return B
}

// Insert the given columns into the matrix, returning a new matrix.
// Passing 0 as the second argument is like making the
// passed columns the first few (on the left), whereas passing Columns() is like appending
// the additional columns to the matrix (on the right).
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeInsertColumns
func (A *Matrix) InsertColumns(cols *Matrix, afterCol int) *Matrix {
	B := Zeros(A.rows, A.cols+cols.cols)

	for i := 0; i < A.rows; i++ {
		copy(B.values[i*B.cols:i*B.cols+afterCol], A.values[i*A.cols:i*A.cols+afterCol])
		copy(B.values[i*B.cols+afterCol:i*B.cols+afterCol+cols.cols], cols.values[i*cols.cols:(i+1)*cols.cols])
		copy(B.values[i*B.cols+afterCol+cols.cols:(i+1)*B.cols], A.values[i*A.cols+afterCol:(i+1)*A.cols])
	}

	return B
}

// Give a function, apply its transformation to every element in the matrix.
//
//	A := matrix.Rand(4, 8)
//	A.Apply(func(index int, value float64) float64 {
//		return sigmoid(value)
//	})
func (A *Matrix) Apply(f ApplyFunc) *Matrix {
	for i, v := range A.values {
		A.values[i] = f(i, v)
	}

	return A
}

// Get the element at [row, col].
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeGet
func (A *Matrix) Get(row, col int) float64 {
	return A.values[(row-1)*A.cols+col-1]
}

// Set the element at [row, col] to val.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeSet
func (A *Matrix) Set(row, col int, val float64) {

	A.values[(row-1)*A.cols+col-1] = val

}

// Transpose the matrix
func (A *Matrix) Transpose() *Matrix {
	B := Zeros(A.cols, A.rows)

	for i := 1; i <= A.rows; i++ {
		for j := 1; j <= A.cols; j++ {
			B.values[(j-1)*B.cols+i-1] = A.values[(i-1)*A.cols+j-1]
		}
	}

	return B
}

// Add B to the matrix A, produces new matrix
func (A *Matrix) Add(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val + B.values[i]
	}

	return C, nil
}

// Subtract B from the matrix A, produces a new matrix
func (A *Matrix) Sub(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val - B.values[i]
	}

	return C, nil
}

// Multiplies 2 matricies with each other.
// Returns a new matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeMul
func (A *Matrix) Mul(B *Matrix) *Matrix {

	C := Zeros(A.rows, B.cols)

	for i := 0; i < C.rows; i++ {
		for j := 0; j < C.cols; j++ {
			sum := float64(0)

			for k := 0; k < A.cols; k++ {
				sum += A.values[i*A.cols+k] * B.values[k*B.cols+j]
			}

			C.values[i*C.cols+j] = sum
		}
	}

	return C
}

func (A *Matrix) Dot(B *Matrix) float64 {
	sum := 0.0
	for i, v := range A.values {
		sum += v * B.values[i]
	}

	return sum
}

// Element-wise multiplication of the matricies
// Returns a new matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeEWProd
func (A *Matrix) EWProd(B *Matrix) *Matrix {
	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val * B.values[i]
	}

	return C
}

// Scale the matrix by the factor f
func (A *Matrix) Scale(f float64) *Matrix {
	B := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		B.values[i] = val * f
	}

	return B
}

// Take every element of the matrix to the power of n
func (A *Matrix) Power(n float64) *Matrix {
	B := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		B.values[i] = math.Pow(val, n)
	}

	return B
}

// Add n to all elements in the matrix (in-place)
func (A *Matrix) AddNum(n float64) *Matrix {
	for i := range A.values {
		A.values[i] += n
	}

	return A
}
<commit_msg>added FromSlice matrix contructor<commit_after>// Package matrix provides functions for simple linear algebra
package matrix

import (
	"bytes"
	"fmt"
	"math"
	"math/rand"
	"strconv"
	"strings"
)

var (
	ErrIncompatibleSizes    = &MatrixError{"Incompatible sizes of matricies"}
	ErrInsertionOutOfBounds = &MatrixError{"The matrix you are trying to insert to hasn't got that row or column."}
	ErrOutOfBounds          = &MatrixError{"The element you are trying to access is out of bounds."}
)

// Describes error during calculations
type MatrixError struct {
	ErrorString string
}

// Return the Error's string
func (err *MatrixError) Error() string { return err.ErrorString }

// Matrix construct that holds all the information about a matrix
type Matrix struct {
	rows, cols int
	values     []float64
}

// A function that will apply an abitary transformation to an element in the matrix
type ApplyFunc func(index int, value float64) float64

// Creates a new Matrix and initializes all values to 0
func Zeros(rows, cols int) *Matrix {
	A := new(Matrix)

	A.rows = rows
	A.cols = cols
	A.values = make([]float64, rows*cols)

	return A
}

// Creates a new Matrix and initializes all values to 1
func Ones(rows, cols int) *Matrix {
	A := Zeros(rows, cols)
	A.AddNum(1)

	return A
}

// Creates a new identity matrix
func Eye(size int) *Matrix {
	A := Zeros(size, size)

	for i := 1; i <= size; i++ {
		A.Set(i, i, 1)
	}

	return A
}

// Constructs a new matrix with random values in range [0;1)
func Rand(rows, cols int) *Matrix {
	A := Zeros(rows, cols)

	for i := range A.values {
		A.values[i] = rand.Float64()
	}

	return A
}

// Constructs a new Matrix from a Matlab style representation
//
//	A := matrix.FromMatlab("[1 2 3; 4 5 6]")
func FromMatlab(str string) *Matrix {
	rows := strings.Split(str, ";")

	for i, row := range rows {
		rows[i] = strings.Replace(row, ",", " ", -1)
	}

	nRows := len(rows)
	nColumns := len(strings.Fields(rows[0]))

	A := Zeros(nRows, nColumns)

	for i, row := range rows {
		row = strings.Trim(row, "[] ")
		strNums := strings.Fields(row)

		for j, num := range strNums {
			n, _ := strconv.ParseFloat(num, 64)
			A.Set(i+1, j+1, n)
		}
	}

	return A
}

// Return a Matlab representation of the matrix
func (A *Matrix) ToMatlab() string {
	buffer := new(bytes.Buffer)
	buffer.WriteString("[")

	for i, v := range A.values {
		buffer.WriteString(fmt.Sprintf("%f ", v))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("; ")
		}
	}

	buffer.WriteString("]")

	return buffer.String()
}

func FromSlice(s []float64, rows, cols int) *Matrix {
	A := Zeros(rows, cols)
	copy(A.values, s)

	return A
}

// Gives the dimensions of the matrix
func (A *Matrix) Dim() (int, int) {
	return A.rows, A.cols
}

// Return the number of rows in the matrix
func (A *Matrix) Rows() int {
	return A.rows
}

// Return the number of columns in the matrix
func (A *Matrix) Columns() int {
	return A.cols
}

// Returns an array of all the values
func (A *Matrix) Values() []float64 {
	tmp := make([]float64, len(A.values))
	copy(tmp, A.values)
	return tmp
}

// Returns a string representation of the matrix
func (A *Matrix) String() string {
	buffer := new(bytes.Buffer)

	for i, elem := range A.values {
		buffer.WriteString(fmt.Sprintf("%.3f ", elem))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("\n")
		}
	}

	return buffer.String()
}

// Returns an exact copy of the matrix
func (A *Matrix) Copy() *Matrix {
	B := Zeros(A.rows, A.cols)
	copy(B.values, A.values)

	return B
}

func (A *Matrix) Unroll() *Matrix {
	return A.Reshape(A.rows*A.cols, 1)
}

// Reshapes the matrix. Make sure they have the same number of elements
func (A *Matrix) Reshape(rows, cols int) *Matrix {
	B := Zeros(rows, cols)
	copy(B.values, A.values)

	return B
}

// Insert the given rows into the matrix, returning a new matrix.
// Passing 0 as the second argument is like making the
// passed rows the first few, whereas passing Rows() is like appending
// the additional rows to the matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeInsertRows
func (A *Matrix) InsertRows(rows *Matrix, afterRow int) *Matrix {
	B := Zeros(A.rows+rows.rows, A.cols)

	// copy rows before inserted rows
	copy(B.values[0:afterRow*B.cols], A.values[0:afterRow*B.cols])
	// insert new rows
	copy(B.values[afterRow*B.cols:afterRow*B.cols+rows.rows*B.cols], rows.values)
	// copy rows after inserted rows
	copy(B.values[afterRow*B.cols+rows.rows*B.cols:], A.values[afterRow*B.cols:])

	return B
}

// Remove a single row from the matrix.
// The indexing for the rows start with 1 and go upto A.Rows()
func (A *Matrix) RemoveRow(row int) *Matrix {
	B := Zeros(A.rows-1, A.cols)
	copy(B.values, append(append([]float64{}, A.values[:(row-1)*A.cols]...), A.values[(row-1)*A.cols+A.cols:]...))
	return B
}

// Insert the given columns into the matrix, returning a new matrix.
// Passing 0 as the second argument is like making the
// passed columns the first few (on the left), whereas passing Columns() is like appending
// the additional columns to the matrix (on the right).
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeInsertColumns
func (A *Matrix) InsertColumns(cols *Matrix, afterCol int) *Matrix {
	B := Zeros(A.rows, A.cols+cols.cols)

	for i := 0; i < A.rows; i++ {
		copy(B.values[i*B.cols:i*B.cols+afterCol], A.values[i*A.cols:i*A.cols+afterCol])
		copy(B.values[i*B.cols+afterCol:i*B.cols+afterCol+cols.cols], cols.values[i*cols.cols:(i+1)*cols.cols])
		copy(B.values[i*B.cols+afterCol+cols.cols:(i+1)*B.cols], A.values[i*A.cols+afterCol:(i+1)*A.cols])
	}

	return B
}

// Give a function, apply its transformation to every element in the matrix.
//
//	A := matrix.Rand(4, 8)
//	A.Apply(func(index int, value float64) float64 {
//		return sigmoid(value)
//	})
func (A *Matrix) Apply(f ApplyFunc) *Matrix {
	for i, v := range A.values {
		A.values[i] = f(i, v)
	}

	return A
}

// Get the element at [row, col].
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeGet
func (A *Matrix) Get(row, col int) float64 {
	return A.values[(row-1)*A.cols+col-1]
}

// Set the element at [row, col] to val.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeSet
func (A *Matrix) Set(row, col int, val float64) {

	A.values[(row-1)*A.cols+col-1] = val

}

// Transpose the matrix
func (A *Matrix) Transpose() *Matrix {
	B := Zeros(A.cols, A.rows)

	for i := 1; i <= A.rows; i++ {
		for j := 1; j <= A.cols; j++ {
			B.values[(j-1)*B.cols+i-1] = A.values[(i-1)*A.cols+j-1]
		}
	}

	return B
}

// Add B to the matrix A, produces new matrix
func (A *Matrix) Add(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val + B.values[i]
	}

	return C, nil
}

// Subtract B from the matrix A, produces a new matrix
func (A *Matrix) Sub(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val - B.values[i]
	}

	return C, nil
}

// Multiplies 2 matricies with each other.
// Returns a new matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeMul
func (A *Matrix) Mul(B *Matrix) *Matrix {

	C := Zeros(A.rows, B.cols)

	for i := 0; i < C.rows; i++ {
		for j := 0; j < C.cols; j++ {
			sum := float64(0)

			for k := 0; k < A.cols; k++ {
				sum += A.values[i*A.cols+k] * B.values[k*B.cols+j]
			}

			C.values[i*C.cols+j] = sum
		}
	}

	return C
}

func (A *Matrix) Dot(B *Matrix) float64 {
	sum := 0.0
	for i, v := range A.values {
		sum += v * B.values[i]
	}

	return sum
}

// Element-wise multiplication of the matricies
// Returns a new matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeEWProd
func (A *Matrix) EWProd(B *Matrix) *Matrix {
	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val * B.values[i]
	}

	return C
}

// Scale the matrix by the factor f
func (A *Matrix) Scale(f float64) *Matrix {
	B := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		B.values[i] = val * f
	}

	return B
}

// Take every element of the matrix to the power of n
func (A *Matrix) Power(n float64) *Matrix {
	B := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		B.values[i] = math.Pow(val, n)
	}

	return B
}

// Add n to all elements in the matrix (in-place)
func (A *Matrix) AddNum(n float64) *Matrix {
	for i := range A.values {
		A.values[i] += n
	}

	return A
}
<|endoftext|>


<commit_before>package librato

// Aggregate provides a means for aggregating metrics on the client side, and pushing the
// the calculated value to librato.
type Aggregate struct {
	// Each metric has a name that is unique to its class of metrics e.g. a gauge name must be unique among gauges. The name
	// identifies a metric in subsequent API calls to store/query individual measurements and can be up to 255 characters in
	// length. Valid characters for metric names are A-Za-z0-9.:-_. The metric namespace is case insensitive.
	Name string `json:"name"`

	// Source is an optional property that can be used to subdivide a common gauge/counter among multiple members of a population.
	// For example the number of requests/second serviced by an application could be broken up among a group of server instances in
	// a scale-out tier by setting the hostname as the value of source.
	Source string `json:"source,omitempty"`

	// The epoch time at which an individual measurement occurred with a maximum resolution of seconds.
	MeasureTime int64 `json:"measure_time,omitempty"`

	values []float64
}

// Add a new value to the aggregate
// The method returns itself to allow chaining
func (a *Aggregate) Add(v float64) *Aggregate {
	a.values = append(a.values, v)
	return a
}

// Count is the number of measurements that have been recorded
func (a Aggregate) Count() int {
	return len(a.values)
}

// Sum is the sum of the measurements
func (a Aggregate) Sum() float64 {
	var total float64
	for _, v := range a.values {
		total += v
	}
	return total
}

// Min is the minimum measurement value, or zero of no measurements exist.
func (a Aggregate) Min() float64 {
	if len(a.values) == 0 {
		return 0
	}

	m := a.values[0]
	for _, v := range a.values {
		if v < m {
			m = v
		}
	}
	return m
}

// Max is the maximum measurement value, or zero of no measurements exist.
func (a Aggregate) Max() float64 {
	if len(a.values) == 0 {
		return 0
	}

	m := a.values[0]
	for _, v := range a.values {
		if v > m {
			m = v
		}
	}
	return m
}

// SumSquares is
func (a Aggregate) SumSquares() float64 {
	if len(a.values) == 0 {
		return 0
	}
	var sum, sumOfSquares float64
	for _, v := range a.values {
		sum += v
		sumOfSquares += v * v
	}

	x := (sum * sum) / float64(len(a.values))
	return sumOfSquares - x
}

func (a Aggregate) ToGauge() Gauge {
	return Gauge{
		Name:       a.Name,
		Source:     a.Source,
		Count:      a.Count(),
		Sum:        a.Sum(),
		Min:        a.Min(),
		Max:        a.Max(),
		SumSquares: a.SumSquares(),
	}
}

type Gauge struct {
	// Each metric has a name that is unique to its class of metrics e.g. a gauge name must be unique among gauges. The name
	// identifies a metric in subsequent API calls to store/query individual measurements and can be up to 255 characters in
	// length. Valid characters for metric names are A-Za-z0-9.:-_. The metric namespace is case insensitive.
	Name string `json:"name"`

	// The numeric value of an individual measurement. Multiple formats are supported (e.g. integer, floating point, etc) but the value must be numeric.
	Value interface{} `json:"value"`

	// Source is an optional property that can be used to subdivide a common gauge/counter among multiple members of a population.
	// For example the number of requests/second serviced by an application could be broken up among a group of server instances in
	// a scale-out tier by setting the hostname as the value of source.
	Source string `json:"source,omitempty"`

	// The epoch time at which an individual measurement occurred with a maximum resolution of seconds.
	MeasureTime int64 `json:"measure_time,omitempty"`

	Count      int         `json:"count,omitempty"`
	Sum        interface{} `json:"sum,omitempty"`
	Min        interface{} `json:"min,omitempty"`
	Max        interface{} `json:"max,omitempty"`
	SumSquares interface{} `json:"sum_squares,omitempty"`
}

type Counter struct {
	Name        string      `json:"name"`
	Value       interface{} `json:"value"`
	Source      string      `json:"source,omitempty"`
	MeasureTime int64       `json:"measure_time,omitempty"`
}
<commit_msg>Added field notes for the Gauge metrics<commit_after>package librato

// Aggregate provides a means for aggregating metrics on the client side, and pushing the
// the calculated value to librato.
type Aggregate struct {
	// Each metric has a name that is unique to its class of metrics e.g. a gauge name must be unique among gauges. The name
	// identifies a metric in subsequent API calls to store/query individual measurements and can be up to 255 characters in
	// length. Valid characters for metric names are A-Za-z0-9.:-_. The metric namespace is case insensitive.
	Name string `json:"name"`

	// Source is an optional property that can be used to subdivide a common gauge/counter among multiple members of a population.
	// For example the number of requests/second serviced by an application could be broken up among a group of server instances in
	// a scale-out tier by setting the hostname as the value of source.
	Source string `json:"source,omitempty"`

	// The epoch time at which an individual measurement occurred with a maximum resolution of seconds.
	MeasureTime int64 `json:"measure_time,omitempty"`

	values []float64
}

// Add a new value to the aggregate
// The method returns itself to allow chaining
func (a *Aggregate) Add(v float64) *Aggregate {
	a.values = append(a.values, v)
	return a
}

// Count is the number of measurements that have been recorded
func (a Aggregate) Count() int {
	return len(a.values)
}

// Sum is the sum of the measurements
func (a Aggregate) Sum() float64 {
	var total float64
	for _, v := range a.values {
		total += v
	}
	return total
}

// Min is the minimum measurement value, or zero of no measurements exist.
func (a Aggregate) Min() float64 {
	if len(a.values) == 0 {
		return 0
	}

	m := a.values[0]
	for _, v := range a.values {
		if v < m {
			m = v
		}
	}
	return m
}

// Max is the maximum measurement value, or zero of no measurements exist.
func (a Aggregate) Max() float64 {
	if len(a.values) == 0 {
		return 0
	}

	m := a.values[0]
	for _, v := range a.values {
		if v > m {
			m = v
		}
	}
	return m
}

// SumSquares is
func (a Aggregate) SumSquares() float64 {
	if len(a.values) == 0 {
		return 0
	}
	var sum, sumOfSquares float64
	for _, v := range a.values {
		sum += v
		sumOfSquares += v * v
	}

	x := (sum * sum) / float64(len(a.values))
	return sumOfSquares - x
}

func (a Aggregate) ToGauge() Gauge {
	return Gauge{
		Name:       a.Name,
		Source:     a.Source,
		Count:      a.Count(),
		Sum:        a.Sum(),
		Min:        a.Min(),
		Max:        a.Max(),
		SumSquares: a.SumSquares(),
	}
}

type Gauge struct {
	// Each metric has a name that is unique to its class of metrics e.g. a gauge name must be unique among gauges. The name
	// identifies a metric in subsequent API calls to store/query individual measurements and can be up to 255 characters in
	// length. Valid characters for metric names are A-Za-z0-9.:-_. The metric namespace is case insensitive.
	Name string `json:"name"`

	// The numeric value of an individual measurement. Multiple formats are supported (e.g. integer, floating point, etc) but the value must be numeric.
	Value interface{} `json:"value"`

	// Source is an optional property that can be used to subdivide a common gauge/counter among multiple members of a population.
	// For example the number of requests/second serviced by an application could be broken up among a group of server instances in
	// a scale-out tier by setting the hostname as the value of source.
	Source string `json:"source,omitempty"`

	// The epoch time at which an individual measurement occurred with a maximum resolution of seconds.
	MeasureTime int64 `json:"measure_time,omitempty"`

	// The following fields are used to publish aggregations
	// They are public to allow serialization, but should not be used directly.
	Count      int         `json:"count,omitempty"`
	Sum        interface{} `json:"sum,omitempty"`
	Min        interface{} `json:"min,omitempty"`
	Max        interface{} `json:"max,omitempty"`
	SumSquares interface{} `json:"sum_squares,omitempty"`
}

type Counter struct {
	Name        string      `json:"name"`
	Value       interface{} `json:"value"`
	Source      string      `json:"source,omitempty"`
	MeasureTime int64       `json:"measure_time,omitempty"`
}
<|endoftext|>


<commit_before>package match

import (
	"fmt"

	"github.com/tisp-lang/tisp/src/lib/ast"
	"github.com/tisp-lang/tisp/src/lib/debug"
	"github.com/tisp-lang/tisp/src/lib/gensym"
	"github.com/tisp-lang/tisp/src/lib/scalar"
)

type desugarer struct {
	lets []interface{}
}

func newDesugarer() *desugarer {
	return &desugarer{nil}
}

func (d *desugarer) desugarMatchExpression(x interface{}) interface{} {
	switch x := x.(type) {
	case ast.App:
		return ast.NewApp(
			d.desugarMatchExpression(x.Function()),
			d.desugarMatchExpression(x.Arguments()).(ast.Arguments),
			x.DebugInfo())
	case ast.Arguments:
		ps := make([]ast.PositionalArgument, 0, len(x.Positionals()))

		for _, p := range x.Positionals() {
			ps = append(ps, d.desugarMatchExpression(p).(ast.PositionalArgument))
		}

		ks := make([]ast.KeywordArgument, 0, len(x.Keywords()))

		for _, k := range x.Keywords() {
			ks = append(ks, d.desugarMatchExpression(k).(ast.KeywordArgument))
		}

		dicts := make([]interface{}, 0, len(x.ExpandedDicts()))

		for _, dict := range x.ExpandedDicts() {
			dicts = append(dicts, d.desugarMatchExpression(dict))
		}

		return ast.NewArguments(ps, ks, dicts)
	case ast.KeywordArgument:
		return ast.NewKeywordArgument(x.Name(), d.desugarMatchExpression(x.Value()))
	case ast.LetFunction:
		ls := make([]interface{}, 0, len(x.Lets()))

		for _, l := range x.Lets() {
			l := d.desugarMatchExpression(l)
			ls = append(ls, append(d.lets, l)...)
			d.lets = nil
		}

		return ast.NewLetFunction(
			x.Name(),
			x.Signature(),
			ls,
			d.desugarMatchExpression(x.Body()),
			x.DebugInfo())
	case ast.LetVar:
		return ast.NewLetVar(x.Name(), d.desugarMatchExpression(x.Expr()))
	case ast.Match:
		return app(d.createMatchFunction(x.Cases()), d.desugarMatchExpression(x.Value()))
	case ast.Output:
		return ast.NewOutput(d.desugarMatchExpression(x.Expr()), x.Expanded())
	case ast.PositionalArgument:
		return ast.NewPositionalArgument(d.desugarMatchExpression(x.Value()), x.Expanded())
	default:
		return x
	}
}

func (d *desugarer) createMatchFunction(cs []ast.Case) interface{} {
	arg := gensym.GenSym("match", "argument")

	f := ast.NewLetFunction(
		gensym.GenSym("match", "function"),
		ast.NewSignature([]string{arg}, nil, "", nil, nil, ""),
		d.lets,
		d.casesToBody(arg, cs),
		debug.NewGoInfo(0))

	d.lets = []interface{}{f}

	return f.Name()
}

func (d *desugarer) casesToBody(matched interface{}, cs []ast.Case) interface{} {
	fs := []interface{}{}

	for _, cs := range groupCases(cs) {
		fs = append(fs, createFunctionOfSameCases(cs))
	}

	ls := []interface{}{}

	body := app("error", "MatchError", "\"Failed to match a value with patterns.\"")
	for _, f := range fs {
		result := gensym.GenSym("match", "intermediate", "result")
		value := gensym.GenSym("match", "intermediate", "value")
		ok := gensym.GenSym("match", "intermediate", "ok")

		ls = append(
			ls,
			ast.NewLetVar(result, app(f, matched)),
			ast.NewLetVar(value, app("first", result)),
			ast.NewLetVar(ok, app("first", app("rest", result))))

		body = app("if", ok, value, body)
	}

	d.lets = append(d.lets, append(fs, ls...)...)

	return body
}

func app(xs ...interface{}) interface{} {
	return ast.NewPApp(xs[0], xs[1:], debug.NewGoInfo(0))
}

func createFunctionOfSameCases(cs []ast.Case) interface{} {
	// Functions should return [result, ok: bool].
	panic("Not implemented")
}

// func matchCasesOfSamePatterns(v string, cs []ast.Case) (interface{}, []interface{}) {
//	// TODO: Implement this function.

//	switch getPatternType(cs[0].Pattern()) {
//	case listPattern:
//		matchType(v, "list")
//	case dictPattern:
//	case scalarPattern:
//	case namePattern:
//		return nil, nil
//	}

//	panic("Not implemented")
// }

// func matchType(v string, typ string, then interface{}, els interface{}) interface{} {
//	return ast.NewPApp(
//		"if",
//		[]interface{}{
//			ast.NewPApp("=", []interface{}{ast.NewPApp("typeOf", v), typ}),
//			then,
//			els},
//		debug.NewGoInfo(0))
// }

func groupCases(cs []ast.Case) map[patternType][]ast.Case {
	m := map[patternType][]ast.Case{}

	for _, c := range cs {
		p := getPatternType(c.Pattern())
		m[p] = append(m[p], c)
	}

	return m
}

func getPatternType(p interface{}) patternType {
	switch x := p.(type) {
	case string:
		if scalar.Defined(x) {
			return scalarPattern
		}

		return namePattern
	case ast.App:
		switch x.Function().(string) {
		case "$list":
			return listPattern
		case "$dict":
			return dictPattern
		}
	}

	panic(fmt.Errorf("Invalid pattern: %#v", p))
}
<commit_msg>Revert "Refactor createMatchFunction"<commit_after>package match

import (
	"fmt"

	"github.com/tisp-lang/tisp/src/lib/ast"
	"github.com/tisp-lang/tisp/src/lib/debug"
	"github.com/tisp-lang/tisp/src/lib/gensym"
	"github.com/tisp-lang/tisp/src/lib/scalar"
)

type desugarer struct {
	lets []interface{}
}

func newDesugarer() *desugarer {
	return &desugarer{nil}
}

func (d *desugarer) desugarMatchExpression(x interface{}) interface{} {
	switch x := x.(type) {
	case ast.App:
		return ast.NewApp(
			d.desugarMatchExpression(x.Function()),
			d.desugarMatchExpression(x.Arguments()).(ast.Arguments),
			x.DebugInfo())
	case ast.Arguments:
		ps := make([]ast.PositionalArgument, 0, len(x.Positionals()))

		for _, p := range x.Positionals() {
			ps = append(ps, d.desugarMatchExpression(p).(ast.PositionalArgument))
		}

		ks := make([]ast.KeywordArgument, 0, len(x.Keywords()))

		for _, k := range x.Keywords() {
			ks = append(ks, d.desugarMatchExpression(k).(ast.KeywordArgument))
		}

		dicts := make([]interface{}, 0, len(x.ExpandedDicts()))

		for _, dict := range x.ExpandedDicts() {
			dicts = append(dicts, d.desugarMatchExpression(dict))
		}

		return ast.NewArguments(ps, ks, dicts)
	case ast.KeywordArgument:
		return ast.NewKeywordArgument(x.Name(), d.desugarMatchExpression(x.Value()))
	case ast.LetFunction:
		ls := make([]interface{}, 0, len(x.Lets()))

		for _, l := range x.Lets() {
			l := d.desugarMatchExpression(l)
			ls = append(ls, append(d.lets, l)...)
			d.lets = nil
		}

		return ast.NewLetFunction(
			x.Name(),
			x.Signature(),
			ls,
			d.desugarMatchExpression(x.Body()),
			x.DebugInfo())
	case ast.LetVar:
		return ast.NewLetVar(x.Name(), d.desugarMatchExpression(x.Expr()))
	case ast.Match:
		return app(d.createMatchFunction(x.Cases()), d.desugarMatchExpression(x.Value()))
	case ast.Output:
		return ast.NewOutput(d.desugarMatchExpression(x.Expr()), x.Expanded())
	case ast.PositionalArgument:
		return ast.NewPositionalArgument(d.desugarMatchExpression(x.Value()), x.Expanded())
	default:
		return x
	}
}

func (d *desugarer) createMatchFunction(cs []ast.Case) interface{} {
	arg := gensym.GenSym("match", "argument")
	body := d.casesToBody(arg, cs)

	f := ast.NewLetFunction(
		gensym.GenSym("match", "function"),
		ast.NewSignature([]string{arg}, nil, "", nil, nil, ""),
		d.lets,
		body,
		debug.NewGoInfo(0))

	d.lets = []interface{}{f}

	return f.Name()
}

func (d *desugarer) casesToBody(matched interface{}, cs []ast.Case) interface{} {
	fs := []interface{}{}

	for _, cs := range groupCases(cs) {
		fs = append(fs, createFunctionOfSameCases(cs))
	}

	ls := []interface{}{}

	body := app("error", "MatchError", "\"Failed to match a value with patterns.\"")
	for _, f := range fs {
		result := gensym.GenSym("match", "intermediate", "result")
		value := gensym.GenSym("match", "intermediate", "value")
		ok := gensym.GenSym("match", "intermediate", "ok")

		ls = append(
			ls,
			ast.NewLetVar(result, app(f, matched)),
			ast.NewLetVar(value, app("first", result)),
			ast.NewLetVar(ok, app("first", app("rest", result))))

		body = app("if", ok, value, body)
	}

	d.lets = append(d.lets, append(fs, ls...)...)

	return body
}

func app(xs ...interface{}) interface{} {
	return ast.NewPApp(xs[0], xs[1:], debug.NewGoInfo(0))
}

func createFunctionOfSameCases(cs []ast.Case) interface{} {
	// Functions should return [result, ok: bool].
	panic("Not implemented")
}

// func matchCasesOfSamePatterns(v string, cs []ast.Case) (interface{}, []interface{}) {
//	// TODO: Implement this function.

//	switch getPatternType(cs[0].Pattern()) {
//	case listPattern:
//		matchType(v, "list")
//	case dictPattern:
//	case scalarPattern:
//	case namePattern:
//		return nil, nil
//	}

//	panic("Not implemented")
// }

// func matchType(v string, typ string, then interface{}, els interface{}) interface{} {
//	return ast.NewPApp(
//		"if",
//		[]interface{}{
//			ast.NewPApp("=", []interface{}{ast.NewPApp("typeOf", v), typ}),
//			then,
//			els},
//		debug.NewGoInfo(0))
// }

func groupCases(cs []ast.Case) map[patternType][]ast.Case {
	m := map[patternType][]ast.Case{}

	for _, c := range cs {
		p := getPatternType(c.Pattern())
		m[p] = append(m[p], c)
	}

	return m
}

func getPatternType(p interface{}) patternType {
	switch x := p.(type) {
	case string:
		if scalar.Defined(x) {
			return scalarPattern
		}

		return namePattern
	case ast.App:
		switch x.Function().(string) {
		case "$list":
			return listPattern
		case "$dict":
			return dictPattern
		}
	}

	panic(fmt.Errorf("Invalid pattern: %#v", p))
}
<|endoftext|>


<commit_before><commit_msg>Number may be singular, plural, or dual (in some languages)<commit_after><|endoftext|>