python punctuation_Python string.punctuation方法代码示例

本文整理汇总了Python中string.punctuation方法的典型用法代码示例。如果您正苦于以下问题:Python string.punctuation方法的具体用法?Python string.punctuation怎么用?Python string.punctuation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块string的用法示例。

在下文中一共展示了string.punctuation方法的25个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: clean_captions

​点赞 6

# 需要导入模块: import string [as 别名]

# 或者: from string import punctuation [as 别名]

def clean_captions(captions):

# Prepare translation table for removing punctuation

table = str.maketrans('', '', string.punctuation)

for _, caption_list in captions.items():

for i in range(len(caption_list)):

caption = caption_list[i]

# Tokenize i.e. split on white spaces

caption = caption.split()

# Convert to lowercase

caption = [word.lower() for word in caption]

# Remove punctuation from each token

caption = [w.translate(table) for w in caption]

# Remove hanging 's' and 'a'

caption = [word for word in caption if len(word)>1]

# Remove tokens with numbers in them

caption = [word for word in caption if word.isalpha()]

# Store as string

caption_list[i] = ' '.join(caption)

开发者ID:dabasajay,项目名称:Image-Caption-Generator,代码行数:20,

示例2: normalize_answer

​点赞 6

# 需要导入模块: import string [as 别名]

# 或者: from string import punctuation [as 别名]

def normalize_answer(s):

"""Lower text and remove extra whitespace."""

def remove_articles(text):

return re_art.sub(' ', text)

def remove_punc(text):

return re_punc.sub(' ', text) # convert punctuation to spaces

def white_space_fix(text):

return ' '.join(text.split())

def lower(text):

return text.lower()

return white_space_fix(remove_articles(remove_punc(lower(s))))

开发者ID:hugochan,项目名称:BAMnet,代码行数:17,

示例3: random_string

​点赞 6

# 需要导入模块: import string [as 别名]

# 或者: from string import punctuation [as 别名]

def random_string(n):

if n == 0:

return ""

x = random.random()

if x > 0.5:

pad = " " * n

elif x > 0.3:

pad = "".join(random.choices(digits + " \t\n", k=n))

elif x > 0.2:

pad = "".join(random.choices(ascii_uppercase + " \t\n", k=n))

elif x > 0.1:

pad = "".join(random.choices(ascii_uppercase + digits + " \t\n", k=n))

else:

pad = "".join(

random.choices(ascii_uppercase + digits + punctuation + " \t\n", k=n)

)

return pad

开发者ID:zzzDavid,项目名称:ICDAR-2019-SROIE,代码行数:21,

示例4: tokenize_sentence

​点赞 6

# 需要导入模块: import string [as 别名]

# 或者: from string import punctuation [as 别名]

def tokenize_sentence(sentence):

"""

Splits a sentence into words, strips punctuation and turns it to lowercase.

:param sentence : the sentence to tokenize.

:type sentence : str

:return : list of words

"""

# Get rid of non-ascii characters to avoid errors with unrecognised characters

sentence = "".join([c for c in sentence if 0 < ord(c) < 127])

sentence = sentence.encode("ascii", errors="ignore").decode()

# Only works in Python 3

sentenceNoPunctuation = sentence.translate(str.maketrans("", "", string.punctuation))

sentenceLower = sentenceNoPunctuation.lower()

sentenceWords = sentenceLower.split()

return sentenceWords

开发者ID:Wluper,项目名称:edm,代码行数:24,

示例5: __init__

​点赞 6

# 需要导入模块: import st

你可能感兴趣的:(python,punctuation)