【问题标题】:how to separate text and labels using python如何使用python分隔文本和标签
【发布时间】:2019-12-08 23:48:46
【问题描述】:

我是 NLP 的新手,我有一个带有标签 01 的文本。

如何分隔标签并创建新列?请帮帮我。

这是我的带有标签的文本:

Everything from acting to cinematography was solid.     1

Definitely worth checking out.      1            
I purchased this and within 2 days it was no longer working!!!!!!!!!    0

【问题讨论】:

    标签: python python-3.x nlp


    【解决方案1】:

    看起来您的源文档可能是tab-delimited file,但在粘贴到 SO 窗口时格式已更改。如果是这种情况,那么您应该使用 csv 包。

    假设您的文本和标签之间没有特殊的分隔符(例如\t,),您可以简单地将标签提取为该行的最后一个非空白字符。比如……

    # suppose you read the file out as a gigantic string
    text_and_labels = """
    Everything from acting to cinematography was solid.     1
    
    Definitely worth checking out.      1
    I purchased this and within 2 days it was no longer working!!!!!!!!!    0
    """
    
    data = []
    lines = text_and_labels.split('\n')  # split each line
    for line in lines:
        line = line.strip()  # remove any outside whitespace
        if line == '':
            continue  # it's a blank line
        label = line[-1]  # the last non-whitespace character
        text = line[:-1].strip()  # everything else, without the extra whitespace
        data.append((text, label))
    data[0]
    >>> ('Definitely worth checking out.', '1')
    

    【讨论】:

      【解决方案2】:

      如果文件具有正确的格式文本,而不是使用简单的文件处理和正确的索引。否则对于格式错误的文本,您可以使用正则表达式。

      file = open('filename','r+')
      list1 = []
      for line in file.readlines():
          try:
              list1.append(line[-2])
          except:
              pass
      

      现在您可以使用此列表来创建列

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-30
        • 2022-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        相关资源
        最近更新 更多