2010-10-31 13 views
6

मैं एक स्ट्रिंग को टोकननाइज़ करने का प्रयास कर रहा हूं। मेरे पास trie के रूप में आदेशित उपलब्ध टोकन की एक तालिका है। प्रत्येक टोकन जानता है कि इसमें बच्चे हैं। एक साधारण टोकन मेज, की तरह दिखाई देगा इस तालिका मेंस्ट्रिंग को टोकन करने का कुशल तरीका - सी

pattern value   has_children 
-------- ------  -------- 
s   s-val   1 
stack  stack-val  0 
over  over-val  1 
overflow overflow-val 0 

, stacks और overflow का एक बच्चा है over का एक बच्चा है। व्यावहारिक रूप से, इस तालिका में 5000+ रिकॉर्ड इस तरह से आदेश दिए जाएंगे।

अब, एक स्ट्रिंग stackover दिया गया है, इसे stack-valover-val आउटपुट करना चाहिए। एल्गोरिदम लालची है और यह हमेशा सबसे लंबा मैच खोजने की कोशिश करेगा।

ऐसा करने के लिए, मैं इनपुट से प्रत्येक चरित्र को पढ़ना शुरू कर दूंगा, मैच की तलाश करूँगा, अगर एक मैच मिला और टोकन के बच्चे हैं, तो अगले चरित्र सहित दोबारा मिलान करें। ऐसा तब तक करें जब तक कि हम सबसे लंबा मैच नहीं पाते। यदि कोई मिलान नहीं मिला है, तो जब तक हम स्ट्रिंग या सफल मिलान के अंत तक नहीं पहुंच जाते, तब तक अगले चरित्र को शामिल करके मिलान करने का प्रयास करें।

यदि हम बिना मैच के स्ट्रिंग के अंत तक पहुंच गए हैं, आउटपुट ? प्रतीक इनपुट से पहले अक्षर को हटा दें। शेष वर्णों के साथ पूरी प्रक्रिया दोहराएं।

यह एल्गोरिदम काम करता है, लेकिन इनपुट के सभी संभावित संयोजनों पर बैकट्रैकिंग और पुनरावृत्ति इसे धीमा और जटिल बनाता है।

मुझे आश्चर्य है कि इसे हल करने का एक बेहतर तरीका है? किसी भी सहायता की सराहना की जाएगी।

+0

मैं बच्चों का ट्रैक क्यों रख रहा हूं: यदि मुझे एक मैच मिला और सबसे लंबे मैच की तलाश में है, तो टोकन के बच्चे होने पर मुझे ऐसा करने की ज़रूरत नहीं है। यह दक्षता में सुधार करता है। –

उत्तर

2

बैकट्रैकिंग के बजाय आप सभी संभावित परिणामों को स्मृति में रख सकते हैं, जब तक कि एक परिणाम इनपुट स्ट्रीम में किसी बिंदु पर एकल न हो। उदाहरण

टोकन: एस स्टैक Stackoverflow हिरन से अधिक अतिप्रवाह
स्ट्रिंग: SSTACKOVERFUN

1 - जगह 0 पर मिला एस, टोकन कि एस से शुरू होती है, उन सब को प्रयास करते हैं, केवल एस मान्य है, इसलिए संकल्प एस
2 - एस 1 पर, ऐसे टोकन हैं, उन्हें आज़माएं, संभावित मान्य एस और स्टैक हैं। हल मत करो, बस उन्हें ध्यान में रखें।
3 - टी 2 पर, ऐसे टोकन नहीं हैं, इसलिए एस को अभी हल किया जा सकता है, लेकिन हमारे पास लंबे टोकन (STACK) भी हैं इसलिए एस अच्छा नहीं है। डुबकी एस, और स्टैक केवल छोड़ दिया गया है, लेकिन इसमें बच्चे हैं। बच्चों के लिए स्ट्रिंग का प्रयास करें।हे पर 6, ऐसे टोकन, उन्हें कोशिश, केवल अधिक है, तो संकल्प से अधिक
5 है है - - एफ 10 पर, ऐसी कोई टोकन, और कुछ भी हल करने के लिए कोई संभव बच्चों इसलिए संकल्प स्टैक
4 हैं से पहले तो यह गैर tokenizable है
6 और 7 - 5 कदम

अंतिम परिणाम के रूप में एक ही: एस स्टैक से अधिक मज़ा

1

क्या आप Aho-Corasick एल्गोरिदम का उपयोग कर सकते हैं? यह एक कीवर्ड पेड़ (trie) खोजने के लिए एक automaton बनाता है।

1

मैं सोच रहा हूँ कि आप अपने सभी कीवर्ड के लेते हैं और उन्हें वर्णानुक्रम रिवर्स, तो अपनी सूची बन (प्लस कुछ अतिरिक्त) हैं

0 stack  1 
1 s   0 
2 overflow 3 
3 over  5 
4 ovum  5 
5 o   0 
6 exchange 7 
7 ex   0 

इस सूची के तीसरे स्तंभ के संकेत दिए गए हैं क्रमबद्ध करना चाहते हैं अभिभावक टोकन जो हमेशा सूची में कम होता है। फिर आप अपनी लक्ष्य स्ट्रिंग और बाइनरी खोज ले सकते हैं जहां यह इस सूची में फिट बैठता है। यदि यह एक टोकन से ऊपर है जो मेल खाता है तो आप उस हिस्से को बंद कर देते हैं और बाकी के लिए प्रक्रिया दोहराते हैं। यदि यह मेल नहीं खाता है तो आप अगले सबसे लंबे संभावित मिलान टोकन को खोजने के लिए मूल पॉइंटर का उपयोग करें।

यदि आप वास्तव में कल्पना करना चाहते हैं तो आप तारों को 64 बिट शब्दों में भी जोड़ सकते हैं और बाइनरी खोज में एक बार में 8 वर्णों की तुलना कर सकते हैं।

1

मैं सुझाव है कि आप Ragel कोशिश, यह कुशल स्कैनर उत्पन्न कर सकते हैं कि सबसे लंबे समय तक मैच कर सकते हैं/बैक ट्रैकिंग। अधिक जानकारी के लिए Ragel user guide में अध्याय 6.3 देखें।

%%{ 
machine test; 

main := |* 
's' => { puts("s-val");}; 
'stack' => { puts("stack-val");}; 
'over' => { puts("over-val");}; 
'overflow' => { puts("overflow-val");}; 

# Anything else matches to any, outputs a '?' and continues 
any => {putc('?');}; 
*|; 
}%% 
0

The following token_tree code is based on the prefix_tree class from ZeroMQ

prefix_tree वर्ग केवल रिटर्न:

मैं इनपुट को खिलाने के लिए एक छोटे से परीक्षण है जो मुझे लगता है कि आपके विनिर्देशन से मेल खाता बना लिया है कोड के बिना, यह केवल राज्य मशीन वर्णन है, "सत्य" जब पेड़ के उपसर्गों में से एक इनपुट टेक्स्ट की शुरुआत से मेल खाता है। यह आपको यह भी नहीं बताएगा कि कौन सा उपसर्ग या उपसर्ग कितना समय था।

यह टोकन_ट्री इनपुट टेक्स्ट की शुरुआत से मेल खाने वाले सबसे लंबे टोकन की तलाश करेगा। खोज फ़ंक्शन token_tree_longest_token() केवल इनपुट टेक्स्ट की शुरुआत के विरुद्ध मिलान किए गए सबसे लंबे टोकन की लंबाई को वापस करने की आवश्यकता है।

मूल एल्गोरिदम प्रश्न में वर्णित एक जैसा है, लेकिन यह प्रत्यारोपण तेजी से हो सकता है।

स्मृति उपयोग में सुधार करने के कुछ तरीके भी हैं, जो इसे तेज़ी से प्राप्त कर सकते हैं।

#include <stdint.h> 
#include <stdlib.h> 

/* #define TEST_TOKEN_TREE */ 
/* 
* TODO: possible improvements, use multiple types of nodes: string/branch/leaf. 
* The string node would replace a chain of normal token_nodes and save memory. 
* This would require spliting a node to add branch points. 
* Use these structs: 
* struct token_node { 
* uint32_t ref_count; 
* uint8_t node_type; -- node is token_node_str/token_node_branch/token_node_leaf 
* }; 
* struct token_node_str { 
* token_node base; 
* uint8_t reserved; 
* uint16_t len;   -- string length 
* token_node *child;  -- string nodes can only have one child. 
* uint8_t str[0];  -- embedded string (not null-terminated) 
* }; 
* struct token_node_branch { 
* token_node base; 
* uint8_t min;   -- smallest char in child list. 
* uint16_t count;   -- child count. 
* token_node *children[0]; 
* }; 
* struct token_node_leaf { -- leaf nodes have no children. 
* token_node base; 
* }; 
* This will save memory, but will make code much more complex. 
*/ 

typedef struct token_tree token_tree; 
typedef struct token_node token_node; 

struct token_tree { 
    token_node *root; /**< root node of token tree. */ 
}; 

struct token_node { 
    uint32_t ref_count; /**< how many token references end at this node. */ 
    uint8_t min;   /**< smallest 'char' in children's list. */ 
    uint8_t reserved;  /**< padding. */ 
    uint16_t count;  /**< number of children. (max count = 256, so count must be 16bits) */ 
    token_node *children[0]; /**< list of children nodes. index by (c - min) */ 
}; 

#define NODE_SIZE(count) (sizeof(token_node) + (sizeof(token_node *) * count)) 

static token_node *token_node_new(uint16_t count) { 
    token_node *node = calloc(1, NODE_SIZE(count)); 
    node->count = count; 
    return node; 
} 

static void token_node_build_chain(token_node **pnode, const uint8_t *token, size_t len) { 
    token_node *node; 
    do { 
     /* the last node in the chain will have no children. */ 
     node = token_node_new((len == 0) ? 0 : 1); 
     *pnode = node; /* add node to slot in parent's children list. */ 
     if(len == 0) break; 
     /* new node will have one child. */ 
     node->min = *token; 
     node->count = 1; 
     /* slot where next node will be saved. */ 
     pnode = &(node->children[0]); 
     /* consume char. */ 
     token++; 
     len--; 
    } while(1); 
    /* mark last node as end of a valid token. */ 
    node->ref_count++; 
} 

static void token_node_free(token_node *node) { 
    uint32_t i; 
    uint32_t count = node->count; 

    /* free children nodes. */ 
    for(i=0; i < count; i++) { 
     if(node->children[i]) token_node_free(node->children[i]); 
    } 
    free(node); 
} 

static void token_node_grow(token_node **pnode, uint8_t c) { 
    token_node *node = *pnode; 
    token_node **children; 
    uint8_t old_min = node->min; 
    uint16_t old_count = node->count; 
    uint32_t i; 
    uint8_t min; 
    uint16_t count; 

    if(c < old_min) { 
     min = c; 
     count = old_count + (old_min - min); 
    } else { 
     if(old_count == 0) { 
      /* the list was empty, so this is the first char. */ 
      old_min = c; 
     } 
     min = old_min; 
     c -= old_min; 
     if(c < old_count) { 
      /* don't need to grow. */ 
      return; 
     } 
     count = c + 1; 
    } 

    node = realloc(node, NODE_SIZE(count)); 
    *pnode = node; 
    children = node->children; 
    /* if the 'min' value changed, then we need to move all the old slots up. */ 
    if(old_min != min) { 
     uint32_t diff = old_min - min; 
     for(i=count-1; i >= diff; i--) { 
      children[i] = children[i - diff]; 
     } 
     /* null new slots at start of children list. */ 
     for(i=0; i < diff; i++) { 
      children[i] = NULL; 
     } 
    } else { 
     /* null new slots at end of children list. */ 
     for(i=old_count; i < count; i++) { 
      children[i] = NULL; 
     } 
    } 
    node->min = min; 
    node->count = count; 
} 

static token_node **token_node_find_last_node(token_node **pnode, const uint8_t **ptoken, size_t *plen) { 
    const uint8_t *token = *ptoken; 
    size_t len = *plen; 
    uint32_t c; 
    token_node *node = *pnode; 

    while(node && len) { 
     /* next char. */ 
     c = (*token); 
     /* if c < node->min, then it will underflow and be > node->count. */ 
     c -= node->min; 
     /* make sure c is in range. */ 
     if(c >= node->count) { 
      /* 
      * NOTE: we don't consume this char and "*pnode" will not be null. 
      * When adding tokens, this node will be grown to hold more children. 
      */ 
      break; 
     } 

     /* consume char. */ 
     token++; 
     len--; 
     /* get pointer to next node's slot. */ 
     pnode = &(node->children[c]); 
     node = *pnode; 
    } 

    *ptoken = token; 
    *plen = len; 
    /* return pointer to last node's slot. */ 
    return pnode; 
} 

static void token_node_add(token_node **pnode, const uint8_t *token, size_t len) { 
    token_node *node; 

    /* find last node in chain for this token. */ 
    pnode = token_node_find_last_node(pnode, &token, &len); 

    /* if full token was consumed then we found the last node for this token. */ 
    if(!len) { 
     node = *pnode; 
     node->ref_count++; 
     return; 
    } 

    /* check if the children list of the last node needs to be grown. */ 
    node = *pnode; 
    if(node) { 
     uint32_t c = *token; 
     /* consume char. */ 
     token++; 
     len--; 
     /* grow node to make room for new char. */ 
     token_node_grow(pnode, c); 
     node = *pnode; /* token_node_grow() may change the node's pointer. */ 
     /* get slot for new child. */ 
     pnode = &(node->children[c - node->min]); 
    } 
    /* build node chain for un-consumed part of token. */ 
    token_node_build_chain(pnode, token, len); 
} 

static size_t token_node_longest_token(token_node *node, const uint8_t *text, size_t len) { 
    size_t last_token_len = 0; 
    size_t off = 0; 
    uint32_t c; 

    /* loop until we get a NULL node or run out of text. */ 
    do { 
     if(node->ref_count > 0) { 
      /* found a token, keep track of it's length. */ 
      last_token_len = off; 
     } 
     /* end of input text. */ 
     if(off >= len) break; 
     /* next char. */ 
     c = text[off]; 
     /* if c < node->min, then it will underflow and be > node->count. */ 
     c -= node->min; 
     /* make sure c is in range. */ 
     if(c >= node->count) { 
      /* End of search, no more child nodes. */ 
      break; 
     } 

     /* consume char. */ 
     off++; 
     /* get pointer to next node's slot. */ 
     node = node->children[c]; 
    } while(node); 

    /* return length of largest token found. */ 
    return last_token_len; 
} 

extern token_tree *token_tree_new() { 
    token_tree *tree = malloc(sizeof(token_tree)); 
    tree->root = token_node_new(0); 
    return tree; 
} 

extern void token_tree_free(token_tree *tree) { 
    token_node_free(tree->root); 
    free(tree); 
} 

extern void token_tree_add(token_tree *tree, const char *token, size_t len) { 
    token_node_add(&(tree->root), token, len); 
} 

extern size_t token_tree_longest_token(token_tree *tree, const char *text, size_t len) { 
    return token_node_longest_token(tree->root, text, len); 
} 

#ifdef TEST_TOKEN_TREE 
#include <stdio.h> 
#include <string.h> 

static const char *test_tokens[] = { 
    "s", 
    "stack", 
    "stackoverflow", 
    "over", 
    "overflow", 
    NULL, 
}; 

static const char *test_input[] = { 
    "aastackoverasdfasdf", 
    "stack7777", 
    "777stack777", 
    "overstackflow", 
    NULL, 
}; 

static void add_tokens(token_tree *tree, const char **tokens) { 
    int i; 
    for(i = 0; tokens[i] != NULL; i++) { 
     token_tree_add(tree, tokens[i], strlen(tokens[i])); 
    } 
} 

static void print_tokens(token_tree *tree, const char *text) { 
    size_t len = strlen(text); 
    size_t token_len; 

    printf("input: \"%s\"\n", text); 
    printf("tokens: ["); 
    while(len) { 
     token_len = token_tree_longest_token(tree, text, len); 
     if(token_len > 0) { 
      printf("<%.*s>", (int)token_len, text); 
     } else { 
      printf("?"); 
      token_len = 1; 
     } 
     text += token_len; 
     len -= token_len; 
    } 
    printf("]\n"); 
} 

static void run_test(token_tree *tree, const char **texts) { 
    int i; 
    for(i = 0; texts[i] != NULL; i++) { 
     print_tokens(tree, texts[i]); 
    } 
} 

int main(int argc, char *argv[]) { 
    token_tree *tree = token_tree_new(); 

    add_tokens(tree, test_tokens); 

    run_test(tree, test_input); 
    run_test(tree, test_tokens); 

    token_tree_free(tree); 
} 

#endif 
संबंधित मुद्दे