元素
- 值
- 后续节点
- 是否为某词的结尾
做一些关键词检查,也不是不行(bushi
export class CDFANode {
constructor() {
this._node_value = 0;
this._map_children = {}; // { [key: number]: CDFANode }
this._is_tail = false;
this._child_num = 0;
this._tail_note = null;
}
IsTail() { return this._is_tail; }
SetTail() {
this._is_tail = true;
}
SetTailNode(tail_note) {
this._tail_note = tail_note
}
GetTailNode() {
return this._tail_note
}
GetNodeValue() { return this._node_value; }
SetNodeValue(v) { this._node_value = v; }
IsChildNode(value) {
return this._map_children[value] != null
}
GetChildNode(value) {
return this._map_children[value]
}
GetAllTailNodeChild() {
let tail_node_list = []
if (this.IsTail()) {
tail_node_list.push(this._tail_note)
}
for (const key in this._map_children) {
const item = this._map_children[key]
tail_node_list.push(...item.GetAllTailNodeChild())
}
return tail_node_list
}
HasChildNode() {
return this._child_num > 0
}
GetMapChilds() {
return this._map_children
}
AddChildNode(value, is_tail = false, tail_note) {
if (this._map_children[value]) {
return this._map_children[value];
}
let _child_node = new CDFANode();
_child_node.node_value = value;
if (is_tail) {
_child_node.SetTail();
_child_node.SetTailNode(tail_note)
}
this._map_children[value] = _child_node
this._child_num += 1
return _child_node;
}
}
export class MgrDFAPattern {
constructor() {
this._is_init = false;
this._arr_filter = {}; // { [key: number]: CDFANode }
this._root_node = new CDFANode();
this._invalid_id = -1;
}
_AddFilterKeysName(data, tail_note) {
if (data==null || typeof data != "string") {
return
}
let _index = 0
const _length = data.length
let _cur_node = this._root_node;
let _is_tail = false;
for (let _i = 0; _i < _length; ++_i) {
const _value = data[_i].codePointAt(0);
if (_index == _length - 1) {
_is_tail = true;
}
if (_cur_node.IsChildNode(_value)) {
_cur_node = _cur_node.GetChildNode(_value);
if (_is_tail && !_cur_node.is_tail) {
_cur_node.SetTail(tail_note);
_cur_node.SetTailNode(tail_note)
}
}
else {
_cur_node = _cur_node.AddChildNode(_value, _is_tail, tail_note);
}
_index++;
}
}
InitByList(data_list, data_key, tail_note_key) {
if (this._is_init || data_list==null) {
return false;
}
for (let _i = 0; _i < data_list.length; _i++) {
const _data = data_key==null ? data_list[_i] : data_list[_i][data_key]
const _tail_note_key = tail_note_key==null ? null : data_list[_i][tail_note_key]
this._AddFilterKeysName(_data, _tail_note_key)
}
this._is_init = true
return true
}
InitByMap(data_map) {
if (this._is_init || data_map==null) {
return false;
}
for (var [key, value] of data_map) {
this._AddFilterKeysName(key, value)
}
this._is_init = true
return true
}
IsContainIllegal(data) {
if (data==null || typeof data != "string") {
return false
}
let _cur_node = this._root_node;
let _start_index = this._invalid_id;
const _length = data.length;
for (let _i = 0; _i < _length; ++_i) {
const _value = data[_i].codePointAt(0);
if (_cur_node.IsChildNode(_value)) {
_cur_node = _cur_node.GetChildNode(_value);
if (_start_index == this._invalid_id) {
_start_index = _i;
}
if (_cur_node.is_tail) {
return true;
}
} else {
if (_start_index == this._invalid_id) {
continue;
}
else {
_cur_node = this._root_node;
_i = _start_index;
_start_index = this._invalid_id;
}
}
}
return false;
}
GetAllContainingFields(data) {
if (data==null || typeof data != "string") {
return []
}
let _cur_node = this._root_node;
let _start_index = this._invalid_id;
const _length = data.length;
for (let _i = 0; _i < _length; _i++) {
const _value = data[_i].codePointAt(0);
if (_cur_node.IsChildNode(_value)) {
_cur_node = _cur_node.GetChildNode(_value);
if (_start_index == this._invalid_id) {
_start_index = _i;
}
} else {
if (_start_index == this._invalid_id) {
continue;
}
else {
_cur_node = this._root_node;
_i = _start_index;
_start_index = this._invalid_id;
break
}
}
}
if (_start_index == this._invalid_id) {
return [];
}
else {
let all_containing_fields = []
let nodeList = []
let _cur_index = 0
nodeList.push(_cur_node)
while (_cur_node && _cur_node.HasChildNode()) {
const childs = _cur_node.GetMapChilds()
for (const key in childs) {
if (childs.hasOwnProperty(key)) {
nodeList.push(childs[key])
}
}
++_cur_index
if (nodeList.length > _cur_index) {
_cur_node = nodeList[_cur_index]
} else {
_cur_node = null
}
}
for (const item of nodeList) {
if (item.IsTail()) {
all_containing_fields.push(item.GetTailNode())
}
}
return all_containing_fields
//return _cur_node.GetAllTailNodeChild()
}
}
}
export default {
MgrDFAPattern
}