【问题标题】:create variable name based on text in the sentence根据句子中的文本创建变量名
【发布时间】:2020-06-03 10:20:17
【问题描述】:

我有一个句子列表。每个句子都必须转换为 json。每个句子都有一个唯一的“名称”,该名称也在该 json 中指定。问题是句子的数量很大,所以手动起名字很单调。名称应该与句子的含义相似,例如,如果句子是“你喜欢蛋糕吗?”那么名字应该像“likeCake”。我想自动化为每个句子创建名称的过程。我搜索了文本摘要,但结果不是句子摘要而是段落摘要。这个怎么办?

【问题讨论】:

    标签: python automation summarization


    【解决方案1】:

    这类任务用于自然语言处理。通过删除Stop Words,您可以获得类似于您想要的结果。基于此article,您可以使用Natural Language Toolkit 来处理停用词。安装 libray (pip install nltk) 后,您可以执行以下操作:

    from nltk.tokenize import word_tokenize
    from nltk.corpus import stopwords
    import string
    
    # load data
    file = open('yourFileWithSentences.txt', 'rt')
    lines = file.readlines() 
    file.close()
    stop_words = set(stopwords.words('english'))
    for line in Lines: 
        # split into words
        tokens = word_tokenize(line)
        # remove punctuation from each word
        table = str.maketrans('', '', string.punctuation)
        stripped = [w.translate(table) for w in tokens]
        # filter out stop words
    
        words = [w for w in words if not w in stop_words]
        print(f"Var name is {''.join(words)}")
    

    请注意,您可以通过添加您可能想要删除的任何其他词来扩展 stop_words 集。

    【讨论】:

      猜你喜欢
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2012-11-18
      • 2013-11-16
      • 1970-01-01
      • 2014-04-11
      相关资源
      最近更新 更多