【问题标题】:Extracting all text before first tab from a list of strings从字符串列表中提取第一个选项卡之前的所有文本
【发布时间】:2021-01-27 13:18:33
【问题描述】:

所以我有来自http://www.manythings.org/anki/ 的这个 text_data 看起来像这样

['Hi.\tHallo!\tCC-BY 2.0 (France) Attribution: tatoeba.org #538123 (CM) & #380701 (cburgmer)\n',
 'Hi.\tGrüß Gott!\tCC-BY 2.0 (France) Attribution: tatoeba.org #538123 (CM) & #659813 (Esperantostern)\n',
 'Run!\tLauf!\tCC-BY 2.0 (France) Attribution: tatoeba.org #906328 (papabear) & #941078 (Fingerhut)\n',
 'Wow!\tPotzdonner!\tCC-BY 2.0 (France) Attribution: tatoeba.org #52027 (Zifre) & #2122382 (Pfirsichbaeumchen)\n',
 'Wow!\tDonnerwetter!\tCC-BY 2.0 (France) Attribution: tatoeba.org #52027 (Zifre) & #2122391 (Pfirsichbaeumchen)\n',
 'Fire!\tFeuer!\tCC-BY 2.0 (France) Attribution: tatoeba.org #1829639 (Spamster) & #1958697 (Tamy)\n',
 'Help!\tHilfe!\tCC-BY 2.0 (France) Attribution: tatoeba.org #435084 (lukaszpp) & #575889 (MUIRIEL)\n',
...
]

我做了这个

English = []
for sent in data_examples:
    pattern  = re.compile(r'.+?\t')
    matches = pattern.finditer(sent)
    for match in matches:
        English.append(match)

如何捕捉文本中的英语?我的不是真的工作。

【问题讨论】:

  • 首先,您可以将pattern=... 移到循环之外,因为它没有改变。
  • mine is not really working - 不是一个详细的问题陈述......如果你只想要第一个词,为什么要使用finditer?为什么不pattern.match(sent)
  • 你的例子中没有英文。
  • 顺便说一句,为什么是 Tatoeba?你认为你可以信任这些语料库吗?它们看起来是用户贡献的,没有进行适当的本地化质量保证。
  • 谢谢大家的cmets。我很新,我在 youtube 上查看过,人们使用 finditer,我猜 Tomerikoo 使用 match 提出了一个很好的观点。我正在努力学习,质量不是我的首要考虑因素

标签: python string list split


【解决方案1】:

解决方案:
这可能会解决您的目的

import nltk
words = None
try:
    words = set(nltk.corpus.words.words())
except:
    nltk.download('words')
    words = set(nltk.corpus.words.words())

# Extra words which are not present in nltk words corpus
words_need_to_include = ['france']

for w in words_need_to_include:
    words.add(w)

# Words which we don't want in nltk words corpus
words_need_to_exclude = ['by']

for w in words_need_to_exclude:
    words.remove(w)

# Input data
in_text = ['Hi.\tHallo!\tCC-BY 2.0 (France) Attribution: tatoeba.org #538123 (CM) & #380701 (cburgmer)\n france',
 'Hi.\tGrüß Gott!\tCC-BY 2.0 (France) Attribution: tatoeba.org #538123 (CM) & #659813 (Esperantostern)\n',
 'Run!\tLauf!\tCC-BY 2.0 (France) Attribution: tatoeba.org #906328 (papabear) & #941078 (Fingerhut)\n',
 'Wow!\tPotzdonner!\tCC-BY 2.0 (France) Attribution: tatoeba.org #52027 (Zifre) & #2122382 (Pfirsichbaeumchen)\n',
 'Wow!\tDonnerwetter!\tCC-BY 2.0 (France) Attribution: tatoeba.org #52027 (Zifre) & #2122391 (Pfirsichbaeumchen)\n',
 'Fire!\tFeuer!\tCC-BY 2.0 (France) Attribution: tatoeba.org #1829639 (Spamster) & #1958697 (Tamy)\n',
 'Help!\tHilfe!\tCC-BY 2.0 (France) Attribution: tatoeba.org #435084 (lukaszpp) & #575889 (MUIRIEL)\n',
]

# Code
English = []
for x in in_text:
    English.append(" ".join(w for w in nltk.wordpunct_tokenize(x) if w.lower() in words))
    #print(" ".join(nltk.wordpunct_tokenize(x)))

print(English)

输出:

['Hi France Attribution france', 'Hi France Attribution', 'Run France Attribution', 'Wow France Attribution', 'Wow France Attribution', 'Fire France Attribution', 'Help France Attribution']

【讨论】:

  • 还添加了变量以从字符串中排除不需要但属于英语的单词
【解决方案2】:

您的英语片段位于第一列。

你需要做的就是

English = [sent.split('\t')[0] for sent in data_examples]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2014-11-28
    • 1970-01-01
    相关资源
    最近更新 更多