我使用python将文本文件中句子中的单词转换为列表中的单个标记,以便统计单词频率。我在将不同的句子转换成一个列表时遇到了麻烦。下面是我要做的:
f = open('music.txt', 'r')
sent = [word.lower().split() for word in f]
这给我提供了以下列表:
[['party', 'rock', 'is', 'in', 'the', 'house', 'tonight'],
['everyb
我正在尝试将一个文本文件读入perl中的变量中,但是它只读取一个文本文件直到第一个换行符字符。(在句子末尾输入)下面是我的代码:
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use v5.16;
open(FILE,"<:encoding(UTF-8)", 'data/text.txt') or die "Can't read file [$!]\n";
chomp(my $document = (<FILE>));
close (FILE);
谢谢你
我有一个文本文件,大约有10个句子。
My Name is Kerry.
I am a female.
My pet is a cat.
It's name is Ronald.
I hate rats.
我想要做的是从这个文本文件中读取一个句子,并将它传递给我编写的fineSearch方法。
def fineSearch(wd):
for line in wd:
for word in line.strip().split():
if word.endswith(('ts.','ld')):
我正在尝试从文本文件中删除停止词。文本文件由9000+语句组成,每个句子位于各自的行上。
代码似乎工作正常,但我显然遗漏了一些东西,因为输出文件已经从文本文档中删除了行结构,而我显然希望保留它。
这是密码;
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
with open(r"C:\\pytest\twitter_problems.txt",'r', encoding="utf8") as inFile, open(r"C:\\pyte
所以我真的看不出我在这里做错了什么,句子的数量一直在说它是0,但是我试图用text.count(‘’)来计算句子/停顿的数量。
我的代码中有没有什么东西可以让这个打印输出为"0"?
谢谢
def countSentences(fileName) :
"""This is a function to count the number
of sentences in a given text file"""
f = open(fileName, 'r')
text = f.read()
我是python编程的初学者,我需要一个函数来帮助我在特定字符之后读取每一行的文件,例如:
这是文本文件的格式。
<ABC>
language \sometext.com xyz
文本文件中充满了这些示例句子,我只需要字符串,它位于'‘和’‘之间。(在上述示例中只有“文本”)。
这是代码,但我无法获得100%的输出。
f = open("test.txt", "r")
for x in f:
if "\\" in x:
x = x.rstrip('\\')
print(x)
我有一个关于c++中文件输入的问题。我希望能够创建一个字符串变量并从文件中读入一句话。我该怎么做呢?这就是我到目前为止所拥有的代码。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string word;
ifstream fin;
// Open the file
fin.open("file.txt");
// Read in the sentence from the file
fin >
我正在编写一个应该读取文件的c++程序。如果该文件包含一组句子,比如一个段落,我将如何将整个段落读成一个字符串?当我尝试编写这个代码时,我的程序只能读取第一个单词,当它看到空间序列时,它就停止了。
例如,:
如果我的句子是:you are a great programmer.
它应该显示:you are a great programmer.
但我得到的是:you
如何编码this...can --有人帮我举个例子吗?
以下是我到目前为止尝试过的:
string b;
ifstream inFile( "file.txt", ios::in );
if ( !
我有一个句子的文本文件。我需要随机读取字符串,并在输出文件中输出字符串及其行号。我编写了以下代码:
ifile = open("test2.txt", "r")
otfile = open("OUT3.txt", "w")
otfile.write("Randomly Selected String \t\t\t Line Number")
import random
i=0
for lines in ifile.readline():
line = lines[i]
words = line
我刚进入python,我想在txt文件中执行一些搜索字符串。我一直在使用TwitterSearch API爬行一些tweet,然后将其导出到txt文件中。在我的txt文件中,有一些句子如下:
1|@tephen_ian|@mypreviana ya yg tabrakan maut itu lo... Antara KA 225 sama 220 thn 1987 itu... Yg bintaro 2 itu td pagi|None
2|@adibnurazis|Warga Jakarta korban tabrakan Bintaro ditanggung Pemprov DKI htt
我们希望检查这个句子是声明性的还是偶尔的,然后将其存储在declarative.txt文件中,然后将其余的句子放在others.txt文件中,然后将declarative.txt的行数放在文件的末尾。
如果一个句子以句号结尾,它就是“陈述式的”。
输入文件($1)
this life is good.
neverthe less.
suppppppppppppppppperb.
the best coders.
everything is good?
are you okay dude?
ma man !!
我的代码到目前为止
#!/bin/sh
while read row
do
x=$ro
我正在尝试从文件中抓取大量数字,将它们存储为数组,然后将它们相加。
nums = Array.new
x, total=0, 0
file = File.open("inputfile.txt", 'r')
while !file.eof?
nums[x] = file.readline
x+=1
end
while x>0
x-=1
total += nums[x]
end
puts total
当我的代码执行时,我得到一个错误,说:
String can't be coerced into Fixnum (TypeError
我正在用Python2.7读取一个文本文件,我要做的是将文本文件中每句话的字数填充到一维数组中。这就是我所拥有的:
f = open(file_txt, 'r')
sen_prog = []
for line in f:
sents = line.split('.')
sen_prog.append(sents)
sen_p = []
for a in sen_prog:
for b in a:
sen_p.append(b)
sen_p = numpy.array(sen_p)
sen_p = sen_p[se
我有一个.txt文件如下:
A B C
England vs autralia
2004
100
D E F
japan vs argentina
3045
140
D E F
india vs pakistan
2012
150
J F G
south africa vs india
1967
100
K GHD D
australia vs pakistan
1993
453
Z E Q
pakistan vs england
2013
150
我想阅读它并存储在变量中。(每行转到一个变量)。
我有这段代码,但它一次读一行,然后作为字符串读取。
if ( file != NULL )
我的C程序有以下问题。它的部分功能是读取一些文本并将其拆分成句子,然后将这些句子写入文件中。
我使用Strtok()将文本分成句子(句子在出现\n时结束),但是当有一个句子在文本块中只包含\n字符时,例如:
/
你好,这是一些示例文本
This is the second sentence
上面的句子只是换了一行。
This is the last sentence.
/
该文件的输出如下:
0你好,这是一些示例文本
1 This is the second sentence
2 The sentence above is just a new line
3 This is the last
我正在尝试找出从文件中读取的句子是否具有某种模式。
到目前为止,我已经编写了从文件中逐行读取所有句子的代码,并将这些句子放入一个数组中。
val infile = "c:/input.txt" ;
fun readlist (infile : string) =
let val ins = TextIO.openIn infile
fun loop ins = case TextIO.inputLine ins of
SOME line => line :: loop ins
我使用了以下代码从文件中提取句子(这个句子应该包含部分或全部搜索关键字)
search_keywords=['mother','sing','song']
with open('text.txt', 'r') as in_file:
text = in_file.read()
sentences = text.split(".")
for sentence in sentences:
if (all(map(lambda word: word in sentence, se
我有一个文本文件:
1 This D
2 is V
3 one A
4 example
5 . P
1 This D
2 is V
3 another
4 example
我想提取第二列附加到以换行符expected output: ["this is one example", "this is another example"]分隔的列表中
with open("data.txt","r") as f:
print(f.read().split()[1])
但我只得到输出This。我该怎么做??获取
嗨,我想写一个代码,读取一个文本文件,并识别该文件中的句子与该句子中有重复的单词。我在考虑把文件中的每个句子都放在字典里,找出哪些句子是重复的。因为我是Python的新手,所以我需要一些帮助来编写代码。
这就是我到目前为止所知道的:
def Sentences():
def Strings():
l = string.split('.')
for x in range(len(l)):
print('Sentence', x + 1, ': ', l[x])
re
我想要自动生成多个文件。我有一个有用的Python脚本来生成一个带有句子的文件,这些句子模仿了我感兴趣的语言领域(来自的代码)。下面的代码以文本my_domain.txt作为输入,在其上训练一个马尔可夫语言模型,然后输出一个包含10个假句子的文件,这些句子读起来非常类似于输入文本中的句子。
import pandas as pd
import markovify #Markov Chain Generator
import sys
with open(r'/my_domain.txt') as f:
text = f.read()
text_model = mark
我想在一个文件中从另一个文件中grep一些单词。我的代码能够grep文件最后一行的单词,但不能grep它前面的单词。我不知道为什么,希望能在这里得到帮助。下面是我使用的perl脚本:
open(FILE1,"file1.txt") or die "Error, File1 could not open\n";
open(FILE2,"file2.txt") or die "Error, File2 could not open\n";
open(FILE3, ">file3.txt")
我创建了一个函数,读取text.txt文件,然后将text.txt文件的值传递给n_text.txt文件。在这个文件中,我将需要在每一行句子前面添加数字。示例
投入:
text.txt
this is my
txt file
use for the code
产出:
n_text.txt
1 this is my
2 txt file
3 use for the code
我已经尝试过这样的代码:
with open('text.txt') as file, open('n_text.txt') as file2:
lines = file.readli
我的程序必须创建另一个名为"CTL.py“的文件,其中包含文本文件大写中的每个单词,以及以"#”结尾的每个句子。我必须使用标题()。我的文本文件中有131行。我的思路是使用一个循环来读取和大写文本中的每一个字母,以及如何在readline()命令之后添加'#‘。对怎么做有什么想法吗?
def main():
myFile = open('/Users/Chandlers_Mac/Downloads/Lab9-2.txt', 'r')
for i in range(131):
data = myFile.r
不是像这样定义documents ...
documents = ["the mayor of new york was there", "machine learning can be useful sometimes","new york mayor was present"]
..。我想从两个不同的txt文件中读取相同的三个句子,第一个文件中的第一个句子,第二个文件中的第二个句子和第三个句子。
我想出了这个代码:
# read txt documents
os.chdir('text_data')
documents = [
我有一个Perl脚本,它将包含几个句子(Sentences.txt)的文本文件作为输入。每个句子都用一条白线隔开。该脚本为Sentences.txt中的每个句子创建单独的文本文件。例如,Sent1.txt用于Sentences.txt中的第一句,Sent2.txt用于Sentences.txt中的第二句,等等。
当我试图使用Sentences.txt函数将一个句子从SentX.txt打印到相应的单独文件(SentX.txt),并且这个句子包含一个%字符时,就会出现问题。我怎么才能解决这个问题?
这是代码:
#!/usr/bin/perl -w
use strict;
use warnings
我编写了PHP代码来检查一个单词是否在一个句子中。
我写了这段代码:
<?php
$text = "I go to school";
$word = file_get_contents("bad.txt");
if (strpos($text,$word)) {
echo 'true';
}
?>
但是它不起作用,因为txt文件如下所示:
test
hola
owb
如何使代码对照句子检查每一行上的单词,而不仅仅是一行?
此代码从句子的数据帧中保存文本文件,然后将每个文本文件保存为ssml文件。
如何将句子保存在新文件夹中?
max = len(sentences)
for i in range(0,max):
txt = sentences[i]
new_txt = starter + txt + ender
print(new_txt)
num = num + 1
with open("text" + str(num) + ".ssml", 'w+') as f:
f.writelines(ne
所以这是我的问题。我有一个非常大的csv文件,其中有3列。第一列是唯一的is。第二列是一个字符串,它是英语句子。第三列是描述第二列中句子的一串字标签(通常是3个标签,最多为5)。下面是一个例子。
id | sentence | tags
1 | "people walk dogs in the park" | "pet park health"
2 | "I am allergic to dogs" | "allergies health"
我想要做的是找出所有标记词与
我必须转换成.txt文件的.csv文件,我做了,我有以下代码。 text_list = []
csv_file = 'test.csv'
with open(csv_file,"r") as infile:
for line in infile:
line = line.split(",", 2)
text_list.append("\t".join(line))
txt_file = 'test.txt'
with open(txt_file, 'w'
我的程序是
从句
创建一个字典并将其放入外部txt文件中。
列一个数字列表,表示什么词在什么位置
使用数字和字典重新创建原始句子,并将其放入外部txt文件中。
但是,在重新创建该句子时,我会得到以下错误消息:
line 22, in <module>
newoutput = (wordDictionary[int(numbers)]) + " "
ValueError: invalid literal for int() with base 10: ''
这是我的密码
sentence = input("What is