在电子邮件中查找名称(低级I / O)

时间:2014-10-10 13:15:11

标签: matlab while-loop fgets low-level-io

第二轮:在电子邮件中挑选领导者 好吧,所以我的下一个问题是试图找出领导者在项目中的位置。为了确定这一点,我们会收到一封电子邮件,并且必须找到谁说'#34;你想要......" (资本化可能会有所不同)。我觉得我的代码应该在大多数情况下工作,但我确实有一个问题,想弄清楚如何正确填充我的单元格数组。我可以让它来创建单元格数组,但它只是将电子邮件重新放入其中。所以每个细胞基本上都是这个名字。

function[Leader_Name] = teamPowerHolder(email)

email = fopen(email, 'r'); %// Opens my file
lines = fgets(email); %// Reads the first line

conversations = {lines}; %// Creates my cell array


while ischar(lines) %// Populates my cell array, just not correct
    Convo = fgets(email);
    if Convo == -1 %// Prevents it from just logging -1 into my cell array like a jerk
        break; %// Returns to function
    end
    conversations = [conversations {lines}]; %// Populates my list
end
Sentences = strfind(conversations,'Do you want'); %// Locates the leader position


Leader_Name = Sentences{1}; %// Indexes that position

fclose(email);
end

理想情况下我需要做的是找到' / n'角色(因此我使用fgets的原因),但我不知道如何做到这一点。我试着让我的while循环像:

while lines == '/n'

但这不正确。我觉得我知道怎么做' / n'我,我无法想到它。所以我很欣赏一些提示或提示。我总是可以尝试strsplit或strtok函数,但我需要填充我的单元格数组,以免混乱。

请和感谢您的帮助:)

Test Case:
Anna: Hey guys, so I know that he just assigned this project, but I want to go ahead   and get started on it.
Can you guys please respond and let me know a weekly meeting time that will work for you?

Wiley: Ummmmm no because ain't nobody got time for that.

John: Wiley? What kind of a name is that? .-.

Wiley: It's better than john. >.>

Anna: Hey boys, let's grow up and talk about a meeting time.
Do you want to have a weekly meeting, or not?

Wiley: I'll just skip all of them and not end up doing anything for the project anyway.
So I really don't care so much.

John: Yes, Anna, I'd like to have a weekly meeting.
Thank you for actually being a good teammate and doing this. :)

out2 = teamPowerHolder('teamPowerHolder_convo2.txt')
    => 'Anna'

1 个答案:

答案 0 :(得分:1)

它不起作用的主要原因是因为您应该更新循环中的lines变量,但是您正在创建一个名为Convo的新变量,而不是更新。这就是为什么每次将lines放入单元格数组时,它只是重复放入第一行,永远不会退出循环。


但是,我建议你做的是读取每行,然后查找:字符,然后提取字符串,直到你第一次遇到这个字符减1 因为您不希望包含实际的:字符本身。这很可能对应于发言人的姓名。如果我们缺少这种情况,那么那个人还在说话。因此,您必须保留一个变量来跟踪谁仍然当前在说话,直到找到“你想要的”字符串。无论谁说这个,我们都会回复当前正在谈话的人,当然要突破这个循环!要确保该行不区分大小写,您需要将字符串转换为较低的字符串。

可能存在无领导的情况。在这种情况下,您可能希望返回空字符串。因此,将Leader_Name初始化为空字符串。在这种情况下,那将是[]。这样,如果我们通过电子邮件找不到领导者,MATLAB将返回[]

你拥有的逻辑非常正确,但我甚至不愿意将东西存储到单元格数组中。只需检查文本文件中的每一行,并跟踪当前正在说话的人,直到我们遇到另一个:字符的句子。我们可以使用strfind来促进这一点。但是,我要提到的一个小问题是,如果说话的人在他们的谈话中包含:,那么这种方法就会破裂。

从我正在看你的测试用例的谈话来看,这可能不是这样的,所以我们没事。因此,借用当前的代码,只需这样做:

function[Leader_Name] = teamPowerHolder(email)

Leader_Name = []; %// Initialize leader name to empty
name = [];    

email = fopen(email, 'r'); %// Opens my file
lines = fgets(email); %// Reads the first line

while ischar(lines)

    % // Get a line in your e-mail
    lines = fgets(email);

    % // Quit like a boss if you see a -1
    if lines == -1
        break;
    end

    % // Check if this line has a ':' character.
    % // If we do, then another person is talking.
    % // Extract the characters just before the first ':' character
    % // as we don't want the ':' character in the name
    % // If we don't encounter a ':' character, then the same person is
    % // talking so don't change the current name
    idxs = strfind(lines, ':');
    if ~isempty(idxs)
        name = lines(1:idxs(1)-1);
    end    

    % // If we find "do you want" in this sentence, then the leader
    % // is found, so quit.
    if ~isempty(strfind(lower(lines), 'do you want'))
        Leader_Name = name;
        break;
    end
end

通过在测试用例中运行上面的代码,这就是我得到的:

out2 = teamPowerHolder('teamPowerHolder_convo2.txt')

out2 = 

Anna