如何将问号替换为单词

时间:2019-03-24 20:54:02

标签: python-3.x nlp

我有阿拉伯语鸣叫,我想将问号和感叹号替换为阿拉伯语同义词。我尝试使用正则表达式编写此代码,但没有任何反应。我用了jupyter笔记本

def replace_questionmark(tweet):
text = re.sub("!", "تعجب",tweet)
text = re.sub('استفهام','؟' ,tweet)
return tweet

data_df['clean text'] = data_df['Text'].apply(lambda x: replace_questionmark(x))

1 个答案:

答案 0 :(得分:0)

以下代码解决了您的问题

import pandas as pd
import re

Text = [u'I am feeling good !', u'I am testing this code ؟']
data_df = pd.DataFrame(columns=['Text'], data=Text)

def replace_questionmark(tweet):
    text = tweet.replace(u'!', u'تعج')
    text = text.replace(u'؟', u'استفهام')
    return text.encode('utf-8')

data_df['clean text'] = data_df['Text'].apply(lambda x: replace_questionmark(x))

print(data_df)
  

输出

                       Text                      clean text
0       I am feeling good !           I am feeling good تعج
1  I am testing this code ؟  I am testing this code استفهام