go-license-detector

Reliable project licenses detector.

Github星跟踪图

go-license-detector GoDoc Build Status Build status codecov Go Report Card

Project license detector - a command line application and a library, written in Go.
It scans the given directory for license files, normalizes and hashes them and outputs
all the fuzzy matches with the list of reference texts.
The returned names follow SPDX standard.
Read the blog post.

Why? There are no similar projects which can be compiled into a native binary without
dependencies and also support the whole SPDX license database (≈400 items).
This implementation is also fast, requires little memory, and the API is easy to use.

The license texts are taken directly from license-list-data
repository. The detection algorithm is not template matching;
this directly implies that go-license-detector does not provide any legal guarantees.
The intended area of it's usage is data mining.

Installation

export GO111MODULE=on
go mod download
go build -v gopkg.in/src-d/go-license-detector.v3/cmd/license-detector

Contributions

...are welcome, see CONTRIBUTING.md and code of conduct.

License

Apache 2.0, see LICENSE.md.

Algorithm

  1. Find files in the root directory which may represent a license. E.g. LICENSE or license.md.
  2. If the file is Markdown or reStructuredText, render to HTML and then convert to plain text. Original HTML files are also converted.
  3. Normalize the text according to SPDX recommendations.
  4. Split the text into unigrams and build the weighted bag of words.
  5. Calculate Weighted MinHash.
  6. Apply Locality Sensitive Hashing and pick the reference licenses which are close.
  7. For each of the candidate, calculate the Levenshtein distance - D.
    the corresponding text is the single line with each unigram represented by a single rune (character).
  8. Set the similarity as 1 - D / L where L is the number of unigrams in the quieried license.

This pipeline guarantees constant time queries, though requires some initialization to preprocess
the reference licenses.

If there are not license files found:

  1. Look for README files.
  2. If the file is Markdown or reStructuredText, render to HTML and then convert to plain text. Original HTML files are also converted.
  3. Scan for words like "copyright", "license" and "released under". Take the neighborhood.
  4. Run Named Entity Recognition (NER) over that surrounding context and extract the possible license name.
  5. Match it against the list of license names from SPDX.

Usage

Command line:

license-detector /path/to/project
license-detector https://github.com/src-d/go-git

Library (for a single license detection):

import (
    "gopkg.in/src-d/go-license-detector.v3/licensedb"
    "gopkg.in/src-d/go-license-detector.v3/licensedb/filer"
)

func main() {
	licenses, err := licensedb.Detect(filer.FromDirectory("/path/to/project"))
}

Library (for a convenient data structure that can be formatted as JSON):

import (
	"encoding/json"
	"fmt"

	"gopkg.in/src-d/go-license-detector.v3/licensedb"
)

func main() {
	results := licensedb.Analyse("/path/to/project1", "/path/to/project2")
	bytes, err := json.MarshalIndent(results, "", "\t")
	if err != nil {
		fmt.Printf("could not encode result to JSON: %v\n", err)
	}
	fmt.Println(string(bytes))
}

Quality

On the dataset of ~1000 most starred repositories on GitHub as of early February 2018
(list), 99% of the licenses are detected.
The analysis of detection failures is going in FAILURES.md.

Comparison to other projects on that dataset:, Detector, Detection rate, Time to scan, sec, :-------, :----------------------------------------:, :-----------------------------------------, go-license-detector, 99% (897/902), 13.5, benbalter/licensee, 75% (673/902), 111, google/licenseclassifier, 76% (682/902), 907, boyter/lc, 88% (797/902), 548, amzn/askalono, 87% (785/902), 165, LiD, 94% (847/902), 3660, How this was measured

Regenerate binary data

The SPDX licenses are included into the binary. To update them, run

make bindata.go

主要指标

概览
名称与所有者src-d/go-license-detector
主编程语言Go
编程语言Makefile (语言数: 2)
平台
许可证Other
所有者活动
创建于2018-01-30 09:21:57
推送于2023-06-09 11:47:00
最后一次提交2020-06-01 00:03:53
发布数10
最新版本名称v3.1.0 (发布于 )
第一版名称v1 (发布于 )
用户参与
星数236
关注者数10
派生数39
提交数169
已启用问题?
问题数21
打开的问题数7
拉请求数51
打开的拉请求数4
关闭的拉请求数4
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?