python re.compile 并用 ÆØÅ 字符分割

标签 python regex split python-2.x

我对 Python 很陌生。 我确实有一个包含单词列表的文件。它们包含丹麦字母 (ÆØÅ),但 re.compile 不理解这些字符。该函数按每个 ÆØÅ 分割单词。文本是从 Twitter 和 Facebook 下载的,并不总是只包含字母。

text = "Rød grød med fløde.... !! :)"
pattern_split = re.compile(r"\W+")
words = pattern_split.split(text.lower())
words = ['r', 'd', 'gr', 'd', 'med', 'fl', 'de']

正确的结果应该是

    words = ['rød', 'grød', 'med', 'fløde']

如何获得正确的结果?

完整代码

#!/usr/bin/python 
# -*- coding: utf-8 -*-

import math, re, sys, os
reload(sys)
sys.setdefaultencoding('utf-8')

# AFINN-111 is as of June 2011 the most recent version of AFINN
#filenameAFINN = 'AFINN/AFINN-111.txt'

# Get location of file
__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))


filenameAFINN = __location__ + '/AFINN/AFINN-111DK.txt'
afinn = dict(map(lambda (w, s): (w, int(s)), [ 
            ws.strip().split('\t') for ws in open(filenameAFINN) ]))

# Word splitter pattern
pattern_split = re.compile(r"\W+")
#pattern_split = re.compile('[ .,:();!?]+')

def sentiment(text):
    print(text)
    words = pattern_split.split(text.lower().strip())
    print(words)
    sentiments = map(lambda word: afinn.get(word, 0), words)
    if sentiments:
        sentiment = float(sum(sentiments))/math.sqrt(len(sentiments))

    else:
        sentiment = 0
    return sentiment


# Print result
text = "ånd ånd med fløde... :)asd "
id = 999
split = "###"
print("%6.2f%s%s%s%s" % (sentiment(text), split, id, split, text))

最佳答案

重新编写脚本以使用最佳实践:

import csv
import math
import os
import re

LOCATION = os.path.dirname(os.path.abspath(__file__))
afinn_filename = os.path.join(LOCATION, '/AFINN/AFINN-111DK.txt')

pattern_split = re.compile(r"\W+")

with open(afinn_filename, encoding='utf8', newline='') as infile:
    reader = csv.reader(infile, delimiter='\t')
    afinn = {key: int(score) for key, score in reader}


def sentiment(text):
    words = pattern_split.split(text.lower().strip())
    if not words:
        return 0
    sentiments = [afinn.get(word, 0) for word in words]
    return sum(sentiments) / math.sqrt(len(sentiments))


# Print result
text = "ånd ånd med fløde... :)asd "
id = 999
split = "###"
print('{sentiment:6.2f}{split}{id}{split}{text}'.format(
    sentiment=sentiment(text), id=id, split=split, text=text))

使用 Python 3 运行此命令意味着 text 是一个 Unicode 对象,并且使用 re.UNICODE 集解释正则表达式。

在 Python 2 中,您可以使用:

text = u"ånd ånd med fløde... :)asd "

(注意字符串上的前导 u 前缀)和

pattern_split = re.compile(ur"\W+", re.UNICODE)

您的 AFINN 文件仍会被读取为 CSV,但事后会从 UTF8 解码key,方法是:

with open(afinn_filename, 'rb') as infile:
    reader = csv.reader(infile, delimiter='\t')
    afinn = {key.decode('utf8'): int(score) for key, score in reader}

关于python re.compile 并用 ÆØÅ 字符分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16549161/

相关文章:

Python - 具有两个参数的多处理 StarMap

python - "pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available."尽管作为可信主机运行

python - 在 selenium webdriver.PhantomJS 上设置超时

java - 如何使用正则表达式来查找特定的 URL?

ruby - 在 ruby​​ 正则表达式中匹配换行符 `\n`

c - 逐行读取数字并将其拆分为 C 中的多个变量

Python 通过多个键使用 lambda 排序列表或字典

android - 查找重复数字的正则表达式不起作用

当分隔符具有单引号时,Java String split() 函数无法按预期工作

sql-server - 如何在 SQL Server 中的特定字符后分割字符串并将该值更新到特定列