concept.go 1.2 KB
Newer Older
P
Phodal Huang 已提交
1 2 3
package cmd

import (
P
Phodal Huang 已提交
4 5
	"encoding/json"
	"github.com/olekukonko/tablewriter"
6 7 8 9
	"github.com/phodal/coca/config"
	"github.com/phodal/coca/core/domain/concept"
	"github.com/phodal/coca/core/models"
	. "github.com/phodal/coca/core/support"
P
Phodal Huang 已提交
10 11
	"github.com/spf13/cobra"
	"log"
P
Phodal Huang 已提交
12 13
	"os"
	"strconv"
P
Phodal Huang 已提交
14 15 16 17
)

var parsedDeps []models.JClassNode

P
Phodal Huang 已提交
18
var conceptCmd = &cobra.Command{
P
Phodal Huang 已提交
19 20 21 22 23 24
	Use:   "concept",
	Short: "concept api",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		dependence := cmd.Flag("dependence").Value.String()

P
Phodal Huang 已提交
25
		if dependence != "" {
P
Phodal Huang 已提交
26
			analyser := concept.NewConceptAnalyser()
P
Phodal Huang 已提交
27 28 29 30 31
			file := ReadFile(dependence)
			if file == nil {
				log.Fatal("lost file:" + dependence)
			}

P
Phodal Huang 已提交
32
			_ = json.Unmarshal(file, &parsedDeps)
P
Phodal Huang 已提交
33

P
Phodal Huang 已提交
34
			wordCounts := analyser.Analysis(&parsedDeps)
P
Phodal Huang 已提交
35 36 37 38

			table := tablewriter.NewWriter(os.Stdout)
			table.SetHeader([]string{"Words", "Counts"})

P
Phodal Huang 已提交
39 40
			for _, word := range wordCounts {
				if word.Value > 0 {
P
Phodal Huang 已提交
41
					table.Append([]string{word.Key, strconv.Itoa(word.Value)})
P
Phodal Huang 已提交
42 43
				}
			}
P
Phodal Huang 已提交
44 45

			table.Render()
P
Phodal Huang 已提交
46 47 48 49 50 51 52
		}
	},
}

func init() {
	rootCmd.AddCommand(conceptCmd)

P
Phodal Huang 已提交
53
	conceptCmd.PersistentFlags().StringP("dependence", "d", config.CocaConfig.ReporterPath+"/deps.json", "get dependence file")
P
Phodal Huang 已提交
54
}