- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个 C 程序,以便在某个字符串中找到最频繁的 n-gram。
一个n-gram是一个
contiguous sequence of n items from a given sequence of text
但是,函数 most_freq_ngram
中存在段错误。
参数依次为:
这是我的代码:
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
typedef struct nodo_t{
char* gram;
int count;
struct nodo_t * next;
} nodo_t;
typedef struct linked_list{
nodo_t * head;
} linked_list;
int compare_arrays(char * igram, char * list_gram, size_t ngram_len){
int i;
for(i=0;i<ngram_len;i++){
if(tolower(list_gram[i]) != tolower(igram[i])) return 0;
}
return 1;
}
void copy_array(char * igram, char * list_gram, size_t ngram_len){
int i;
for(i=0;i<ngram_len;i++)
list_gram[i] = tolower(igram[i]);
}
void add_gram(char * igram, linked_list * list, size_t ngram_len ){
if(list == NULL){
list = malloc(sizeof(linked_list));
nodo_t* head = malloc(sizeof(nodo_t));
head->count = 1;
head->next = NULL;
head->gram = malloc(ngram_len * sizeof(char));
int i;
for(i=0;i<ngram_len;i++)
head->gram[i] = igram[i];
list->head = head;
}else{
nodo_t * sent = list->head;
int found = 0;
while(sent->next != NULL && !found){
//Check every element, otherwise add to que
int same = compare_arrays(igram, sent->gram, ngram_len);
if(same){
sent->count++;
found = 1;
}
sent = sent->next;
}
if(!found){
sent->next = malloc(sizeof(nodo_t));
sent = sent->next;
sent->next = NULL;
sent->count = 1;
copy_array(igram, sent->gram, ngram_len);
}
}
}
void most_freq_ngram(const char* text, size_t text_len, size_t ngram_len, char** ngram){
int i;
linked_list * list = NULL;
for(i=0;i<text_len - ngram_len +1;i++){
char igram[ngram_len+1];
int j;
int temp_i = i;
for(j=0;j<ngram_len;j++){
igram[j] = text[temp_i];
temp_i++;
}
igram[ngram_len] = '\0';
add_gram(igram, list, ngram_len);
}
//Check list for most frequent element
char * most_frequent = malloc(ngram_len * sizeof(char));
int frequency = 0;
nodo_t * sent = list->head;
if(sent == NULL ){
int i;
for(i=0;i<ngram_len;i++)
most_frequent[i] = '\0';
return;
}
while(sent->next != NULL){
if(sent->count > frequency){
copy_array(sent->gram, most_frequent, ngram_len);
frequency = sent->count;
}
}
*ngram = most_frequent;
return ;
}
int main(){
size_t ngram_len = 2;
char *ngram = malloc((ngram_len+1) * sizeof(char));
size_t text_len = 5;
const char text[6] = {'a','a','a','a','a', '\0'};
most_freq_ngram(text, text_len, ngram_len, &ngram);
return 0;
}
最佳答案
您的函数 void add_gram(char * igram, linked_list * list, size_t ngram_len )
不会更改 list
。它更改了 list
的副本。 most_freq_ngram
中的原始list
保持不变(一个NULL指针),导致nodo_t * sent = list->head;
中出现segfault。将add_gram
的第二个参数改为linked_list ** list
,并相应重写函数。
关于找不到 n-gram 算法的泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41839013/
我正在尝试在 Windows 上运行的小于 1GB 的 VM 上设置 YouTrack 和 TeamCity。使用率将非常低(用户和请求)。这是一个 POC 环境,如果它有效,我可能会将它推送到一个超
所以我在尝试使用 FORFILES 解决这个问题时遇到了麻烦。我正在尝试获取不超过 4 天的文件。所以基本上少于 4 天。然而,这似乎不太可能,因为/d -4 获取所有 4 天或更早的项目。 以下是我
如何从下面的 events 表中选择小于 15 分钟前创建的 events? CREATE TABLE events ( created_at timestamp NOT NULL DEFAU
Google Analytics Realtime提供 rt:minutesAgo ,可以过滤查询。 然而,它是一个维度而不是一个度量标准,<=不能在过滤器中使用。 假设我想在最后 n 分钟内获得一些
iOS 核心数据 - 严重的应用程序错误 - 尝试插入 nil 你好, 我的应用程序实际上运行稳定,但在极少数情况下它会崩溃并显示此错误消息... 2019-04-02 20:48:52.437172
我想制作一个 html div 以快速向右移动(例如不到 1 秒)并消失。然后1秒后再次直接出现在这个过程最开始div的位置。此过程将由单击按钮并重复 10 次触发。 我试图在 CSS 中使用过渡属性
我发现使用 TimeTrigger 是 Windows 10 (UWP) 上计划后台任务的方式。但是看起来我们需要给出的最小数字是 15 分钟。只是想知道,即使我们安排它在接下来的 1 分钟内运行,警
我必须在 1 秒内在屏幕上打印 2^20 行整数 printf 不够快,还有其他易于使用的快速输出替代方法吗? 每一行只包含 1 个整数。 我要求它用于竞争性编程问题,我必须将其源代码提交给法官。 最
我是一名优秀的程序员,十分优秀!