更改词汇表(单词袋)

时间:2018-07-06 20:50:39

标签: python machine-learning nlp artificial-intelligence countvectorizer

嘿,我正在处理具有大量XSS攻击有效负载的数据集,我正在尝试遵循单词袋方法进行特征提取。我已经在下面创建了代码。

我想删除和更改使用矢量化程序创建的词汇。

import pandas as pd
train = pd.read_csv('xss_v.csv', error_bad_lines=False)
train.shape
train.columns.values
from bs4 import BeautifulSoup             
example1 = BeautifulSoup(train["Script;\t\t"][0])  
print train["Script;\t\t"][0]
print example1.get_text()
import re
letters_only= re.sub(" \d+", " ",example1.get_text() )
print letters_only
lower_case = letters_only.lower()
words = lower_case.split()
from nltk.corpus import stopwords # Import the stop word list
print stopwords.words("english")
words = [w for w in words if not w in stopwords.words("english")]
print words
print words
print lower_case
def review_to_words( raw_review ):
    # 1. Remove HTML
    review_text = BeautifulSoup(raw_review).get_text() 
    #
    # 2. Remove non-letters        
    letters_only = re.sub("[^a-zA-Z]", " ", review_text) 
    #
    # 3. Convert to lower case, split into individual words
    words = letters_only.lower().split()                             
    #
    # 4. In Python, searching a set is much faster than searching
    #   a list, so convert the stop words to a set
    stops = set(stopwords.words("english"))                  
    # 
    # 5. Remove stop words
    meaningful_words = [w for w in words if not w in stops]   
    #
    # 6. Join the words back into one string separated by space, 
    # and return the result.
    return( " ".join( meaningful_words ))   
clean_review = review_to_words( train["Script;\t\t"][0] )
print clean_review
num_reviews = train["Script;\t\t"].size
# Initialize an empty list to hold the clean reviews
clean_train_reviews = []
# Loop over each review; create an index i that goes from 0 to the length
for i in xrange( 0, num_reviews ):
    # Call our function for each one, and add the result to the list of
    # clean reviews
    clean_train_reviews.append( review_to_words( train["Script;\t\t"][i] ) )
clean_train_reviews = []
for i in xrange( 0, num_reviews ):
    # If the index is evenly divisible by 1000, print a message
    if( (i+1)%1000 == 0 ):
        print "Review %d of %d\n" % ( i+1, num_reviews )                                                                    
    clean_train_reviews.append( review_to_words( train["Script;\t\t"][i] ))
print "Creating the bag of words...\n"
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(analyzer = "word",tokenizer = None,preprocessor = None,                             stop_words = None,max_features = 5000)                         
train_data_features = vectorizer.fit_transform(clean_train_reviews)
train_data_features = train_data_features.toarray()
vocab = vectorizer.get_feature_names()
print vocab

在此之后它给了我:-

[u'abcdef', u'abg', u'absolute', u'aca', u'acaapabi', u'acga', u'actiontype', u'ad', u'adata', u'admin', u'adw', u'adz', u'ae', u'aef', u'ag', u'ageabablahiadaaoadeakq', u'agn', u'ago', u'aguacgbyag', u'ahi', u'ahm', u'ajavascript', u'ale', u'alert', u'allowscriptaccess', u'always', u'anchorclick', u'animate', u'application', u'ascript', u'asp', u'attribute', u'attributename', u'autofocus', u'background', u'bar', u'base', u'bc', u'bcscript', u'bealert', u'beh', u'behavior', u'bgsound', u'bin', u'binding', u'bjavascript', u'blah', u'body', u'bonjour', u'br', u'button', u'bx', u'cat', u'cdata', u'charset', u'chr', u'cjavascript', u'ckers', u'class', u'classid', u'click', u'clickme', u'clsid', u'cmd', u'code', u'colon', u'color', u'com', u'comment', u'confirm', u'constructor', u'content', u'cookie', u'crip', u'cript', u'cscript', u'css', u'csvg', u'current', u'cursor', u'data', u'datafld', u'dataformatas', u'datasrc', u'default', u'definegetter', u'deleteuser', u'displayed', u'div', u'djavascript', u'document', u'dom', u'domain', u'dynsrc', u'ealert', u'echo', u'ejavascript', u'element', u'embed', u'equiv', u'ert', u'ession', u'eval', u'event', u'ex', u'exec', u'execute', u'exp', u'expr', u'expression', u'family', u'farsi', u'fdae', u'filter', u'firefox', u'fjavascript', u'folder', u'font', u'foo', u'fooalert', u'form', u'formaction', u'frame', u'frameset', u'friends', u'fromcharcode', u'fscript', u'function', u'get', u'getelementbyid', u'glow', u'gohttp', u'google', u'googlealert', u'gotcha', u'green', u'group', u'gt', u'gte', u'ha', u'head', u'height', u'hello', u'hidden', u'hover', u'href', u'htc', u'html', u'http', u'id', u'ie', u'iframe', u'ij', u'im', u'image', u'img', u'implementation', u'import', u'index', u'infet', u'innerhtml', u'input', u'ipt', u'ja', u'jav', u'java', u'javafunctionname', u'javapgno', u'javas', u'javascript', u'jpchq', u'jpg', u'js', u'jscript', u'king', u'label', u'layer', u'le', u'left', u'length', u'lert', u'li', u'line', u'link', u'list', u'livescript', u'location', u'long', u'lowsrc', u'lpar', u'lt', u'mac', u'maction', u'maliciouscode', u'marquee', u'math', u'member', u'message', u'meta', u'microsoft', u'mocha', u'mouse', u'movies', u'moz', u'mozilla', u'msgbox', u'name', u'namespace', u'navigator', u'ndocument', u'net', u'newline', u'njcmlwdd', u'nosuchmethod', u'noxss', u'ns', u'nxss', u'object', u'ogle', u'onbegin', u'onblur', u'onerror', u'onfilterchange', u'onfocus', u'onformchange', u'onforminput', u'oninput', u'onload', u'onmouseover', u'onreadystatechange', u'onscroll', u'opera', u'org', u'page', u'pageclick', u'paragraph', u'param', u'paramheight', u'payload', u'perl', u'pg', u'php', u'port', u'position', u'poster', u'prefix', u'pression', u'print', u'prompt', u'prototype', u'pt', u'px', u'quot', u'red', u'redirect', u'referenceerror', u'refresh', u'rel', u'ri', u'ript', u'ro', u'rpar', u'rpdgxlpjxzy', u'rvrsh', u'schemas', u'scr', u'scri', u'script', u'scriptlet', u'scrscriptipt', u'sct', u'securitycompass', u'self', u'set', u'setter', u'simpatie', u'site', u'size', u'somecommand', u'somevariables', u'source', u'src', u'statusline', u'stream', u'string', u'strokecolor', u'strokeweight', u'style', u'stylesheet', u'svg', u'swf', u'table', u'targetelement', u'td', u'test', u'text', u'thesiteyouareon', u'time', u'title', u'top', u'true', u'tt', u'type', u'ua', u'uff', u'ul', u'url', u'urn', u'useragent', u'userid', u'utf', u'value', u'values', u'var', u'vasc', u'vbscript', u'veris', u'victimsite', u'video', u'vior', u'visibility', u'vml', u'vmlframe', u'vulnerable', u'want', u'white', u'width', u'window', u'write', u'www', u'xa', u'xados', u'xaejavascript', u'xafjavascript', u'xb', u'xbb', u'xbc', u'xbejavascript', u'xbf', u'xbfjavascript', u'xc', u'xe', u'xee', u'xef', u'xf', u'xlink', u'xml', u'xmlns', u'xpression', u'xs', u'xss', u'xssmoz', u'xsstest', u'xxs', u'xxx', u'xxxxxx', u'xyz', u'yoursite', u'ywxlcnqomtmznyk']

现在我要编辑词汇表并删除所有垃圾数据,例如abcdef,怎么可能呢?

0 个答案:

没有答案