Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python标记化_Python_Tokenize - Fatal编程技术网

Python标记化

Python标记化,python,tokenize,Python,Tokenize,我是Python新手,我有一个标记化任务 输入是一个包含句子的.txt文件 输出是带有标记的.txt文件,当我说标记时,我的意思是:简单的单词“' 我有这个功能: 输入: Elemnt是一个带标点符号或不带标点符号的单词,可以是这样的单词:Hi或said:或said“ StrForCheck:是我想从单词中分离出来的标点符号数组 TokenFile:是我的输出文件 def CheckIfSEmanExist(元素、StrForCheck、令牌文件): 代码在标点数组上循环,如果他找到了一个,我会

我是Python新手,我有一个标记化任务 输入是一个包含句子的.txt文件 输出是带有标记的.txt文件,当我说标记时,我的意思是:简单的单词“'

我有这个功能: 输入: Elemnt是一个带标点符号或不带标点符号的单词,可以是这样的单词:Hi或said:或said“ StrForCheck:是我想从单词中分离出来的标点符号数组 TokenFile:是我的输出文件

def CheckIfSEmanExist(元素、StrForCheck、令牌文件):

代码在标点数组上循环,如果他找到了一个,我会检查标点是单词中的第一个字母还是最后一个字母,然后在输出文件中以不同的行写入单词和标点

但我的问题是,除了以下文字外,它在整个文本中都非常有效: “创造的就业机会”、公共的、警察的”

注意

for l in open('some_file.txt', 'r'):
    ...
在每一行上重复,因此您只需要考虑在一行中要做什么。

考虑以下功能:

def tokenizer(l):
    prev_i = 0
    for (i, c) in enumerate(l):
        if c in ',.?!- ':
            if prev_i != i:
                yield l[prev_i: i]
            yield c
            prev_i = i + 1
    if prev_i != 0:
        yield l[prev_i: ]
它在行进中“吐出”代币。您可以这样使用它:

l = "hello, hello, what's all this shouting? We'll have no trouble here"
for tok in tokenizer(l):
    print tok


但是我还需要在我的文件中写下标点符号的意思,根据你的句子,我的输出应该是:你好,你好,这是什么?
l = "hello, hello, what's all this shouting? We'll have no trouble here"
for tok in tokenizer(l):
    print tok
hello
,

hello
,

what's

all

this

shouting
?

We'll

have

no

trouble

here