python RE reviews

Something about RE

Version:2.2.1

Description

This module provides regular expression matching operations similar to
those found in Perl. It supports both 8-bit and Unicode strings; both
the pattern and the strings being processed can contain null bytes and
characters outside the US ASCII range.

Regular expressions can contain both special and ordinary characters.
Most ordinary characters, like "A", "a", or "0", are the simplest
regular expressions; they simply match themselves.  You can
concatenate ordinary characters, so last matches the string 'last'.

All of the special characters

The special characters are:
        "."      Matches any character except a newline.
        "^"      Matches the start of the string.
        "$"      Matches the end of the string or just before the newline at
                 the end of the string.
        "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
                 Greedy means that it will match as many repetitions as possible.
        "+"      Matches 1 or more (greedy) repetitions of the preceding RE.
        "?"      Matches 0 or 1 (greedy) of the preceding RE.
        *?,+?,?? Non-greedy versions of the previous three special characters.
        {m,n}    Matches from m to n repetitions of the preceding RE.
        {m,n}?   Non-greedy version of the above.
        "\\"     Either escapes special characters or signals a special sequence.
        []       Indicates a set of characters.
                 A "^" as the first character indicates a complementing set.
        "|"      A|B, creates an RE that will match either A or B.
        (...)    Matches the RE inside the parentheses.
                 The contents can be retrieved or matched later in the string.
        (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
        (?:...)  Non-grouping version of regular parentheses.
        (?P<name>...) The substring matched by the group is accessible by name.
        (?P=name)     Matches the text matched earlier by the group named name.
        (?#...)  A comment; ignored.
        (?=...)  Matches if ... matches next, but doesn't consume the string.
        (?!...)  Matches if ... doesn't match next.
        (?<=...) Matches if preceded by ... (must be fixed length).
        (?<!...) Matches if not preceded by ... (must be fixed length).
        (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
                           the (optional) no pattern otherwise.

The special sequences consist of "\\" and a character from the list
    below.  If the ordinary character is not on the list, then the
    resulting RE will match the second character.
        \number  Matches the contents of the group of the same number.
        \A       Matches only at the start of the string.
        \Z       Matches only at the end of the string.
        \b       Matches the empty string, but only at the start or end of a word.
        \B       Matches the empty string, but not at the start or end of a word.
        \d       Matches any decimal digit; equivalent to the set [0-9] in
                 bytes patterns or string patterns with the ASCII flag.
                 In string patterns without the ASCII flag, it will match the whole
                 range of Unicode digits.
        \D       Matches any non-digit character; equivalent to [^\d].
        \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
                 bytes patterns or string patterns with the ASCII flag.
                 In string patterns without the ASCII flag, it will match the whole
                 range of Unicode whitespace characters.
        \S       Matches any non-whitespace character; equivalent to [^\s].
        \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
                 in bytes patterns or string patterns with the ASCII flag.
                 In string patterns without the ASCII flag, it will match the
                 range of Unicode alphanumeric characters (letters plus digits
                 plus underscore).
                 With LOCALE, it will match the set [0-9_] plus characters defined
                 as letters for the current locale.
        \W       Matches the complement of \w.
        \\       Matches a literal backslash.

Functions contained:

  compile(pattern, flags=0)
    Compile a regular expression pattern, returning a pattern object.

  escape(pattern)
    Escape all the characters in pattern except ASCII letters, numbers and '_'.

  findall(pattern, string, flags=0)
    Return a list of all non-overlapping matches in the string.
    
    If one or more capturing groups are present in the pattern, return
    a list of groups; this will be a list of tuples if the pattern
    has more than one group.
    
    Empty matches are included in the result.

  finditer(pattern, string, flags=0)
    Return an iterator over all non-overlapping matches in the
    string.  For each match, the iterator returns a match object.
    
    Empty matches are included in the result.

  fullmatch(pattern, string, flags=0)
    Try to apply the pattern to all of the string, returning
    a match object, or None if no match was found.

  match(pattern, string, flags=0)
    Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found.

  purge()
    Clear the regular expression caches

  search(pattern, string, flags=0)
    Scan through string looking for a match to the pattern, returning
    a match object, or None if no match was found.

  split(pattern, string, maxsplit=0, flags=0)
    Split the source string by the occurrences of the pattern,
    returning a list containing the resulting substrings.  If
    capturing parentheses are used in pattern, then the text of all
    groups in the pattern are also returned as part of the resulting
    list.  If maxsplit is nonzero, at most maxsplit splits occur,
    and the remainder of the string is returned as the final element
    of the list.

  sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the match object and must return
    a replacement string to be used.

subn(pattern, repl, string, count=0, flags=0)
    Return a 2-tuple containing (new_string, number).
    new_string is the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in the source
    string by the replacement repl.  number is the number of
    substitutions that were made. repl can be either a string or a
    callable; if a string, backslash escapes in it are processed.
    If it is a callable, it's passed the match object and must
    return a replacement string to be used.

  template(pattern, flags=0)
    Compile a template pattern, returning a pattern object

DATA

	A = <RegexFlag.ASCII: 256>
	ASCII = <RegexFlag.ASCII: 256>
	DOTALL = <RegexFlag.DOTALL: 16>
	I = <RegexFlag.IGNORECASE: 2>
	IGNORECASE = <RegexFlag.IGNORECASE: 2>
	L = <RegexFlag.LOCALE: 4>
	LOCALE = <RegexFlag.LOCALE: 4>
	M = <RegexFlag.MULTILINE: 8>
	MULTILINE = <RegexFlag.MULTILINE: 8>
	S = <RegexFlag.DOTALL: 16>
	U = <RegexFlag.UNICODE: 32>
	UNICODE = <RegexFlag.UNICODE: 32>
	VERBOSE = <RegexFlag.VERBOSE: 64>
	X = <RegexFlag.VERBOSE: 64>
	__all__ = ['match', 'fullmatch', 'search', 'sub', 'subn', 'split', 'fi...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
商品评价信息分析可以通过Python中的自然语言处理(NLP)技术来实现。下面是一个基于Python的商品评价信息分析的代码示例: ``` import pandas as pd import numpy as np import re import nltk from nltk.corpus import stopwords from sklearn.feature_extraction.text import CountVectorizer from sklearn.decomposition import LatentDirichletAllocation # 导入数据 df = pd.read_csv('product_reviews.csv') # 数据清洗 df.dropna(inplace=True) df['review'] = df['review'].apply(lambda x: re.sub('[^a-zA-Z]', ' ', x)) df['review'] = df['review'].apply(lambda x: x.lower()) nltk.download('stopwords') stop_words = set(stopwords.words('english')) df['review'] = df['review'].apply(lambda x: ' '.join([word for word in x.split() if word not in stop_words])) # 特征提取 vectorizer = CountVectorizer(max_df=0.95, min_df=2, max_features=1000, ngram_range=(1, 2)) X = vectorizer.fit_transform(df['review']) # LDA主题建模 lda_model = LatentDirichletAllocation(n_components=5, random_state=42) lda_model.fit(X) feature_names = vectorizer.get_feature_names() # 输出每个主题的关键词 for index, topic in enumerate(lda_model.components_): top_keywords = [feature_names[i] for i in topic.argsort()[:-10 - 1:-1]] print(f'Topic {index}: {" ".join(top_keywords)}') # 输出每个评价对应的主题 topic_values = lda_model.transform(X) df['topic'] = topic_values.argmax(axis=1) print(df[['review', 'topic']]) ``` 以上代码的流程如下: 1. 导入数据,其中每一行表示一个评价。 2. 对评价文本进行清洗,去除数字和标点符号,转换为小写,去除停用词。 3. 使用CountVectorizer提取特征,将文本转换为向量表示。 4. 使用LatentDirichletAllocation进行LDA主题建模,得到每个主题的关键词。 5. 输出每个评价对应的主题。 需要注意的是,这只是一个简单的示例代码,实际应用中可能需要进行更复杂的数据清洗和特征提取,同时LDA主题建模的结果需要进行分析和解释。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值